-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathapp.py
More file actions
58 lines (40 loc) · 1.17 KB
/
app.py
File metadata and controls
58 lines (40 loc) · 1.17 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
import os
from fastapi import FastAPI
from gino_starlette import Gino
from pydantic import BaseModel
app = FastAPI()
db = Gino(
app,
host=os.getenv("DB_HOST", "localhost"),
port=os.getenv("DB_PORT", 5432),
user=os.getenv("DB_USER", "postgres"),
password=os.getenv("DB_PASS", ""),
database=os.getenv("DB_NAME", "postgres"),
)
class User(db.Model):
__tablename__ = "simple_fastapi_demo_users"
id = db.Column(db.BigInteger(), primary_key=True)
nickname = db.Column(db.Unicode(), default="unnamed")
class UserModel(BaseModel):
name: str
@app.get("/")
async def index():
return {"message": "Hello, world!"}
@app.get("/users/{uid}")
async def get_user(uid: int):
q = User.query.where(User.id == uid)
return (await q.gino.first_or_404()).to_dict()
@app.post("/users")
async def add_user(user: UserModel):
u = await User.create(nickname=user.name)
return u.to_dict()
@app.on_event("startup")
async def create():
await db.gino.create_all()
if __name__ == "__main__":
import uvicorn
uvicorn.run(
app,
host=os.getenv("APP_HOST", "127.0.0.1"),
port=int(os.getenv("APP_PORT", "5000")),
)