-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
220 lines (169 loc) · 5.83 KB
/
app.py
File metadata and controls
220 lines (169 loc) · 5.83 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python3
"""
Litefs 生产环境部署示例
本示例演示:
1. 多进程模式 - workers 参数启动多进程
2. 日志配置 - 结构化日志输出到文件
3. 缓存系统 - 内存/Redis 缓存
4. 健康检查 - 用于负载均衡器的健康检查端点
5. Debug 工具 - 环境变量控制的调试工具栏
6. 优雅关闭 - 正确处理进程信号
启动方式:
开发模式: python app.py
生产模式: WORKERS=4 python app.py
启用调试: DEBUG_TOOLBAR=true python app.py
"""
import os
import sys
import signal
import logging
import logging.handlers
from datetime import datetime
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', 'src'))
from litefs.core import Litefs
from litefs.handlers import Response
from litefs.routing import get, post
from litefs.middleware.logging import LoggingMiddleware
from litefs.middleware.csrf import CSRFMiddleware
from litefs.cache import MemoryCache
from litefs.debug import DebugMiddleware
APP_DIR = os.path.dirname(os.path.abspath(__file__))
debug_mode = os.environ.get('DEBUG', 'false').lower() == 'true'
debug_toolbar = os.environ.get('DEBUG_TOOLBAR', 'false').lower() == 'true'
workers = int(os.environ.get('WORKERS', '1'))
secret_key = os.environ.get('SECRET_KEY', 'production-secret-key-change-me')
def setup_logging(app: Litefs) -> None:
log_dir = os.path.join(APP_DIR, 'logs')
os.makedirs(log_dir, exist_ok=True)
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
formatter = logging.Formatter(log_format)
file_handler = logging.handlers.RotatingFileHandler(
os.path.join(log_dir, 'app.log'),
maxBytes=10 * 1024 * 1024,
backupCount=5,
encoding='utf-8'
)
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.INFO)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(formatter)
console_handler.setLevel(logging.DEBUG if debug_mode else logging.INFO)
app_logger = logging.getLogger('litefs')
app_logger.setLevel(logging.DEBUG)
app_logger.addHandler(file_handler)
app_logger.addHandler(console_handler)
app = Litefs(
host='0.0.0.0',
port=8085,
debug=debug_mode,
workers=workers,
secret_key=secret_key
)
app.add_static('/static', os.path.join(APP_DIR, 'static'))
setup_logging(app)
app.add_middleware(LoggingMiddleware)
if not debug_mode:
app.add_middleware(CSRFMiddleware)
if debug_toolbar:
app.add_middleware(DebugMiddleware)
try:
from litefs.cache import RedisCache
app.cache = RedisCache(
host=os.environ.get('REDIS_HOST', 'localhost'),
port=int(os.environ.get('REDIS_PORT', 6379)),
db=int(os.environ.get('REDIS_DB', 0)),
key_prefix='litefs:'
)
app.logger.info("Redis 缓存已连接")
except Exception as e:
app.logger.warning(f"Redis 连接失败,使用内存缓存: {e}")
app.cache = MemoryCache()
class HealthStatus:
def __init__(self):
self.start_time = datetime.now()
self.requests_count = 0
self.errors_count = 0
@property
def uptime(self) -> str:
delta = datetime.now() - self.start_time
hours, remainder = divmod(int(delta.total_seconds()), 3600)
minutes, seconds = divmod(remainder, 60)
return f"{hours}h {minutes}m {seconds}s"
health_status = HealthStatus()
@get('/', name='index')
def index(request):
health_status.requests_count += 1
data = {
'message': 'Litefs 生产环境示例',
'features': [
'多进程模式',
'结构化日志',
'Redis 缓存',
'健康检查端点',
'优雅关闭'
],
'workers': workers,
'debug': debug_mode,
'uptime': health_status.uptime
}
return request.render_template('index.html', **data)
@get('/health', name='health')
def health_check(request):
return Response.json({
'status': 'healthy',
'uptime': health_status.uptime,
'requests': health_status.requests_count,
'errors': health_status.errors_count,
'workers': workers,
'timestamp': datetime.now().isoformat()
})
@get('/ready', name='ready')
def readiness_check(request):
checks = {
'server': True,
'cache': False,
}
try:
app.cache.put('health_check', 'ok')
if app.cache.get('health_check') == 'ok':
checks['cache'] = True
except Exception:
pass
all_healthy = all(checks.values())
status_code = 200 if all_healthy else 503
return Response.json({
'ready': all_healthy,
'checks': checks
}, status_code=status_code)
@get('/metrics', name='metrics')
def metrics(request):
return Response.json({
'uptime_seconds': (datetime.now() - health_status.start_time).total_seconds(),
'requests_total': health_status.requests_count,
'errors_total': health_status.errors_count,
'workers': workers,
})
@post('/api/cache/clear', name='cache_clear')
def clear_cache(request):
try:
app.cache._cache.clear()
return Response.json({'success': True, 'message': '缓存已清除'})
except Exception as e:
health_status.errors_count += 1
return Response.json({'success': False, 'error': str(e)}, status_code=500)
@get('/api/config', name='config')
def get_config(request):
return Response.json({
'workers': workers,
'debug': debug_mode,
'log_level': 'DEBUG' if debug_mode else 'INFO'
})
app.register_routes(__name__)
def signal_handler(signum, frame):
app.logger.info(f"收到信号 {signum},正在优雅关闭...")
sys.exit(0)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
if __name__ == '__main__':
app.logger.info(f"启动 Litefs 生产服务器,workers={workers}")
app.run()