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
|
#!/usr/bin/env python
"""
FastAPI site serving static html files.
"""
from os import listdir
import uvicorn
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.exceptions import HTTPException
ARTICLES = 'static/html'
templates = Jinja2Templates(directory="templates")
LIST_OF_ARTICLES = [i for i in listdir(ARTICLES) if i.endswith('.html')]
async def page_not_found(request: Request, exc: HTTPException):
"""Return 404 page"""
return templates.TemplateResponse(
'error_404/error_404.html', {
'request': request,
},
status_code=404
)
exception_handlers = {
404: page_not_found,
}
app = FastAPI(exception_handlers=exception_handlers)
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/")
async def main_page(request: Request):
"""Generate main page"""
return templates.TemplateResponse(
"main_page.html", {
"request": request,
"files": LIST_OF_ARTICLES,
"static": ARTICLES,
}
)
if __name__ == "__main__":
uvicorn.run(
app,
port=8000,
host='127.0.0.1',
log_level="debug",
uds="/run/tech_blog.sock",
)
|