-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
69 lines (57 loc) · 2.09 KB
/
app.py
File metadata and controls
69 lines (57 loc) · 2.09 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
import logging
import os
import uvicorn
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from tortoise.contrib.fastapi import register_tortoise
from exception.all_exception import global_exception_handlers
from web.user_web import user
def create_app():
_app = FastAPI(
title="digital portrait service",
version="v1.0.1",
# 全局以及自定义异常捕获
exception_handlers=global_exception_handlers
)
_app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 添加路由
_app.include_router(router=user, prefix="/v1/user", tags=["user"])
# SQLite 数据源路径
sqlite_db_path = "./db/app.db" # 替换为你的 SQLite 数据库路径
# 注册 SQLite 数据源
register_tortoise(
_app,
db_url=f"sqlite://{sqlite_db_path}",
modules={'models': ['entity.database.sqlite']}, # 替换成你定义的模型路径
generate_schemas=True, # 若首次初始化需要建表可打开
add_exception_handlers=True,
)
@_app.on_event("startup")
async def app_startup():
os.makedirs("logs", exist_ok=True)
# 清除旧的 handler,防止重复添加
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("logs/app.log", mode='a', encoding='utf-8'),
logging.StreamHandler()
]
)
logging.info("fastapi run------------------ Starting")
@_app.on_event("shutdown")
async def shutdown_event():
logging.info("fastapi run------------------ ending")
return _app
# app = create_app() # 正式环境切换
if __name__ == '__main__':
app = create_app()
uvicorn.run(app, host=os.environ.get('SERVER_HOST', '0.0.0.0'), port=int(os.environ.get('SERVER_PORT', 8000)))