forked from mts-ai/FastAPI-JSONAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (48 loc) · 1.47 KB
/
main.py
File metadata and controls
61 lines (48 loc) · 1.47 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
"""
Main module for w_mount service.
In module placed db initialization functions, app factory.
"""
import sys
from pathlib import Path
import uvicorn
from fastapi import FastAPI
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 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)
def create_app() -> FastAPI:
"""
Create app factory.
:return: app
"""
app = FastAPI(
title="FastAPI and SQLAlchemy",
debug=True,
openapi_url="/openapi.json",
docs_url="/docs",
)
app.config = {"MAX_INCLUDE_DEPTH": 5}
add_routes(app)
app.on_event("startup")(sqlalchemy_init)
init(app)
return app
if __name__ == "__main__":
uvicorn.run(
"asgi:app",
host="0.0.0.0",
port=8082,
reload=True,
app_dir=str(CURRENT_DIR),
)