Initial commit: yu-zhi-ran platform with automation integration
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import logging
|
||||
from fastapi import FastAPI, Depends, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
import os
|
||||
from .database import engine, get_db, init_db
|
||||
from .models import Base
|
||||
from .api import topics, system, articles, publisher
|
||||
from .initial_data import import_topics_from_json
|
||||
|
||||
app = FastAPI(title="宇之然内容创作平台", version="0.1.0")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# CORS - 生产环境应限制 origins
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"], # TODO: 生产环境改为具体域名
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# 初始化数据库
|
||||
Base.metadata.create_all(bind=engine)
|
||||
init_db()
|
||||
import_topics_from_json() # 首次自动导入
|
||||
|
||||
# 注册路由
|
||||
app.include_router(topics.router)
|
||||
app.include_router(system.router)
|
||||
app.include_router(articles.router)
|
||||
app.include_router(publisher.router)
|
||||
|
||||
# 挂载前端静态文件
|
||||
FRONTEND_DIR = Path(__file__).parent.parent.parent / "frontend"
|
||||
STATIC_DIR = FRONTEND_DIR / "static"
|
||||
|
||||
# 检查前端文件是否存在,若不存在下载Element Plus等依赖
|
||||
if not FRONTEND_DIR.exists():
|
||||
FRONTEND_DIR.mkdir(parents=True, exist_ok=True)
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.warning(f"Frontend dir not found: {FRONTEND_DIR}, will serve API only")
|
||||
|
||||
# 默认静态文件服务(若前端存在)
|
||||
if FRONTEND_DIR.exists() and (FRONTEND_DIR / "index.html").exists():
|
||||
app.mount("/", StaticFiles(directory=str(FRONTEND_DIR), html=True), name="frontend")
|
||||
if STATIC_DIR.exists():
|
||||
app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
|
||||
logging.getLogger(__name__).info(f"Frontend mounted at / from {FRONTEND_DIR}")
|
||||
else:
|
||||
@app.get("/")
|
||||
def root():
|
||||
return {
|
||||
"service": "宇之然内容创作平台 API",
|
||||
"version": "0.1.0",
|
||||
"docs": "/docs",
|
||||
"frontend_missing": str(FRONTEND_DIR)
|
||||
}
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)
|
||||
Reference in New Issue
Block a user