-
Notifications
You must be signed in to change notification settings - Fork 51
/
app.py
59 lines (43 loc) · 1.48 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
from starlette.applications import Starlette
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.staticfiles import StaticFiles
from starlette.routing import Route, Mount
from starlette.templating import Jinja2Templates
import uvicorn
templates = Jinja2Templates(directory='templates')
async def homepage(request):
template = "index.html"
context = {"request": request}
return templates.TemplateResponse(template, context)
async def error(request):
"""
An example error. Switch the `debug` setting to see either tracebacks or 500 pages.
"""
raise RuntimeError("Oh no")
async def not_found(request: Request, exc: HTTPException):
"""
Return an HTTP 404 page.
"""
template = "404.html"
context = {"request": request}
return templates.TemplateResponse(template, context, status_code=404)
async def server_error(request: Request, exc: HTTPException):
"""
Return an HTTP 500 page.
"""
template = "500.html"
context = {"request": request}
return templates.TemplateResponse(template, context, status_code=500)
routes = [
Route('/', homepage),
Route('/error', error),
Mount('/static', app=StaticFiles(directory='statics'), name='static')
]
exception_handlers = {
404: not_found,
500: server_error
}
app = Starlette(debug=True, routes=routes, exception_handlers=exception_handlers)
if __name__ == "__main__":
uvicorn.run(app, host='0.0.0.0', port=8000)