From 8639fedf7461274c5283e2051ef30d2de50b7fe2 Mon Sep 17 00:00:00 2001 From: Misaka_Company Date: Fri, 12 Jun 2026 12:27:45 +0800 Subject: [PATCH] refactor: move credentials to .env and add python-dotenv support Replace hardcoded database credentials, ntfy tokens, and Uptime Kuma push URLs with environment variable lookups via python-dotenv. Add .env.example template and update .gitignore to exclude .env. Co-Authored-By: Claude Opus 4.6 --- .env.example | 15 +++++++++++++++ .gitignore | 3 +++ config/__init__.py | 3 +++ config/app_settings.py | 10 +++++----- config/database.py | 12 +++++++----- requirements.txt | 3 ++- 6 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..6d68e09 --- /dev/null +++ b/.env.example @@ -0,0 +1,15 @@ +# SQL Server +DB_DRIVER=ODBC Driver 18 for SQL Server +DB_SERVER= +DB_DATABASE= +DB_USERNAME= +DB_PASSWORD= + +# ntfy +NTFY_SERVER_URL= +NTFY_TOPIC= +NTFY_TOKEN= + +# Uptime Kuma +UPTIME_KUMA_PUSH_URL= +EXCEL_SYNC_UPTIME_KUMA_PUSH_URL= diff --git a/.gitignore b/.gitignore index 9ae416a..b04ee6c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,9 @@ log *.spec temp +# 环境变量 +.env + # Claude 临时文件 .claude/ tmpclaude-* \ No newline at end of file diff --git a/config/__init__.py b/config/__init__.py index ff8e21b..f4e146e 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -1,6 +1,9 @@ # config/__init__.py # 统一配置导出接口 +from dotenv import load_dotenv +load_dotenv() + # 数据库配置 from .database import SQL_SERVER_CONFIG, SQL_SERVER_CONN, DB_CONFIG, ACCESS_DRIVER diff --git a/config/app_settings.py b/config/app_settings.py index cf7a40a..a04201d 100644 --- a/config/app_settings.py +++ b/config/app_settings.py @@ -19,9 +19,9 @@ LOG_TABLE_CONFIG = { # 从原 config.py 抽取 NTFY_CONFIG = { 'enabled': True, - 'server_url': 'https://ntfy.server10086.icu', - 'topic': 'bld', - 'token': 'tk_eop5fs66acxtwxf6vlkiojhdvkgb0', + 'server_url': os.environ.get('NTFY_SERVER_URL', ''), + 'topic': os.environ.get('NTFY_TOPIC', ''), + 'token': os.environ.get('NTFY_TOKEN', ''), 'priority': { 'error': 'high', 'critical': 'urgent' @@ -32,14 +32,14 @@ NTFY_CONFIG = { # 增量同步服务心跳 UPTIME_KUMA_CONFIG = { 'enabled': True, - 'push_url': 'https://uptimekuma.server10086.icu/api/push/clMSgOQs4CJeF7DJAOiHaFaaZzoL8eB9', + 'push_url': os.environ.get('UPTIME_KUMA_PUSH_URL', ''), 'heartbeat_interval': 59 # 心跳间隔(秒),需要与 Uptime Kuma 设置一致 } # Excel 同步服务心跳 EXCEL_SYNC_UPTIME_KUMA_CONFIG = { 'enabled': True, - 'push_url': 'https://uptimekuma.server10086.icu/api/push/MEXi5DYnC3nTMs2OfMqdelK3FawymtVY', + 'push_url': os.environ.get('EXCEL_SYNC_UPTIME_KUMA_PUSH_URL', ''), 'heartbeat_interval': 59 # 心跳间隔(秒),需要与 Uptime Kuma 设置一致 } diff --git a/config/database.py b/config/database.py index e41bd7f..e2f2233 100644 --- a/config/database.py +++ b/config/database.py @@ -1,13 +1,15 @@ # config/database.py # 统一的数据库配置 +import os + # ================= SQL Server 配置 ================= SQL_SERVER_CONFIG = { - 'driver': 'ODBC Driver 18 for SQL Server', - 'server': '192.168.110.114', - 'database': 'CompanyDB', - 'username': 'peng', - 'password': 'Cqbld123456.', + 'driver': os.environ.get('DB_DRIVER', 'ODBC Driver 18 for SQL Server'), + 'server': os.environ.get('DB_SERVER', ''), + 'database': os.environ.get('DB_DATABASE', ''), + 'username': os.environ.get('DB_USERNAME', ''), + 'password': os.environ.get('DB_PASSWORD', ''), 'TrustServerCertificate': 'yes' } diff --git a/requirements.txt b/requirements.txt index ae554ba..e2ca18f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ pyodbc>=5.0.0 pandas>=1.5.0 sqlalchemy>=2.0.0 -openpyxl>=3.0.0 \ No newline at end of file +openpyxl>=3.0.0 +python-dotenv>=1.0.0 \ No newline at end of file