Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
143 lines (110 loc) · 3.71 KB
/
Copy pathapp.py
File metadata and controls
143 lines (110 loc) · 3.71 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from pathlib import Path
from litestar import Litestar
from litestar.middleware import (
AbstractAuthenticationMiddleware,
AuthenticationResult,
DefineMiddleware,
)
from litestar.connection import ASGIConnection
from litestar.exceptions import NotAuthorizedException
from litestar.contrib.jinja import JinjaTemplateEngine
from litestar.template.config import TemplateConfig
from litestar.openapi.config import OpenAPIConfig
from litestar.openapi.plugins import (
RapidocRenderPlugin,
RedocRenderPlugin,
ScalarRenderPlugin,
StoplightRenderPlugin,
SwaggerRenderPlugin,
YamlRenderPlugin,
)
from functions import ntfy_client
from functions import fortune as fortune_function
from functions import get_pids_in_port
from aux.logging import logging_config
from database import create_db_tables
from database import Text
from routes import (
root_path,
display_dhammapada,
dhammapada_qid,
text_to_img,
get_cowsay,
get_box,
get_fortune,
get_specific,
get_index,
github_webhook,
reboot,
)
from routes_dashboard import (
dhammapada_qid_dashboard
)
import secret_config
# DEBUG = True
DEBUG = False
noauth = {"exclude_from_auth": True}
class YokeAuthMiddleware(AbstractAuthenticationMiddleware):
async def authenticate_request(self, connection: ASGIConnection)\
-> AuthenticationResult:
auth_header = connection.headers.get(secret_config.API_KEY_HEADER)
if not auth_header:
raise NotAuthorizedException()
auth = secret_config.check_auth(header=auth_header)
if not auth:
raise NotAuthorizedException()
return AuthenticationResult(**auth)
async def on_startup():
await create_db_tables(Text, if_not_exists=True)
fortune = fortune_function()
await Text.insert(Text(text=fortune))
pid_list = get_pids_in_port(port=8000)
message = f"pids: {', '.join(pid_list)}\n\n{fortune}"
ntfy_client(message=message, title="server started", priority="low")
async def on_shutdown():
texts = await Text.select()
fortune = "\n\n".join([f"{text['id']}: {text['text']}"
for text in texts])
pid_list = get_pids_in_port(port=8000)
message = f"pids: {', '.join(pid_list)}\n\n{fortune}"
ntfy_client(message=message, title="server stopping", priority="low")
auth_mw = DefineMiddleware(YokeAuthMiddleware, exclude="schema")
route_handlers = [
root_path,
display_dhammapada,
dhammapada_qid,
text_to_img,
get_cowsay,
get_box,
get_fortune,
get_specific,
get_index,
github_webhook,
reboot,
dhammapada_qid_dashboard,
]
template_config = TemplateConfig(directory=Path("templates"),
engine=JinjaTemplateEngine)
# https://docs.litestar.dev/2/usage/openapi/schema_generation.html
# https://docs.litestar.dev/2/reference/app.html
app = Litestar(route_handlers=route_handlers,
middleware=[auth_mw],
logging_config=logging_config,
template_config=template_config,
openapi_config=OpenAPIConfig(
title='Yoke API',
version='0.0.1',
use_handler_docstrings=False,
render_plugins=[
RapidocRenderPlugin(),
RedocRenderPlugin(),
ScalarRenderPlugin(),
StoplightRenderPlugin(),
SwaggerRenderPlugin(),
YamlRenderPlugin()
],
),
on_startup=[on_startup],
on_shutdown=[on_shutdown],
pdb_on_exception=DEBUG
)