f1be24ab83
- Add FastAPI app with paper browsing UI and REST API - Add crawler service and database models - Add scripts for DB init and manual crawl - Add docs (api-and-ui, data-model, services) - Add requirements and project config
60 lines
1.4 KiB
Python
60 lines
1.4 KiB
Python
"""FastAPI 应用入口。"""
|
|
|
|
import logging
|
|
import os
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from app.config import settings
|
|
from app.database import engine
|
|
from app.models import init_db
|
|
from app.routes.pages import router as pages_router
|
|
|
|
logging.basicConfig(
|
|
level=logging.DEBUG if settings.APP_DEBUG else logging.INFO,
|
|
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
app = FastAPI(
|
|
title="HF Daily Papers",
|
|
description="HuggingFace Daily Papers — 中文论文导览站",
|
|
version="0.1.0",
|
|
)
|
|
|
|
# 确保数据目录存在
|
|
os.makedirs(settings.db_path.parent, exist_ok=True)
|
|
|
|
# 初始化数据库
|
|
init_db(engine)
|
|
logger.info("Database initialized at %s", settings.db_path)
|
|
|
|
# 安全警告
|
|
if settings.ADMIN_TOKEN == "change-me":
|
|
logger.warning("⚠️ ADMIN_TOKEN is the default value 'change-me'. Please change it in .env!")
|
|
|
|
# 静态文件
|
|
app.mount("/static", StaticFiles(directory="app/static"), name="static")
|
|
|
|
# 路由
|
|
app.include_router(pages_router)
|
|
|
|
return app
|
|
|
|
|
|
app = create_app()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
|
|
uvicorn.run(
|
|
"app.main:app",
|
|
host=settings.APP_HOST,
|
|
port=settings.APP_PORT,
|
|
reload=settings.APP_DEBUG,
|
|
)
|