-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
60 lines (49 loc) · 1.72 KB
/
app.py
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
import uvicorn
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from routers import main_router, ai_router, data_router
import logging
import colorlog
from config.config_manager import config
def setup_logging():
handler = colorlog.StreamHandler()
log_colors = config.get('logging.colors', {
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
})
handler.setFormatter(colorlog.ColoredFormatter(
'%(asctime)s - %(log_color)s%(levelname)s%(reset)s [in %(pathname)s:%(lineno)d] - %(message)s',
log_colors=log_colors
))
root_logger = logging.getLogger()
root_logger.addHandler(handler)
root_logger.setLevel(logging.DEBUG if config.get('app.debug') else logging.INFO)
ignored_loggers = config.get('logging.ignored_loggers', {})
for logger_name, level in ignored_loggers.items():
logging.getLogger(logger_name).setLevel(getattr(logging, level))
setup_logging()
app = FastAPI()
# 配置静态文件目录
app.mount("/static", StaticFiles(directory="static"), name="static")
# 配置模板目录
templates = Jinja2Templates(directory="templates")
# 主页路由
@app.get("/")
async def root(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
# Include routers
app.include_router(main_router)
app.include_router(ai_router, prefix="/ai")
app.include_router(data_router, prefix="/data")
if __name__ == "__main__":
uvicorn.run(
app,
host=config.get('app.host', '127.0.0.1'),
port=config.get('app.port', 5000),
log_config=None,
use_colors=True
)