first commit
This commit is contained in:
30
server/app.py
Normal file
30
server/app.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from fastapi import FastAPI
|
||||
from server.servers import servers_lifespan
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
async def lifespan(app: FastAPI):
|
||||
async with servers_lifespan(app):
|
||||
yield
|
||||
|
||||
app = FastAPI(
|
||||
title="Leaves API",
|
||||
version="1.0",
|
||||
description="Leaves API",
|
||||
lifespan=lifespan,
|
||||
contact={
|
||||
"name": "Seayu",
|
||||
"email": "yhyuhai13@163.com",
|
||||
},
|
||||
)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(app, host="0.0.0.0", port=12345)
|
||||
30
server/servers.py
Normal file
30
server/servers.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import List
|
||||
|
||||
from fastapi import FastAPI
|
||||
from servers.base import BaseService
|
||||
from servers.database.server import database_service
|
||||
from servers.redis.server import redis_service
|
||||
|
||||
servers: List[BaseService] = [
|
||||
database_service,
|
||||
redis_service,
|
||||
]
|
||||
|
||||
@asynccontextmanager
|
||||
async def servers_lifespan(app: FastAPI):
|
||||
global servers
|
||||
for server in servers:
|
||||
await server.initialize()
|
||||
|
||||
@app.get("/health")
|
||||
async def health_check():
|
||||
health_status = {}
|
||||
for server in servers:
|
||||
health_status[server.service_name] = await server.check_health()
|
||||
return health_status
|
||||
|
||||
yield
|
||||
for server in servers:
|
||||
await server.shutdown()
|
||||
|
||||
Reference in New Issue
Block a user