forked from Defendinary/future
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
261 lines (210 loc) · 8.64 KB
/
example.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env python3
"""
Runs the application for local development. This file should not be used to start the
application for production.
Refer to https://www.uvicorn.org/deployment/ for production deployments.
"""
from future.application import Future
from future.controller import WelcomeController
from future.middleware import ResponseCodeConfuser, TestMiddlewareRequest, TestMiddlewareResponse
from future.routing import RouteGroup, Get
import os
from future.logger import setup_logging
from future.request import Request
from future.response import Response
from future.middleware import Middleware
from future.controller import WelcomeController
from future.routing import *
from future.types import ASGIScope, ASGIReceive, ASGISend
from typing import Any, Callable, Optional, Union, TypedDict
from rich.console import Console
import re
import logging
import uvicorn
setup_logging()
# spawn background tasks in the lifespan startup process... or database connections etc...
# on shutdown, save shit, send info about shutdown etc...
from pprint import pprint as print
#@dataclass
class Lifespan:
#app: ASGIApp
async def __aenter__(self):
print("startup things")
async def __aexit__(self, exc_type, exc_value, tb) -> bool | None:
print("shutdown things")
None
async def lifespan(app):
async with asyncio.timeout(30):
print("startup things")
#await.could_hang_forever()
try:
yield # { "a": "b" } # yields stuff into scope
finally:
async with asyncio.timeout(30):
print("shutdown things")
class Future:
def __init__(self, lifespan, name: str = "Future", debug: bool = False, domain: str = ""):
self.lifespan = lifespan
self.debug = debug
self.domain = domain
self.logger = logging.getLogger(name)
self.routes: dict[str, list[EndpointConfig]] = {}
def _add_route(self, route: Route, subdomain: Optional[str] = None) -> None:
if subdomain not in self.routes:
self.routes[subdomain] = []
before = []
after = []
for middleware in route.middlewares:
if middleware.attach_to == "request":
before.append(middleware)
elif middleware.attach_to == "response":
after.append(middleware)
before.sort(key=lambda middleware: middleware.priority)
after.sort(key=lambda middleware: middleware.priority)
endpoint_config = EndpointConfig(
#endpoint=route.endpoint,
middleware_before=before,
middleware_after=after,
#regex={"paths": [route._rx]},
#og_path=route.path,
route=route # Include the full Route object
)
self.routes[subdomain].append(endpoint_config)
def add_routes(self, routes: list[Union[Route, RouteGroup]]) -> None:
for x in routes:
if isinstance(x, Route):
self._add_route(
route=x,
subdomain=None
)
elif isinstance(x, RouteGroup):
for route in x.routes:
full_path = f"{x.prefix}{route.path}"
combined_middlewares = x.middlewares + route.middlewares
route.path = full_path
route.middlewares = combined_middlewares
self._add_route(
route=route,
subdomain=x.subdomain
)
else:
raise NotImplementedError
async def handle_lifespan(self, scope: ASGIScope, receive: ASGIReceive, send: ASGISend) -> None:
assert scope["type"] == "lifespan"
message = await receive()
assert message["type"] == "lifespan.startup"
started = False
app = scope.get("app")
try:
async with self.lifespan(app) as state:
if state is not None:
scope["state"].update(state)
await send({"type": "lifespan.startup.complete"})
started = True
message = await receive()
assert message["type"] == "lifespan.shutdown"
except BaseException:
event_type = "lifespan.shutdown.failed" if started else "lifespan.startup.failed"
await send({"type": event_type, "message": traceback.format_exc()})
raise
await send({"type": "lifespan.shutdown.complete"})
async def __call__(self, scope: ASGIScope, receive: ASGIReceive, send: ASGISend) -> None:
# Inject ourselves into the chain for later convenience
scope["app"] = self
print(scope)
if scope["type"] == "lifespan":
await self.handle_lifespan(scope, receive, send)
request = Request(scope, receive)
host_header_parts = request.host.split(".") if request.host else []
subdomain = host_header_parts[0] if len(host_header_parts) > 2 else ""
domain = (
host_header_parts[-2] + "." + host_header_parts[-1]
if len(host_header_parts) > 1
else ""
)
if domain != self.domain and not self.debug:
response = Response(body=b"Not Found", status=404)
await response(send)
return
request_path = request.path.encode()
subdomain_routes = self.routes.get(subdomain, [])
matched_route = None
route_params = None
# Perform the actual regex matching
for epc in subdomain_routes:
route: Route = epc["route"]
route_match: RouteMatch = route.match(request_path)
if route_match:
matched_route = epc
route_params = route_match.params
break
if matched_route is None:
response = Response(body=b"Not Found", status=404)
await response(send)
return
# See if we can unfuck anything here:
#endpoint = subdomain_routes.get(request_path).get('endpoint', None)
middleware_before = matched_route["middleware_before"]
middleware_after = matched_route["middleware_after"]
endpoint = matched_route["route"].endpoint
for b_middleware in middleware_before:
response = b_middleware.intercept(request)
if response is not None:
await response(send)
return
if route_params:
response = await endpoint(request, **route_params)
else:
response = await endpoint(request)
for a_middleware in middleware_after:
modified_response = a_middleware.intercept(request, response)
if modified_response is not None:
response = modified_response
await response(send)
def run(self, host: str = "127.0.0.1", port: int = 8000, workers: int = 4, tls_key: Optional[str] = None, tls_cert: Optional[str] = None, tls_password: Optional[str] = None) -> None:
console = Console()
console.rule("[bold yellow]Running for local development", align="left")
console.print(f"[bold yellow]Visit http://localhost:{port}/docs")
uvicorn.run(
app="__main__:app",
host=host,
port=port,
workers=workers,
reload=self.debug,
ssl_keyfile=tls_key,
ssl_certfile=tls_cert,
ssl_keyfile_password=tls_password,
timeout_graceful_shutdown=3,
log_level="info",
)
# Example usage
async def aString(request: Request, arg1) -> Response:
return Response(body=f"{arg1=}".encode())
async def anInt(request: Request, arg1, arg2) -> Response:
return Response(body=f"{arg1=}, {arg2=}".encode())
app = Future(lifespan=Lifespan(), name="Future", debug=False, domain="example.com:8000")
routes = [
# Grouped routes
RouteGroup(
name="test",
subdomain="api",
middlewares=[
#TestMiddlewareRequest,
#TestMiddlewareResponse,
#ResponseCodeConfuser,
],
routes=[
Get(path='/', endpoint=WelcomeController.root, name="Welcome"),
Get(path='/example/<str:aString>', endpoint=aString, name="aString"),
Get(path='/example/<int:one>/<str:two>', endpoint=anInt, name="anInt"),
],
),
# Also test Single Routes
Get(path='/ping', endpoint=WelcomeController.ping, name="Ping", middlewares=[TestMiddlewareRequest]),
]
app.add_routes(routes)
#print(app.routes)
if __name__ == "__main__":
#os.environ["APP_ENV"] = "dev"
#port = int(os.environ.get("APP_PORT", 44777))
app.run(host="0.0.0.0", port=8000, workers=1)