浏览代码

修改数据库名称

Naruto 1 年之前
父节点
当前提交
de2ea08ef2

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 6 - 0
.idea/inspectionProfiles/Project_Default.xml

@@ -0,0 +1,6 @@
+<component name="InspectionProjectProfileManager">
+  <profile version="1.0">
+    <option name="myName" value="Project Default" />
+    <inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
+  </profile>
+</component>

+ 6 - 0
.idea/inspectionProfiles/profiles_settings.xml

@@ -0,0 +1,6 @@
+<component name="InspectionProjectProfileManager">
+  <settings>
+    <option name="USE_PROJECT_PROFILE" value="false" />
+    <version value="1.0" />
+  </settings>
+</component>

+ 10 - 0
.idea/misc.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="Black">
+    <option name="sdkName" value="Python 3.12 (python)" />
+  </component>
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (python)" project-jdk-type="Python SDK" />
+  <component name="PyPackaging">
+    <option name="earlyReleasesAsUpgrades" value="true" />
+  </component>
+</project>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/short_url.iml" filepath="$PROJECT_DIR$/.idea/short_url.iml" />
+    </modules>
+  </component>
+</project>

+ 19 - 0
.idea/short_url.iml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="PYTHON_MODULE" version="4">
+  <component name="Flask">
+    <option name="enabled" value="true" />
+  </component>
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="jdk" jdkName="Python 3.12 (python)" jdkType="Python SDK" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+  <component name="TemplatesService">
+    <option name="TEMPLATE_CONFIGURATION" value="Jinja2" />
+    <option name="TEMPLATE_FOLDERS">
+      <list>
+        <option value="$MODULE_DIR$/templates" />
+      </list>
+    </option>
+  </component>
+</module>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="" vcs="Git" />
+  </component>
+</project>

+ 32 - 3
app.py

@@ -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()

+ 1 - 0
templates/index.html

@@ -7,6 +7,7 @@
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>短网址生成器</title>
     <title>短网址生成器</title>
+	<link rel="shortcut icon" href="https://29455906.s21i.faiusr.com/4/ABUIABAEGAAg9_TSrAYoxvHpgAcwgAI4gAI.png">
     <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0-alpha1/css/bootstrap.min.css" rel="stylesheet">
     <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0-alpha1/css/bootstrap.min.css" rel="stylesheet">
     <style>
     <style>
         body {
         body {