-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
89 lines (77 loc) · 2.58 KB
/
Copy pathmain.py
File metadata and controls
89 lines (77 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import yaml
import os
import sys
from flask import Flask, render_template
import redis
from flask_cors import CORS
from email_service import EmailVerificationService
from auth_routes import auth_bp
def create_default_config():
"""创建默认配置文件"""
default_config = {
'smtp': {
'server': 'smtp.example.com',
'port': 587,
'username': 'your-email@example.com',
'password': 'your-password'
},
'redis': {
'host(不要携带http和https!)': 'localhost',
'port': 6379,
'db': 0,
'username': 'default',
'password': '',
'decode_responses': True
},
'app': {
'host': '0.0.0.0',
'port': 5002,
'debug': False
}
}
with open('config.yml', 'w', encoding='utf-8') as f:
yaml.dump(default_config, f, default_flow_style=False, allow_unicode=True)
print("已创建默认配置文件 config.yml,请修改其中的配置信息后再运行程序。")
return default_config
def load_config():
"""加载配置文件,如果不存在则创建默认配置"""
if not os.path.exists('config.yml'):
print("配置文件 config.yml 不存在,正在创建默认配置文件...")
print("请修改 config.yml 中的配置信息后再运行程序。")
return create_default_config()
try:
with open('config.yml', 'r', encoding='utf-8') as f:
return yaml.safe_load(f)
except Exception as e:
print(f"读取配置文件时出错: {e}")
sys.exit(1)
app = Flask(__name__)
CORS(app)
config = load_config()
required_sections = ['redis', 'smtp', 'app']
missing_sections = [section for section in required_sections if section not in config]
if missing_sections:
print(f"配置文件缺少必要配置项: {', '.join(missing_sections)}")
sys.exit(1)
REDIS_CONFIG = config['redis']
try:
redis_client = redis.Redis(**REDIS_CONFIG)
redis_client.ping()
except Exception as e:
print(f"无法连接到Redis: {e}")
sys.exit(1)
SMTP_CONFIG = config['smtp']
email_service = EmailVerificationService(SMTP_CONFIG, redis_client)
from auth_routes import init_email_service
init_email_service(email_service)
app.register_blueprint(auth_bp)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app_config = config['app']
app.run(
host=app_config.get('host', '0.0.0.0'),
port=app_config.get('port', 5000),
debug=app_config.get('debug', False)
)