init
This commit is contained in:
+134
@@ -0,0 +1,134 @@
|
||||
# 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:
|
||||
from config.settings import settings
|
||||
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
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
import asyncio
|
||||
|
||||
from api.routes import router
|
||||
from utils.logger import setup_logging
|
||||
from database.init_db import init_database
|
||||
|
||||
# 设置日志
|
||||
setup_logging()
|
||||
|
||||
# 创建FastAPI应用
|
||||
app = FastAPI(
|
||||
title="模具几何分析服务",
|
||||
description="基于PythonOCC的STP文件几何分析和模具设计建议服务",
|
||||
version="3.0.0"
|
||||
)
|
||||
|
||||
# 启动时初始化数据库和RustFS
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
"""应用启动时初始化数据库和RustFS"""
|
||||
# 初始化数据库
|
||||
success = await init_database()
|
||||
if success:
|
||||
print("[OK] 数据库初始化成功")
|
||||
else:
|
||||
print("[FAIL] 数据库初始化失败,服务将继续运行但数据库功能不可用")
|
||||
|
||||
# 初始化RustFS连接
|
||||
try:
|
||||
from storage.rustfs_storage import rustfs_manager
|
||||
from config.settings import settings
|
||||
|
||||
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] 文件上传功能将不可用,但其他功能正常")
|
||||
|
||||
# 创建必要目录
|
||||
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
|
||||
static_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "static")
|
||||
app.mount("/static", StaticFiles(directory=static_dir), name="static")
|
||||
|
||||
# 注册路由
|
||||
app.include_router(router)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
async def health():
|
||||
from database.database import db_manager
|
||||
return {
|
||||
"status": "healthy",
|
||||
"service": "mold-geometry-analysis",
|
||||
"database_connected": db_manager.is_connected
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
import os
|
||||
|
||||
# 直接从环境变量获取端口,避免配置导入问题
|
||||
host = os.getenv('HOST', '0.0.0.0')
|
||||
port = int(os.getenv('PORT', '8000'))
|
||||
|
||||
print("启动模具几何分析服务 v3.0...")
|
||||
print(f"访问 http://localhost:{port} 使用网页界面")
|
||||
print("新增功能:")
|
||||
print(" - STP文件解析为JSON数据")
|
||||
print(" - 数据存储到PostgreSQL数据库")
|
||||
print(" - 自动生成3D可视化HTML页面")
|
||||
print(" - 源文件、JSON数据、HTML文件统一管理")
|
||||
print(f"调试接口: http://localhost:{port}/debug/tasks")
|
||||
|
||||
uvicorn.run(
|
||||
"main:app",
|
||||
host=host,
|
||||
port=port,
|
||||
reload=True
|
||||
)
|
||||
Reference in New Issue
Block a user