forked from mts-ai/FastAPI-JSONAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
60 lines (47 loc) · 1.26 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
60
"""
Main module for w_mount service.
In module placed db initialization functions, app factory.
"""
import sys
from pathlib import Path
CURRENT_FILE = Path(__file__).resolve()
CURRENT_DIR = CURRENT_FILE.parent
PROJECT_DIR = CURRENT_DIR.parent.parent
sys.path.append(str(PROJECT_DIR))
import uvicorn
from fastapi import FastAPI
from tortoise import Tortoise
from examples.api_for_tortoise_orm.urls import add_routes
async def tortoise_init() -> None:
# Here we create a SQLite DB using file "db.sqlite3"
# also specify the app name of "models"
# which contain models from "app.models"
await Tortoise.init(
db_url="sqlite://db.sqlite3",
modules={"models": ["models.tortoise"]},
)
# Generate the schema
await Tortoise.generate_schemas()
def create_app() -> FastAPI:
"""
Create app factory.
:return: app
"""
app = FastAPI(
title="FastAPI and Tortoise ORM",
debug=True,
openapi_url="/openapi.json",
docs_url="/docs",
)
add_routes(app)
app.on_event("startup")(tortoise_init)
return app
if __name__ == "__main__":
uvicorn.run(
"asgi:app",
host="0.0.0.0",
port=8080,
debug=True,
reload=True,
app_dir=str(CURRENT_DIR),
)