2bbdb00a4d
包含节点账本、每日清算,以及 Ubuntu 交互式部署脚本。 Co-authored-by: Cursor <cursoragent@cursor.com>
42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
"""应用入口。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.responses import JSONResponse
|
|
|
|
import app.models # noqa: F401 注册全部表
|
|
from app.api import router
|
|
from app.database import Base, engine
|
|
from app.errors import AppError
|
|
from app.scheduler import start_scheduler, stop_scheduler
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s")
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(_: FastAPI):
|
|
Base.metadata.create_all(bind=engine)
|
|
start_scheduler()
|
|
try:
|
|
yield
|
|
finally:
|
|
stop_scheduler()
|
|
|
|
|
|
app = FastAPI(title="DePIN 算力节点与任务调度", version="1.0.0", lifespan=lifespan)
|
|
app.include_router(router)
|
|
|
|
|
|
@app.exception_handler(AppError)
|
|
async def handle_app_error(_: Request, exc: AppError) -> JSONResponse:
|
|
return JSONResponse(status_code=exc.status_code, content={"detail": exc.message})
|
|
|
|
|
|
@app.get("/health")
|
|
def health() -> dict[str, str]:
|
|
return {"status": "ok"}
|