-
Notifications
You must be signed in to change notification settings - Fork 2
/
app_fastapi.py
68 lines (50 loc) · 2.19 KB
/
app_fastapi.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
from fastapi import FastAPI, Header, Cookie, Form, Request, requests, Body, Response, HTTPException
from fastapi.responses import HTMLResponse
from typing import List, Callable, Optional
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.exceptions import RequestValidationError
from fastapi.routing import APIRoute
from pydantic import BaseModel
import uvicorn
class Login(BaseModel):
username: str
password: str
checkbox: list
class ValidationError(APIRoute):
def get_route_handler(self) -> Callable:
original_route_handler = super().get_route_handler()
async def customer_route_handler(request: Request) -> Response:
try:
return await original_route_handler(request)
except RequestValidationError as exc:
body = await request.body()
detail = {'error': exc.errors(), 'body': body.decode()}
raise HTTPException(status_code=200, detail=detail)
return customer_route_handler
app = FastAPI()
app.router.route_class = ValidationError
app.mount('/static', StaticFiles(directory='static'), name='static')
templates = Jinja2Templates(directory='templates')
@app.get('/index', tags=['Page'])
async def index(request: Request):
return templates.TemplateResponse('template_fastapi/index.vue', context={'request': request})
@app.get('/dashboard', tags=['Page'])
async def dashboard(request: Request):
return templates.TemplateResponse('template_fastapi/dashboard.vue', context={'request': request})
@app.get('/', summary='First Page')
@app.get('/login', tags=['Page'], summary='Login', description='Page Login', response_model=Login)
async def login(request: Request):
return templates.TemplateResponse('template_fastapi/login.vue', context={'request': request})
@app.post('/login', tags=['Security'], response_model=Login)
async def login_post(formElements: Login):
"""
POST LOGIN
- **email**: your email here.
- **password**: your password here.
- **checkbox**: your remember
"""
items = formElements.dict()
return items
if __name__ == '__main__':
uvicorn.run('app_fastapi:app', debug=True, port=8888)