Files
dashboard/backend/main.py
T
yangy abd44fe467 Refactor Docker setup and remove unused files
- Updated docker-compose.yml to rename backend and frontend container names for clarity.
- Added volume mappings for frontend and backend services to persist data.
- Removed obsolete files related to frequency response extraction, including Python scripts and output data files.
- Adjusted backend port configuration in main.py to align with new service architecture.
- Enhanced logging for file upload paths in models.py for better traceability.
2026-03-20 09:31:24 +08:00

78 lines
1.8 KiB
Python

from fastapi import FastAPI
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
# Load environment variables
load_dotenv()
# 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(brands_router)
app.include_router(models_router)
@app.get("/")
def root():
"""根路径"""
return {
"message": "欢迎使用 Audio Dashboard API",
"docs": "/docs",
"redoc": "/redoc"
}
@app.get("/health")
def health_check():
"""健康检查"""
return {"status": "healthy"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"main:app",
host="0.0.0.0",
port=8083,
reload=False
)