-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·44 lines (37 loc) · 1.34 KB
/
server.py
File metadata and controls
executable file
·44 lines (37 loc) · 1.34 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
#!/usr/bin/env python3
from pydantic import BaseSettings
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from fastapi.requests import Request
from pathlib import Path
class ServerSettings(BaseSettings):
hostname: str = '0.0.0.0'
port: int = 9999
module_path = Path(__file__).parent.absolute()
app = FastAPI()
app.mount("/static", StaticFiles(directory=module_path / "static"), name="static")
templates = Jinja2Templates(directory=module_path / "templates")
def get_pics() -> list[str]:
return glob.glob(str(module_path / 'static/*'), recursive=True)
@app.get("/")
def root(self, request: Request) -> HTMLResponse:
"""Return a response."""
response = templates.TemplateResponse(
'index.html',
{'request': request, 'pics': get_pics()}
)
# The most important feature of this webserver
response.init_headers({'x-clacks-overhead': 'long live Terry'})
return response
def main(settings: ServerSettings | None=None):
"""Run webserver with default settings or the given settings."""
if settings is None:
settings = ServerSettings(
# type: ignore
)
host = f'{settings.hostname}:{settings.port}'
# app.start(host, name='sample_http')
if __name__ == '__main__':
main()