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 <noreply@anthropic.com>
This commit is contained in:
Misaka_Company
2026-06-12 12:27:45 +08:00
parent bc461c12ce
commit 8639fedf74
6 changed files with 35 additions and 11 deletions

15
.env.example Normal file
View File

@@ -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=

3
.gitignore vendored
View File

@@ -6,6 +6,9 @@ log
*.spec
temp
# 环境变量
.env
# Claude 临时文件
.claude/
tmpclaude-*

View File

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

View File

@@ -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 设置一致
}

View File

@@ -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'
}

View File

@@ -2,3 +2,4 @@ pyodbc>=5.0.0
pandas>=1.5.0
sqlalchemy>=2.0.0
openpyxl>=3.0.0
python-dotenv>=1.0.0