-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
35 lines (26 loc) · 900 Bytes
/
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
from fastapi import FastAPI, Request
from apitoolbox import crud, db_registry
from apitoolbox.middleware import SessionMiddleware
from apitoolbox.models import BASE, Session, User
DATABASE_URL = "sqlite:///sqlite.db?check_same_thread=False"
# Define our model
class MyUser(User):
pass
# Instantiate the application
app = FastAPI()
app.add_middleware(SessionMiddleware, bind=DATABASE_URL)
# Create all tables
bind = db_registry.get_or_create(DATABASE_URL)
BASE.metadata.create_all(bind=bind)
# Load some data
# session = Session()
# for name in ["alice", "bob", "charlie", "david"]:
# user = MyUser.get_by_username(session, name)
# if user is None:
# user = MyUser(username=name)
# session.add(user)
# session.commit()
# Add an endpoint
@app.get("/users")
async def list_users(request: Request):
return await crud.list_instances(MyUser, request.state.session)