91 lines
2.3 KiB
Python
91 lines
2.3 KiB
Python
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from .core.config import settings
|
|
from .core.database import init_db
|
|
from .api.routes import jobs as jobs_routes
|
|
from .core.logging import setup_logging
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
app = FastAPI(
|
|
title=settings.APP_NAME,
|
|
version=settings.APP_VERSION,
|
|
docs_url="/docs",
|
|
redoc_url="/redoc",
|
|
openapi_url=f"{settings.API_V1_PREFIX}/openapi.json",
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.CORS_ORIGINS,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
app.include_router(jobs_routes.router, prefix=f"{settings.API_V1_PREFIX}/jobs", tags=["jobs"])
|
|
|
|
@app.on_event("startup")
|
|
def _on_startup() -> None:
|
|
setup_logging(settings.LOG_LEVEL, settings.LOG_FORMAT)
|
|
init_db()
|
|
|
|
@app.get("/healthz")
|
|
def healthz() -> dict:
|
|
return {"status": "ok"}
|
|
|
|
return app
|
|
|
|
|
|
app = create_app()
|
|
|
|
"""FastAPI 主应用"""
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from contextlib import asynccontextmanager
|
|
|
|
from .config import settings
|
|
from .api.v1 import jobs, upload, results
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""应用生命周期管理"""
|
|
# 启动时
|
|
print("🚀 Starting BtToxin Pipeline API...")
|
|
yield
|
|
# 关闭时
|
|
print("👋 Shutting down BtToxin Pipeline API...")
|
|
|
|
app = FastAPI(
|
|
title=settings.APP_NAME,
|
|
version=settings.APP_VERSION,
|
|
description="Automated Bacillus thuringiensis toxin mining pipeline",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.CORS_ORIGINS,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 路由
|
|
app.include_router(jobs.router, prefix=f"{settings.API_V1_STR}/jobs", tags=["jobs"])
|
|
app.include_router(upload.router, prefix=f"{settings.API_V1_STR}/upload", tags=["upload"])
|
|
app.include_router(results.router, prefix=f"{settings.API_V1_STR}/results", tags=["results"])
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {
|
|
"name": settings.APP_NAME,
|
|
"version": settings.APP_VERSION,
|
|
"status": "healthy"
|
|
}
|
|
|
|
@app.get("/health")
|
|
async def health():
|
|
return {"status": "ok"}
|