-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathserver.py
More file actions
63 lines (49 loc) · 1.41 KB
/
server.py
File metadata and controls
63 lines (49 loc) · 1.41 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
"""
Running the backend:
$ uvicorn formatter:app
Viewing the frontend:
http://127.0.0.1:8000/
"""
from typing import Optional
import yaml
from pydantic import BaseModel
from fastapi import FastAPI
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
TEST_DATA = {
"person": {
"name_latin": "Ivan",
"name": "Иван",
"age": 42,
}
}
class Parameters(BaseModel):
# Boolean flags:
allow_unicode: bool = False
canonical: bool = False
default_flow_style: bool = False
explicit_end: bool = False
explicit_start: bool = False
sort_keys: bool = True
# Valued parameters:
indent: Optional[int] = None
width: Optional[int] = None
default_style: Optional[str] = None
encoding: Optional[str] = None
line_break: Optional[str] = None
version: Optional[list] = None
app = FastAPI()
app.mount("/static", StaticFiles(directory="static/"), name="static")
@app.get("/", response_class=HTMLResponse)
async def index():
with open("views/index.html", "rb") as file:
return file.read()
@app.post("/")
async def serialize(parameters: Parameters):
try:
serialized = yaml.dump(TEST_DATA, **vars(parameters))
if isinstance(serialized, bytes):
return repr(serialized)
return serialized
except Exception as ex:
return JSONResponse(str(ex), status_code=400)