feat: scaffold modular monolith backend architecture
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
from pydantic_settings import BaseSettings
|
||||
from typing import List
|
||||
|
||||
class Settings(BaseSettings):
|
||||
PROJECT_NAME: str = "WTFX Backend API"
|
||||
VERSION: str = "2.0.0"
|
||||
DEBUG: bool = False
|
||||
|
||||
# 环境:development, staging, production
|
||||
ENVIRONMENT: str = "development"
|
||||
|
||||
# 数据库配置 (PostgreSQL)
|
||||
DATABASE_URL: str = "postgresql+asyncpg://postgres:postgres@localhost:5432/wtfx"
|
||||
|
||||
# Redis 配置
|
||||
REDIS_URL: str = "redis://localhost:6379/0"
|
||||
|
||||
# JWT 鉴权
|
||||
JWT_SECRET: str = "wtfx_secret_jwt_key_change_in_production"
|
||||
JWT_ALGORITHM: str = "HS256"
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 7 # 7 days
|
||||
|
||||
# 跨域设置
|
||||
CORS_ORIGINS: List[str] = [
|
||||
"http://localhost:3000",
|
||||
"http://localhost:3001",
|
||||
"https://wtfx.app",
|
||||
"https://test.wtfx.app",
|
||||
"https://admin.wtfx.app",
|
||||
"https://admin.test.wtfx.app"
|
||||
]
|
||||
|
||||
# 链上 RPC 配置 (Robinhood Chain)
|
||||
ROBINHOOD_TESTNET_RPC: str = "https://rpc.testnet.chain.robinhood.com"
|
||||
ROBINHOOD_MAINNET_RPC: str = "https://rpc.chain.robinhood.com"
|
||||
|
||||
# 控制器合约地址 (由部署脚本生成)
|
||||
CONTROLLER_ADDRESS: str = "0x0000000000000000000000000000000000000000"
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
case_sensitive = True
|
||||
|
||||
settings = Settings()
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from contextlib import asynccontextmanager
|
||||
from app.config import settings
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
# 启动时:初始化 DB 连接池、Redis 事件总线与外部适配器
|
||||
print(f"Starting {settings.PROJECT_NAME} in [{settings.ENVIRONMENT}] mode...")
|
||||
yield
|
||||
# 关闭时:优雅断开连接
|
||||
print(f"Shutting down {settings.PROJECT_NAME}...")
|
||||
|
||||
app = FastAPI(
|
||||
title=settings.PROJECT_NAME,
|
||||
version=settings.VERSION,
|
||||
lifespan=lifespan,
|
||||
docs_url="/docs" if settings.ENVIRONMENT != "production" else None,
|
||||
redoc_url="/redoc" if settings.ENVIRONMENT != "production" else None
|
||||
)
|
||||
|
||||
# CORS 配置
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=settings.CORS_ORIGINS,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
@app.get("/health", tags=["System"])
|
||||
async def health_check():
|
||||
"""生产探活与健康检查端点 (Blue-Green Probe)"""
|
||||
return {
|
||||
"status": "ok",
|
||||
"service": "wtfx-backend",
|
||||
"version": settings.VERSION,
|
||||
"environment": settings.ENVIRONMENT
|
||||
}
|
||||
Reference in New Issue
Block a user