|
@@ -10,7 +10,7 @@ Bootstrap(app)
|
|
# 配置 MySQL 数据库连接
|
|
# 配置 MySQL 数据库连接
|
|
app.config['MYSQL_DATABASE_USER'] = 'root'
|
|
app.config['MYSQL_DATABASE_USER'] = 'root'
|
|
app.config['MYSQL_DATABASE_PASSWORD'] = 'guo784512'
|
|
app.config['MYSQL_DATABASE_PASSWORD'] = 'guo784512'
|
|
-app.config['MYSQL_DATABASE_DB'] = 'your_database'
|
|
|
|
|
|
+app.config['MYSQL_DATABASE_DB'] = 'short_url'
|
|
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
|
|
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
|
|
app.config['MYSQL_DATABASE_PORT'] = 3306
|
|
app.config['MYSQL_DATABASE_PORT'] = 3306
|
|
|
|
|
|
@@ -29,13 +29,39 @@ def is_short_url_exists(short_url):
|
|
result = cursor.fetchone()
|
|
result = cursor.fetchone()
|
|
return result[0] > 0
|
|
return result[0] > 0
|
|
|
|
|
|
|
|
+
|
|
|
|
+"""旧"""
|
|
|
|
+# def generate_unique_short_url():
|
|
|
|
+# current_length = 3
|
|
|
|
+# while True:
|
|
|
|
+# short_url = ''.join(random.choices(string.digits + string.ascii_letters, k=current_length))
|
|
|
|
+# if not is_short_url_exists(short_url):
|
|
|
|
+# yield short_url # 使用 yield 生成迭代器
|
|
|
|
+# current_length = 4 # 在使用完当前长度后,设置下一次长度为4
|
|
|
|
+
|
|
def generate_unique_short_url():
|
|
def generate_unique_short_url():
|
|
current_length = 3
|
|
current_length = 3
|
|
while True:
|
|
while True:
|
|
short_url = ''.join(random.choices(string.digits + string.ascii_letters, k=current_length))
|
|
short_url = ''.join(random.choices(string.digits + string.ascii_letters, k=current_length))
|
|
if not is_short_url_exists(short_url):
|
|
if not is_short_url_exists(short_url):
|
|
- yield short_url # 使用 yield 生成迭代器
|
|
|
|
- current_length = 4 # 在使用完当前长度后,设置下一次长度为4
|
|
|
|
|
|
+ yield short_url
|
|
|
|
+ else:
|
|
|
|
+ current_length += 1 # 只有在使用完所有 3 位短链接后才递增长度
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def check_and_reconnect_db(db_connection):
|
|
|
|
+ try:
|
|
|
|
+ db_connection.ping(reconnect=True)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ print(f"Error: {e}")
|
|
|
|
+ db_connection = pymysql.connect(
|
|
|
|
+ user=app.config['MYSQL_DATABASE_USER'],
|
|
|
|
+ password=app.config['MYSQL_DATABASE_PASSWORD'],
|
|
|
|
+ database=app.config['MYSQL_DATABASE_DB'],
|
|
|
|
+ host=app.config['MYSQL_DATABASE_HOST'],
|
|
|
|
+ port=app.config['MYSQL_DATABASE_PORT']
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
|
|
short_url_generator = generate_unique_short_url()
|
|
short_url_generator = generate_unique_short_url()
|
|
|
|
|
|
@@ -62,11 +88,13 @@ def shorten():
|
|
with db.cursor() as cursor:
|
|
with db.cursor() as cursor:
|
|
cursor.execute("INSERT INTO urls (original_url, short_url) VALUES (%s, %s)", (original_url, short_url))
|
|
cursor.execute("INSERT INTO urls (original_url, short_url) VALUES (%s, %s)", (original_url, short_url))
|
|
db.commit()
|
|
db.commit()
|
|
|
|
+ check_and_reconnect_db(db) # 在每次操作后检查并重新连接数据库
|
|
|
|
|
|
return render_template('result.html', original_url=original_url, short_url=short_url)
|
|
return render_template('result.html', original_url=original_url, short_url=short_url)
|
|
|
|
|
|
@app.route('/<short_url>')
|
|
@app.route('/<short_url>')
|
|
def redirect_to_original(short_url):
|
|
def redirect_to_original(short_url):
|
|
|
|
+ check_and_reconnect_db(db) # 在每次操作后检查并重新连接数据库
|
|
# 从数据库中获取原始URL并重定向
|
|
# 从数据库中获取原始URL并重定向
|
|
with db.cursor() as cursor:
|
|
with db.cursor() as cursor:
|
|
cursor.execute("SELECT original_url FROM urls WHERE short_url = %s", (short_url,))
|
|
cursor.execute("SELECT original_url FROM urls WHERE short_url = %s", (short_url,))
|
|
@@ -80,6 +108,7 @@ def redirect_to_original(short_url):
|
|
if __name__ == '__main__':
|
|
if __name__ == '__main__':
|
|
# 创建数据库表
|
|
# 创建数据库表
|
|
with app.app_context():
|
|
with app.app_context():
|
|
|
|
+ check_and_reconnect_db(db) # 在每次操作后检查并重新连接数据库
|
|
with db.cursor() as cursor:
|
|
with db.cursor() as cursor:
|
|
cursor.execute("CREATE TABLE IF NOT EXISTS urls (id INT AUTO_INCREMENT PRIMARY KEY, original_url VARCHAR(255), short_url VARCHAR(255), UNIQUE KEY short_url_unique (short_url))")
|
|
cursor.execute("CREATE TABLE IF NOT EXISTS urls (id INT AUTO_INCREMENT PRIMARY KEY, original_url VARCHAR(255), short_url VARCHAR(255), UNIQUE KEY short_url_unique (short_url))")
|
|
db.commit()
|
|
db.commit()
|