-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
125 lines (109 loc) · 3.55 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import json
from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import models
from database import SessionLocal, engine
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
# Create tables if not exist
models.Base.metadata.create_all(bind=engine)
db = SessionLocal()
@app.middleware("http")
async def get_request_path(request: Request, call_next):
"""
Middleware que intercepta toda request que chega, verifica no banco se já
existe um endpoint mocado para ela, e retorna os dados mocados caso exista.
Caso não exista, adiciona uma entrada no banco, para que seja possível
preencher o restante dos dados na tela de admin.
"""
exceptions_paths = [
"/admin",
"/admin/",
"/save",
"/static/css/custom.css",
"/static/css/bulma.min.css",
"/favicon.ico",
]
if request.url.path in exceptions_paths:
response = await call_next(request)
return response
else:
path = (
db.query(models.Path)
.filter(
models.Path.endpoint == request.url.path,
models.Path.method == request.method,
)
.first()
)
if path:
# Retorna as informações mocadas
response = JSONResponse(
path.return_body, headers=path.return_header
)
else:
# Registrar o novo endpoint
path_to_create = models.Path(
endpoint=request.url.path,
method=request.method,
return_body={},
return_header={},
)
db.add(path_to_create)
db.commit()
response = await call_next(request)
db.close()
return response
@app.get("/admin", response_class=HTMLResponse)
async def load_html(request: Request):
paths = db.query(models.Path).order_by(models.Path.id.asc())
payload = []
db.close()
for path in paths:
payload.append(
{
"id": path.id,
"method": path.method,
"endpoint": path.endpoint,
"return_body": path.return_body,
"return_header": path.return_header,
}
)
return templates.TemplateResponse(
"admin.html", {"request": request, "payload": payload}
)
@app.post("/admin", response_class=HTMLResponse)
async def load_html(
request: Request,
method: str = Form(),
endpoint: str = Form(),
# return_header: str = Form(),
return_body: str = Form(),
):
# Update data
db.query(models.Path).filter(models.Path.endpoint == endpoint).update({
"method": method,
# "return_header": json.loads(return_header.replace("\'", "\"")),
"return_body": json.loads(return_body.replace("\'", "\"")),
})
db.commit()
# Load all data and return
paths = db.query(models.Path).order_by(models.Path.id.asc())
payload = []
db.close()
for path in paths:
payload.append(
{
"id": path.id,
"method": path.method,
"endpoint": path.endpoint,
"return_body": path.return_body,
"return_header": path.return_header,
}
)
return templates.TemplateResponse(
"admin.html", {"request": request, "payload": payload}
)