63 lines
2.4 KiB
Python
63 lines
2.4 KiB
Python
import logging, os
|
||
from fastapi import FastAPI
|
||
from fastapi.middleware.cors import CORSMiddleware
|
||
|
||
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(name)s] %(levelname)s: %(message)s')
|
||
logger = logging.getLogger('nac-gnacs')
|
||
|
||
app = FastAPI(
|
||
title='NAC GNACS 资产分类服务',
|
||
description='全球原生资产分类系统 - NAC公链基础设施层',
|
||
version='1.0.0',
|
||
docs_url='/api/docs',
|
||
redoc_url='/api/redoc'
|
||
)
|
||
|
||
app.add_middleware(CORSMiddleware, allow_origins=['*'], allow_credentials=True, allow_methods=['*'], allow_headers=['*'])
|
||
|
||
@app.on_event('startup')
|
||
async def startup():
|
||
from database import connect_db
|
||
await connect_db()
|
||
logger.info('GNACS服务启动完成')
|
||
|
||
@app.on_event('shutdown')
|
||
async def shutdown():
|
||
from database import close_db
|
||
await close_db()
|
||
|
||
# 健康检查(必须在静态文件挂载之前)
|
||
@app.get('/api/health')
|
||
async def health():
|
||
import database
|
||
try:
|
||
if database.db is not None:
|
||
await database.db.command('ping')
|
||
mongo_status = 'connected'
|
||
counts = {
|
||
'asset_classes': await database.asset_classes_col.count_documents({}),
|
||
'jurisdictions': await database.jurisdictions_col.count_documents({}),
|
||
'compliance_rules': await database.compliance_rules_col.count_documents({}),
|
||
'tax_treaties': await database.tax_treaties_col.count_documents({}),
|
||
}
|
||
else:
|
||
mongo_status = 'not_initialized'
|
||
counts = {}
|
||
except Exception as e:
|
||
mongo_status = f'error: {str(e)}'
|
||
counts = {}
|
||
return {'status': 'ok', 'service': 'GNACS', 'version': '1.0.0', 'mongo': mongo_status, 'database': 'gnacs_db', 'counts': counts}
|
||
|
||
# API路由注册(必须在静态文件挂载之前)
|
||
from routers import classify, jurisdiction, compliance, encode
|
||
app.include_router(classify.router, prefix='/api/gnacs/classify', tags=['资产分类'])
|
||
app.include_router(jurisdiction.router, prefix='/api/gnacs/jurisdiction', tags=['司法辖区'])
|
||
app.include_router(compliance.router, prefix='/api/gnacs/compliance', tags=['合规规则'])
|
||
app.include_router(encode.router, prefix='/api/gnacs/encode', tags=['GNACS编码'])
|
||
|
||
# 静态文件(管理界面,最后挂载)
|
||
static_dir = '/opt/nac/gnacs-service/static'
|
||
if os.path.exists(static_dir):
|
||
from fastapi.staticfiles import StaticFiles
|
||
app.mount('/', StaticFiles(directory=static_dir, html=True), name='static')
|