-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (48 loc) · 1.5 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import asyncio
import logging
from fastapi import FastAPI
from sqlmodel import SQLModel
from app import settings, auth_settings
from app.core.database import async_engine
from app.router.api_v1.endpoints import api_router
from contextlib import asynccontextmanager
from starlette.middleware.cors import CORSMiddleware
# Import models here
from app.users.models import User
from app.blogs.models import Blog
logging.basicConfig(level="DEBUG")
async def create_tables():
"""
Creates tables if not exists
"""
# Import models here
async with async_engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)
@asynccontextmanager
async def lifespan(app: FastAPI):
await create_tables()
yield
origins = [
"http://localhost:3000",
]
app = FastAPI(title=settings.project_name, openapi_url=f"{settings.api_v1_prefix}/openapi.json", debug=settings.debug,
version=settings.version, lifespan=lifespan)
app.include_router(api_router, prefix=settings.api_v1_prefix)
@app.get("/", tags=["status"])
async def health_check():
return {
"name": settings.project_name,
"version": settings.version,
"description": settings.description
}
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
if __name__ == '__main__':
import uvicorn
asyncio.run(create_tables())
uvicorn.run(app, host='0.0.0.0', port=8000, reload=True, log_level="debug")