Files
geMoldInsight/src/main.py
T

258 lines
7.7 KiB
Python
Raw Normal View History

2026-02-11 22:40:35 +08:00
# main.py
import sys
import os
from pathlib import Path
# 添加项目根目录到Python路径
project_root = Path(__file__).parent.parent
src_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
sys.path.insert(0, str(src_root))
# 确保当前工作目录是项目根目录
os.chdir(project_root)
# 打印调试信息
print(f"项目根目录: {project_root}")
print(f"Python路径: {sys.path}")
print(f"当前工作目录: {os.getcwd()}")
# 测试导入配置模块
try:
2026-05-29 18:10:08 +08:00
from shared.config.settings import settings
2026-02-11 22:40:35 +08:00
print("[OK] 配置模块导入成功")
except ImportError as e:
print(f"[FAIL] 配置模块导入失败: {e}")
# 列出当前目录内容
print("当前目录内容:")
for item in os.listdir('.'):
print(f" - {item}")
# 列出config目录内容
if os.path.exists('config'):
print("config目录内容:")
for item in os.listdir('config'):
print(f" - {item}")
raise
2026-05-06 10:28:16 +08:00
from fastapi import FastAPI, Request
2026-02-11 22:40:35 +08:00
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
2026-05-06 10:28:16 +08:00
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
2026-02-11 22:40:35 +08:00
import asyncio
2026-05-06 10:28:16 +08:00
import time
2026-02-11 22:40:35 +08:00
2026-05-29 18:10:08 +08:00
from shared.services.auth_routes import router as auth_router
from inventory.api import inventory_router
from moldinsight.api.aluminum_price_routes import router as aluminum_price_router
from shared.utils.logger import setup_logging, get_logger
from shared.database.init_db import init_database
2026-02-11 22:40:35 +08:00
setup_logging()
2026-05-06 10:28:16 +08:00
logger = get_logger(__name__)
2026-02-11 22:40:35 +08:00
app = FastAPI(
2026-03-04 00:47:41 +08:00
title="Gemold - 模具制造管理系统",
description="模具制造行业综合管理平台,包含模具分析、进销存管理等功能",
version="4.0.0"
2026-02-11 22:40:35 +08:00
)
2026-05-06 10:28:16 +08:00
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.middleware("http")
async def log_requests(request: Request, call_next):
start_time = time.time()
response = await call_next(request)
duration = time.time() - start_time
status = response.status_code
if status >= 400:
logger.warning(
f"[HTTP] {request.method} {request.url.path} -> {status} "
f"({duration:.2f}s) "
2026-05-14 16:56:18 +08:00
f"client={request.client.host if request.client else 'unknown'}"
2026-05-06 10:28:16 +08:00
)
return response
2026-02-11 22:40:35 +08:00
# 启动时初始化数据库和RustFS
@app.on_event("startup")
async def startup_event():
"""应用启动时初始化数据库和RustFS"""
# 初始化数据库
2026-03-25 23:59:29 +08:00
success = await init_database(keep_connected=True)
2026-02-11 22:40:35 +08:00
if success:
print("[OK] 数据库初始化成功")
else:
print("[FAIL] 数据库初始化失败,服务将继续运行但数据库功能不可用")
# 初始化RustFS连接
try:
2026-05-29 18:10:08 +08:00
from moldinsight.storage.rustfs_storage import rustfs_manager
from shared.config.settings import settings
2026-02-11 22:40:35 +08:00
await rustfs_manager.connect(
endpoint=settings.RUSTFS_ENDPOINT,
access_key=settings.RUSTFS_ACCESS_KEY,
secret_key=settings.RUSTFS_SECRET_KEY,
timeout=settings.RUSTFS_TIMEOUT
)
print("[OK] RustFS连接成功")
except Exception as e:
print(f"[FAIL] RustFS连接失败: {e}")
print("[WARN] 文件上传功能将不可用,但其他功能正常")
2026-04-13 09:43:24 +08:00
# 初始化Redis连接
try:
2026-05-29 18:10:08 +08:00
from shared.services.redis_task_manager import redis_task_manager
2026-04-13 09:43:24 +08:00
await redis_task_manager.connect()
if redis_task_manager.is_connected:
print("[OK] Redis连接成功")
else:
print("[WARN] Redis连接失败,任务状态将使用内存回退")
except Exception as e:
print(f"[WARN] Redis初始化异常: {e},任务状态将使用内存回退")
@app.on_event("shutdown")
async def shutdown_event():
"""应用关闭时清理资源"""
try:
2026-05-29 18:10:08 +08:00
from shared.services.redis_task_manager import redis_task_manager
2026-04-13 09:43:24 +08:00
await redis_task_manager.disconnect()
print("[OK] Redis连接已断开")
except Exception as e:
print(f"[WARN] Redis断开异常: {e}")
2026-02-11 22:40:35 +08:00
# 创建必要目录
UPLOAD_DIR = Path("uploads")
UPLOAD_DIR.mkdir(exist_ok=True)
TEMPLATES_DIR = Path("templates")
TEMPLATES_DIR.mkdir(exist_ok=True)
STATIC_DIR = Path("static")
STATIC_DIR.mkdir(exist_ok=True)
HTML_OUTPUT_DIR = Path("html_output")
HTML_OUTPUT_DIR.mkdir(exist_ok=True)
# 挂载静态文件
import os
2026-02-16 01:13:13 +08:00
# 使用当前工作目录下的static文件夹
static_dir = os.path.join(os.getcwd(), "static")
2026-02-11 22:40:35 +08:00
app.mount("/static", StaticFiles(directory=static_dir), name="static")
2026-03-07 00:56:51 +08:00
# 挂载HTML输出目录
html_output_dir = os.path.join(os.getcwd(), "html_output")
app.mount("/html", StaticFiles(directory=html_output_dir), name="html")
2026-03-04 00:47:41 +08:00
app.include_router(auth_router)
app.include_router(inventory_router)
2026-05-06 17:20:54 +08:00
app.include_router(aluminum_price_router, prefix="/api")
2026-03-25 23:59:29 +08:00
try:
2026-05-29 18:10:08 +08:00
from moldinsight.api import router as moldinsight_router
2026-03-25 23:59:29 +08:00
app.include_router(moldinsight_router, prefix="/api")
2026-05-29 18:10:08 +08:00
except Exception as e:
print(f"[WARN] MoldInsight 路由未加载: {e}")
2026-02-11 22:40:35 +08:00
@app.get("/health")
2026-02-16 01:32:13 +08:00
@app.post("/health")
2026-02-11 22:40:35 +08:00
async def health():
2026-05-29 18:10:08 +08:00
from shared.database.database import db_manager
2026-03-25 23:59:29 +08:00
from sqlalchemy import text
db_ok = False
db_error = None
try:
if not db_manager.is_connected:
await db_manager.connect()
async with db_manager.engine.begin() as conn:
await conn.execute(text("SELECT 1"))
db_ok = True
except Exception as e:
db_ok = False
db_error = str(e)
2026-02-11 22:40:35 +08:00
return {
"status": "healthy",
2026-03-04 00:47:41 +08:00
"service": "gemold",
"version": "4.0.0",
2026-03-25 23:59:29 +08:00
"database_connected": db_ok,
"database_error": db_error
2026-02-11 22:40:35 +08:00
}
2026-03-04 00:47:41 +08:00
@app.get("/")
async def root():
from fastapi.responses import FileResponse
return FileResponse(os.path.join(os.getcwd(), "static", "index.html"))
@app.get("/moldinsight")
async def moldinsight():
from fastapi.responses import FileResponse
return FileResponse(os.path.join(os.getcwd(), "static", "index.html"))
@app.get("/inventory")
async def inventory():
from fastapi.responses import FileResponse
return FileResponse(os.path.join(os.getcwd(), "static", "index.html"))
2026-03-25 23:59:29 +08:00
@app.get("/login")
async def login():
from fastapi.responses import FileResponse
return FileResponse(os.path.join(os.getcwd(), "static", "index.html"))
2026-03-04 00:47:41 +08:00
@app.get("/users")
async def users():
from fastapi.responses import FileResponse
return FileResponse(os.path.join(os.getcwd(), "static", "index.html"))
2026-05-29 17:30:26 +08:00
@app.get("/moldinsight/result/{task_id:path}")
async def moldinsight_result(task_id: str):
from fastapi.responses import FileResponse
return FileResponse(os.path.join(os.getcwd(), "static", "index.html"))
@app.get("/_design-system")
async def design_system():
from fastapi.responses import FileResponse
return FileResponse(os.path.join(os.getcwd(), "static", "index.html"))
@app.get("/_release")
async def release():
from fastapi.responses import FileResponse
return FileResponse(os.path.join(os.getcwd(), "static", "index.html"))
2026-02-11 22:40:35 +08:00
if __name__ == "__main__":
import uvicorn
2026-05-29 18:10:08 +08:00
from shared.config.settings import settings
2026-03-25 23:59:29 +08:00
reload_enabled = os.getenv("UVICORN_RELOAD", "0").lower() in {"1", "true", "yes", "on"}
2026-02-11 22:40:35 +08:00
2026-03-04 00:47:41 +08:00
print("启动 Gemold 模具制造管理系统 v4.0...")
print(f"访问 http://localhost:{settings.PORT}")
2026-03-25 23:59:29 +08:00
print(f"Python可执行文件: {sys.executable}")
print(f"进程ID: {os.getpid()}")
print(f"热重载: {reload_enabled}")
2026-03-04 00:47:41 +08:00
print("功能模块:")
print(" - 首页仪表盘")
print(" - 用户管理")
print(" - MoldInsight 模具分析")
print(" - 进销存管理")
2026-02-11 22:40:35 +08:00
uvicorn.run(
"main:app",
2026-02-13 00:29:12 +08:00
host=settings.HOST,
port=settings.PORT,
2026-03-25 23:59:29 +08:00
reload=reload_enabled
)