1564c8766d
- Modified load_dotenv() calls in multiple files to include override=True, ensuring that .env variables can replace existing environment variables. - Updated ota_storage.py to use default values for OTA upload directories, improving clarity and maintainability. - Enhanced get_ota_upload_dir() function to read environment variables dynamically, ensuring accurate directory paths based on the current environment.
82 lines
2.1 KiB
Python
82 lines
2.1 KiB
Python
from fastapi import FastAPI, Depends
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
import os
|
|
import logging
|
|
from dotenv import load_dotenv
|
|
|
|
from database import engine, Base
|
|
from routes import brands_router, models_router, ota_router
|
|
from routes.auth import router as auth_router
|
|
from security import get_current_user
|
|
|
|
# Load environment variables
|
|
load_dotenv(override=True)
|
|
|
|
# Configure logging
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
handlers=[
|
|
logging.StreamHandler(), # 输出到控制台
|
|
logging.FileHandler('logs/app.log', encoding='utf-8') # 输出到文件
|
|
]
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Create database tables (only if tables don't exist)
|
|
try:
|
|
logger.info("Creating database tables...")
|
|
Base.metadata.create_all(bind=engine)
|
|
logger.info("Database tables created successfully")
|
|
except Exception as e:
|
|
logger.error(f"Warning: Could not create tables: {e}")
|
|
logger.error("Continuing without table creation...")
|
|
|
|
# Create FastAPI app
|
|
app = FastAPI(
|
|
title=os.getenv("APP_NAME", "Audio Dashboard API"),
|
|
description="耳机品牌与型号管理平台后端 API",
|
|
version="1.0.0"
|
|
)
|
|
|
|
# Configure CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # 生产环境应该限制具体域名
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Include routers(业务接口需登录;登录接口除外)
|
|
app.include_router(auth_router)
|
|
app.include_router(brands_router, dependencies=[Depends(get_current_user)])
|
|
app.include_router(models_router, dependencies=[Depends(get_current_user)])
|
|
app.include_router(ota_router, dependencies=[Depends(get_current_user)])
|
|
|
|
|
|
@app.get("/")
|
|
def root():
|
|
"""根路径"""
|
|
return {
|
|
"message": "欢迎使用 Audio Dashboard API",
|
|
"docs": "/docs",
|
|
"redoc": "/redoc"
|
|
}
|
|
|
|
|
|
@app.get("/health")
|
|
def health_check():
|
|
"""健康检查1"""
|
|
return {"status": "healthy"}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(
|
|
"main:app",
|
|
host="0.0.0.0",
|
|
port=8083,
|
|
reload=False
|
|
)
|