main.py
1.73 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
# -*- coding: utf-8 -*-
"""
文件名: main.py
职责: 项目启动入口,初始化 FastAPI 应用,挂载路由和静态资源。
注意: 不要删除这个注释。
"""
import uvicorn
import os
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from contextlib import asynccontextmanager
from app.core.scheduler import SchedulerManager
from app.core.config import settings
# 基础目录
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
@asynccontextmanager
async def lifespan(app: FastAPI):
# 启动时执行
print("[*] Starting Data Analyze WebUI...")
# 启动定时任务调度器
await SchedulerManager.start()
yield
# 关闭时执行
print("[*] Stopping Data Analyze WebUI...")
await SchedulerManager.stop()
app = FastAPI(
title="问界门店运营数据分析平台",
description="支持 ODS/DWS/DWM/DWA 各层数据同步任务及分析模版展示",
version="1.0.0",
lifespan=lifespan
)
# 挂载静态文件
app.mount("/static", StaticFiles(directory=os.path.join(BASE_DIR, "app/static")), name="static")
# 配置模板引擎
templates = Jinja2Templates(directory=os.path.join(BASE_DIR, "app/templates"))
@app.get("/")
async def root(request: Request):
"""返回主页面"""
return templates.TemplateResponse(
request=request,
name="index.html",
context={}
)
@app.get("/api/health")
async def health():
"""健康检查接口"""
return {"status": "ok", "message": "Welcome to Data Analyze WebUI API"}
if __name__ == "__main__":
# 端口指定 9500
uvicorn.run(
"main:app",
host="0.0.0.0",
port=9500,
reload=settings.APP_DEBUG
)