-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (55 loc) · 1.84 KB
/
main.py
File metadata and controls
68 lines (55 loc) · 1.84 KB
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
60
61
62
63
64
65
66
67
68
import sys
from contextlib import asynccontextmanager
from pathlib import Path
import uvicorn
from fastapi import FastAPI
from fastapi.responses import ORJSONResponse
from sqlalchemy.engine import make_url
from sqlalchemy.ext.asyncio import create_async_engine
from examples.api_for_sqlalchemy import config
from examples.api_for_sqlalchemy.extensions.sqlalchemy import Base
from examples.api_for_sqlalchemy.urls import add_routes
from examples.api_for_sqlalchemy.util import register_static_docs_routes
from fastapi_jsonapi import init
CURRENT_FILE = Path(__file__).resolve()
CURRENT_DIR = CURRENT_FILE.parent
PROJECT_DIR = CURRENT_DIR.parent.parent
sys.path.append(str(PROJECT_DIR))
async def sqlalchemy_init() -> None:
engine = create_async_engine(url=make_url(config.SQLA_URI), echo=config.SQLA_ECHO)
async with engine.begin() as conn:
# We don't want to drop tables on each app restart!
# await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)
@asynccontextmanager
async def lifespan(app: FastAPI):
# startup
await sqlalchemy_init()
yield
# shutdown
# await db_helper.dispose()
def create_app(
*,
create_custom_static_urls: bool = False,
) -> FastAPI:
app = FastAPI(
title="FastAPI and SQLAlchemy",
default_response_class=ORJSONResponse,
lifespan=lifespan,
docs_url=None if create_custom_static_urls else "/docs",
redoc_url=None if create_custom_static_urls else "/redoc",
)
if create_custom_static_urls:
register_static_docs_routes(app)
app.config = {"MAX_INCLUDE_DEPTH": 5}
add_routes(app)
init(app)
return app
if __name__ == "__main__":
uvicorn.run(
"asgi:app",
host="0.0.0.0", # noqa: S104
port=8082,
reload=True,
app_dir=str(CURRENT_DIR),
)