-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
84 lines (67 loc) · 2.16 KB
/
app.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import json
import sentry_sdk
import yaml
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from fastapi.middleware.cors import CORSMiddleware
from prometheus_fastapi_instrumentator import Instrumentator
from config import sentry_dsn
from src.dummy_data.dummy_chair import DummyChair
from src.dummy_data.dummy_train import DummyTrain
from src.models.train import Train
app = FastAPI()
# Prometheus
Instrumentator().instrument(app).expose(app)
# CORS
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Sentry
sentry_sdk.init(
dsn=sentry_dsn,
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production.
traces_sample_rate=1.0,
# Set profiles_sample_rate to 1.0 to profile 100%
# of sampled transactions.
# We recommend adjusting this value in production.
profiles_sample_rate=1.0,
)
@app.get("/")
def read_root() -> None:
return {"Hello": "World"}
@app.get("/dummy_train/random")
def get_dummy_train_random() -> str:
dummy_train = DummyTrain()
dummy_train = jsonable_encoder(dummy_train)
dummy_train = json.dumps(dummy_train["random"])
return dummy_train
@app.get("/dummy_train/fixed")
def get_dummy_train_fixed() -> str:
with open("./tests/dummy_data.yaml") as file:
dummy_train_dict = yaml.load(file, Loader=yaml.FullLoader)
dummy_train = Train(**dummy_train_dict)
dummy_train = jsonable_encoder(dummy_train)
dummy_train = json.dumps(dummy_train)
return dummy_train
@app.get("/dummy_chair/one/random")
def get_dummy_chair_random() -> str:
dummy_chair = DummyChair().one
dummy_chair = jsonable_encoder(dummy_chair)
dummy_chair = json.dumps(dummy_chair[0])
return dummy_chair
@app.get("/dummy_chair/two/random")
def get_dummy_chairs_random() -> str:
dummy_chair = DummyChair().two
dummy_chair = jsonable_encoder(dummy_chair)
dummy_chair = json.dumps(dummy_chair)
return dummy_chair
@app.get("/sample_exception")
def sample_exception() -> None:
1 / 0