-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·97 lines (80 loc) · 2.96 KB
/
app.py
File metadata and controls
executable file
·97 lines (80 loc) · 2.96 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
from decouple import config, UndefinedValueError
from fastapi import FastAPI, Query, Request, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from random import randint
from sqlalchemy import bindparam, create_engine, text, Column, Integer, String
from sqlalchemy.orm import declarative_base, sessionmaker, Session
from typing import Optional
# postgres schema
Base = declarative_base()
class Quote(Base):
__tablename__ = 'quotes'
id = Column(Integer, primary_key=True)
quote = Column(String, nullable=False)
author = Column(String, nullable=False)
# postgres connection
try:
db_uri = config("DATABASE_URL")
except UndefinedValueError:
db_name = config("POSTGRES_DB")
db_host = config("POSTGRES_HOST")
db_user = config("POSTGRES_USER")
db_pass = config("POSTGRES_PASSWORD")
db_port = config("POSTGRES_PORT",
default=5432,
cast=int)
db_uri = f"postgresql://{db_user}:{db_pass}@{db_host}:{db_port}/{db_name}"
uri = db_uri
conn = create_engine(uri, echo=False)
sesh = sessionmaker(bind=conn)
# fastapi instantiation
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
@app.get('/healthz')
def healthz() -> JSONResponse:
"""Health check endpoint"""
return JSONResponse(content={"status": "ok"})
@app.get('/', response_class=HTMLResponse)
def home(request: Request):
"""Home page"""
return templates.TemplateResponse("index.html", {"request": request})
# TODO: fix optional name parameter
@app.get('/hello/{name}')
def hello(name: Optional[str] = None) -> JSONResponse:
"""Hello, World!"""
if name:
message = f"Hello, {name}!"
else:
message = "Hello, World!"
return JSONResponse(content={"hello": message})
# TODO: improve sql performance
@app.get('/all')
def get_quotes(request: Request) -> JSONResponse:
"""Get all quotes"""
with Session(conn) as session:
quotes = session.query(Quote).all()
result = [{"id": quote.id, "quote": quote.quote, "author": quote.author} for quote in quotes]
return JSONResponse(content={"quotes": result})
@app.get('/quotes/{limit}')
def get_quotes_by_limit(request: Request, limit: int) -> JSONResponse:
"""Get quotes by limit"""
with Session(conn) as session:
count = session.query(Quote).count()
if count > 0:
random_index = randint(0, count - 1)
quote = session.query(Quote).offset(random_index).limit(limit).first()
result = [{"id": quote.id, "quote": quote.quote, "author": quote.author}]
else:
result = []
return JSONResponse(content={"quotes": result})