43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
from contextlib import asynccontextmanager
|
|
import os
|
|
import time
|
|
from multiprocessing import freeze_support
|
|
|
|
import uvicorn
|
|
from fastapi import FastAPI
|
|
|
|
from api.core import Exceptions, Gzip, Middleware
|
|
from api.core.Logging import configure_logging
|
|
from api.main import api_router
|
|
from config.main import config
|
|
from database.connect import close_connection_pools
|
|
|
|
configure_logging()
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(_: FastAPI):
|
|
print('dcalc is starting...')
|
|
print('Application starting...')
|
|
print('Application startup complete.')
|
|
try:
|
|
yield
|
|
finally:
|
|
await close_connection_pools()
|
|
|
|
|
|
application = FastAPI(docs_url='/doc', redoc_url=None, lifespan=lifespan)
|
|
application.add_exception_handler(Exception, Exceptions.all_exception_handler)
|
|
Middleware.register_cors(application)
|
|
Gzip.register_gzip_response(application)
|
|
application.include_router(api_router)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
freeze_support()
|
|
|
|
os.environ['TZ'] = 'Asia/Shanghai'
|
|
if os.name != 'nt': # Only call tzset on Unix-like systems, not on Windows
|
|
time.tzset()
|
|
uvicorn.run(app='main:application', host='0.0.0.0', port=config.PORT, workers=config.WORKERS, reload=False)
|