feat: initial release - Hunzi agent framework v0.1.0
This commit is contained in:
+68
@@ -0,0 +1,68 @@
|
||||
"""Hunzi 主应用入口"""
|
||||
from contextlib import asynccontextmanager
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from .config import load_config, set_config
|
||||
from .database import init_db, get_session
|
||||
from .models import Base
|
||||
from .api import router as api_router
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
# Startup
|
||||
config = load_config()
|
||||
set_config(config)
|
||||
|
||||
# Setup logging
|
||||
logging.basicConfig(
|
||||
level=config.log_level,
|
||||
format=config.log_format,
|
||||
)
|
||||
logging.info("Hunzi 启动中...")
|
||||
|
||||
# Init database
|
||||
init_db()
|
||||
logging.info("Hunzi 启动完成")
|
||||
|
||||
yield
|
||||
|
||||
# Shutdown
|
||||
logging.info("Hunzi 关闭中...")
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="Hunzi 智能体框架",
|
||||
version="0.1.0",
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
# CORS
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# API routes
|
||||
app.include_router(api_router, prefix="/api")
|
||||
|
||||
# Static files
|
||||
web_dir = Path(__file__).parent.parent / "web"
|
||||
if web_dir.exists():
|
||||
app.mount("/static", StaticFiles(directory=str(web_dir)), name="static")
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def serve_frontend():
|
||||
index_path = web_dir / "index.html"
|
||||
if index_path.exists():
|
||||
return FileResponse(str(index_path))
|
||||
return {"message": "Hunzi 智能体框架", "status": "running", "docs": "/docs"}
|
||||
Reference in New Issue
Block a user