-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsample.py
More file actions
109 lines (78 loc) · 2.79 KB
/
Copy pathsample.py
File metadata and controls
109 lines (78 loc) · 2.79 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
import os
from starlette.exceptions import HTTPException
from starlette.responses import JSONResponse, PlainTextResponse
from starlette.routing import Host, Mount, Route, Router
from ellar.common import Controller, Module, ModuleRouter, exception_handler, get
from ellar.core import APIKeyQuery, ModuleBase, Request, WebSocket
def create_tmp_template_and_static_dir(tmpdir):
template_folder = os.path.join(tmpdir, "templates")
os.mkdir(template_folder)
static_folder = os.path.join(tmpdir, "statics")
os.mkdir(static_folder)
path = os.path.join(static_folder, "example.txt")
with open(path, "w") as file:
file.write("<file content>")
path = os.path.join(template_folder, "example.html")
with open(path, "w") as file:
file.write("<html>Hello World<html/>")
return template_folder, static_folder
class AppAPIKey(APIKeyQuery):
async def authenticate(self, connection, key):
return key
def custom_sub_domain(request):
return PlainTextResponse("Subdomain: " + request.path_params["subdomain"])
def all_users_page(request):
return PlainTextResponse("Hello, everyone!")
def user_page(request):
username = request.path_params["username"]
return PlainTextResponse(f"Hello, {username}!")
sub_domain = Router(
routes=[
Route("/", custom_sub_domain),
]
)
users = Router(
routes=[
Route("/", endpoint=all_users_page),
Route("/{username}", endpoint=user_page),
]
)
router = ModuleRouter()
@router.get("/func")
@router.head("/func")
def func_homepage(request: Request):
return PlainTextResponse("Hello, world!")
@router.get("/async")
async def async_homepage(request: Request):
return PlainTextResponse("Hello, world!")
@router.ws_route("/ws")
async def websocket_endpoint(session: WebSocket):
await session.accept()
await session.send_text("Hello, world!")
await session.close()
@Controller
class ClassBaseController:
@get("/class")
def class_function(self):
return PlainTextResponse("Hello, world!")
@get("/500")
def runtime_error(self, request: Request):
raise RuntimeError()
@Module(
routers=[
Host("{subdomain}.example.org", app=sub_domain),
Mount("/users", app=users),
router,
],
controllers=[ClassBaseController],
)
class ApplicationModule(ModuleBase):
@exception_handler(405)
async def method_not_allow_exception(self, ctx, exec):
return JSONResponse({"detail": "Custom message"}, status_code=405)
@exception_handler(500)
async def error_500(self, ctx, exec):
return JSONResponse({"detail": "Server Error"}, status_code=500)
@exception_handler(HTTPException)
async def http_exception(self, ctx, exc):
return JSONResponse({"detail": exc.detail}, status_code=exc.status_code)