123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- """
- 文献启明星 API 服务器
- FastAPI应用程序入口
- """
- import logging
- import uvicorn
- from fastapi import FastAPI, HTTPException
- from fastapi.middleware.cors import CORSMiddleware
- from pathlib import Path
- import sys
- import os
- # 添加项目根目录到Python路径
- sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
- from backend.api import research, chat, auth, users, chat_history, research_history
- from backend.config import CORS_ORIGINS, BASE_DIR
- from backend.core.models import Base
- from backend.core.database import engine
- # 创建数据库表
- Base.metadata.create_all(bind=engine)
- # 配置日志
- logging.basicConfig(
- level=logging.INFO,
- format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
- handlers=[
- logging.StreamHandler(),
- logging.FileHandler(Path(BASE_DIR) / "lightstar_api.log")
- ]
- )
- logger = logging.getLogger(__name__)
- # 创建FastAPI应用
- app = FastAPI(
- title="LightStar API",
- description="文献启明星 - 科研调研智能体API服务",
- version="0.1.0",
- docs_url=None, # 禁用Swagger UI
- redoc_url=None # 禁用ReDoc
- )
- # 配置CORS - 使用 config.py 中的设置
- app.add_middleware(
- CORSMiddleware,
- allow_origins=CORS_ORIGINS, # 使用配置文件中定义的域名列表
- allow_credentials=True,
- allow_methods=["*"],
- allow_headers=["*"],
- )
- # 注册路由
- app.include_router(research.router, prefix="/api")
- app.include_router(chat.router, prefix="/api")
- app.include_router(auth.router, prefix="/api")
- app.include_router(users.router, prefix="/api")
- app.include_router(chat_history.router, prefix="/api")
- app.include_router(research_history.router, prefix="/api")
- # 健康检查端点
- @app.get("/health")
- async def health_check():
- return {"status": "healthy", "version": "0.1.0"}
- # API信息端点
- @app.get("/")
- async def root():
- return {
- "name": "LightStar API",
- "description": "文献启明星 - 科研调研智能体API服务",
- "version": "0.1.0",
- "endpoints": [
- "/api/auth/token",
- "/api/auth/register",
- "/api/users/profile",
- "/api/chat-history",
- "/api/research-history",
- "/api/research/process",
- "/api/chat/send-message"
- ]
- }
- # 启动服务器
- if __name__ == "__main__":
- logger.info("Starting LightStar API server")
- # 使用模块的绝对路径确保可以从任何目录启动
- uvicorn.run("backend.main:app", host="0.0.0.0", port=8000, reload=True)
|