Skip to content

Commit ef5e068

Browse files
committed
Refactor code
1 parent 8eea3b3 commit ef5e068

File tree

4 files changed

+168
-134
lines changed

4 files changed

+168
-134
lines changed

integration_tests/base_routes.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ async def hello(request):
4343
global callCount
4444
callCount += 1
4545
message = "Called " + str(callCount) + " times"
46-
print(message)
47-
return jsonify(request)
46+
print(message, request)
47+
return {"status_code": "200", "body": "hello", "type": "text"}
48+
# return
4849

4950

5051
@app.before_request("/")

robyn/__init__.py

+21-132
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .processpool import spawn_process
1414
from .log_colors import Colors
1515
from .ws import WS
16+
from .router import Router, MiddlewareRouter, WebSocketRouter
1617

1718
# 3rd party imports and exports
1819
from multiprocess import Process
@@ -32,15 +33,14 @@ def __init__(self, file_object):
3233
self.dev = self.parser.is_dev()
3334
self.processes = self.parser.num_processes()
3435
self.workers = self.parser.workers()
35-
self.routes = []
36+
self.router = Router()
37+
self.middleware_router = MiddlewareRouter()
38+
self.web_socket_router = WebSocketRouter()
3639
self.headers = []
37-
self.routes = []
38-
self.middlewares = []
39-
self.web_sockets = {}
4040
self.directories = []
4141
self.event_handlers = {}
4242

43-
def add_route(self, route_type, endpoint, handler):
43+
def _add_route(self, route_type, endpoint, handler):
4444
"""
4545
[This is base handler for all the decorators]
4646
@@ -51,133 +51,23 @@ def add_route(self, route_type, endpoint, handler):
5151

5252
""" We will add the status code here only
5353
"""
54-
async def async_inner_handler(*args):
55-
res = await handler(args)
56-
if type(res) == "dict":
57-
if "status_code" not in res:
58-
res["status_code"] = "200"
59-
else:
60-
response = {
61-
"status_code": "200",
62-
"body": res,
63-
"type": "text"
64-
}
65-
res = response
66-
return res
67-
68-
def inner_handler(*args):
69-
res = handler(args)
70-
if type(res) == "dict":
71-
if "status_code" not in res:
72-
res["status_code"] = 200
73-
else:
74-
response = {
75-
"status_code": 200,
76-
"body": res,
77-
"type": "text"
78-
}
79-
res = response
80-
return res
81-
82-
number_of_params = len(signature(handler).parameters)
83-
if asyncio.iscoroutinefunction(handler):
84-
self.routes.append(
85-
(
86-
route_type,
87-
endpoint,
88-
async_inner_handler,
89-
True,
90-
number_of_params,
91-
)
92-
)
93-
else:
94-
self.routes.append(
95-
(
96-
route_type,
97-
endpoint,
98-
inner_handler,
99-
False,
100-
number_of_params,
101-
)
102-
)
103-
104-
def add_middleware_route(self, route_type, endpoint, handler):
105-
"""
106-
[This is base handler for the middleware decorator]
107-
108-
:param route_type [str]: [??]
109-
:param endpoint [str]: [endpoint for the route added]
110-
:param handler [function]: [represents the sync or async function passed as a handler for the route]
111-
"""
112-
113-
""" We will add the status code here only
114-
"""
115-
number_of_params = len(signature(handler).parameters)
116-
self.middlewares.append(
117-
(
118-
route_type,
119-
endpoint,
120-
handler,
121-
asyncio.iscoroutinefunction(handler),
122-
number_of_params,
123-
)
124-
)
54+
self.router.add_route(route_type, endpoint, handler)
12555

12656
def before_request(self, endpoint):
12757
"""
12858
[The @app.before_request decorator to add a get route]
12959
13060
:param endpoint [str]: [endpoint to server the route]
13161
"""
132-
133-
# This inner function is basically a wrapper arround the closure(decorator)
134-
# being returned.
135-
# It takes in a handler and converts it in into a closure
136-
# and returns the arguments.
137-
# Arguments are returned as they could be modified by the middlewares.
138-
def inner(handler):
139-
async def async_inner_handler(*args):
140-
await handler(args)
141-
return args
142-
143-
def inner_handler(*args):
144-
handler(*args)
145-
return args
146-
147-
if asyncio.iscoroutinefunction(handler):
148-
self.add_middleware_route("BEFORE_REQUEST", endpoint, async_inner_handler)
149-
else:
150-
self.add_middleware_route("BEFORE_REQUEST", endpoint, inner_handler)
151-
152-
return inner
62+
return self.middleware_router.add_before_request(endpoint)
15363

15464
def after_request(self, endpoint):
15565
"""
15666
[The @app.after_request decorator to add a get route]
15767
15868
:param endpoint [str]: [endpoint to server the route]
15969
"""
160-
161-
# This inner function is basically a wrapper arround the closure(decorator)
162-
# being returned.
163-
# It takes in a handler and converts it in into a closure
164-
# and returns the arguments.
165-
# Arguments are returned as they could be modified by the middlewares.
166-
def inner(handler):
167-
async def async_inner_handler(*args):
168-
await handler(args)
169-
return args
170-
171-
def inner_handler(*args):
172-
handler(*args)
173-
return args
174-
175-
if asyncio.iscoroutinefunction(handler):
176-
self.add_middleware_route("AFTER_REQUEST", endpoint, async_inner_handler)
177-
else:
178-
self.add_middleware_route("AFTER_REQUEST", endpoint, inner_handler)
179-
180-
return inner
70+
return self.middleware_router.add_after_request(endpoint)
18171

18272
def add_directory(
18373
self, route, directory_path, index_file=None, show_files_listing=False
@@ -188,7 +78,7 @@ def add_header(self, key, value):
18878
self.headers.append((key, value))
18979

19080
def add_web_socket(self, endpoint, ws):
191-
self.web_sockets[endpoint] = ws
81+
self.web_socket_router.add_route(endpoint, ws)
19282

19383
def _add_event_handler(self, event_type: str, handler):
19484
print(f"Add event {event_type} handler")
@@ -213,17 +103,16 @@ def start(self, url="127.0.0.1", port=5000):
213103
if not self.dev:
214104
workers = self.workers
215105
socket = SocketHeld(url, port)
216-
print(self.middlewares)
217106
for _ in range(self.processes):
218107
copied_socket = socket.try_clone()
219108
p = Process(
220109
target=spawn_process,
221110
args=(
222111
self.directories,
223112
self.headers,
224-
self.routes,
225-
self.middlewares,
226-
self.web_sockets,
113+
self.router.get_routes(),
114+
self.middleware_router.get_routes(),
115+
self.web_socket_router.get_routes(),
227116
self.event_handlers,
228117
copied_socket,
229118
workers,
@@ -256,7 +145,7 @@ def get(self, endpoint):
256145
"""
257146

258147
def inner(handler):
259-
self.add_route("GET", endpoint, handler)
148+
self._add_route("GET", endpoint, handler)
260149

261150
return inner
262151

@@ -268,7 +157,7 @@ def post(self, endpoint):
268157
"""
269158

270159
def inner(handler):
271-
self.add_route("POST", endpoint, handler)
160+
self._add_route("POST", endpoint, handler)
272161

273162
return inner
274163

@@ -280,7 +169,7 @@ def put(self, endpoint):
280169
"""
281170

282171
def inner(handler):
283-
self.add_route("PUT", endpoint, handler)
172+
self._add_route("PUT", endpoint, handler)
284173

285174
return inner
286175

@@ -292,7 +181,7 @@ def delete(self, endpoint):
292181
"""
293182

294183
def inner(handler):
295-
self.add_route("DELETE", endpoint, handler)
184+
self._add_route("DELETE", endpoint, handler)
296185

297186
return inner
298187

@@ -304,7 +193,7 @@ def patch(self, endpoint):
304193
"""
305194

306195
def inner(handler):
307-
self.add_route("PATCH", endpoint, handler)
196+
self._add_route("PATCH", endpoint, handler)
308197

309198
return inner
310199

@@ -316,7 +205,7 @@ def head(self, endpoint):
316205
"""
317206

318207
def inner(handler):
319-
self.add_route("HEAD", endpoint, handler)
208+
self._add_route("HEAD", endpoint, handler)
320209

321210
return inner
322211

@@ -328,7 +217,7 @@ def options(self, endpoint):
328217
"""
329218

330219
def inner(handler):
331-
self.add_route("OPTIONS", endpoint, handler)
220+
self._add_route("OPTIONS", endpoint, handler)
332221

333222
return inner
334223

@@ -340,7 +229,7 @@ def connect(self, endpoint):
340229
"""
341230

342231
def inner(handler):
343-
self.add_route("CONNECT", endpoint, handler)
232+
self._add_route("CONNECT", endpoint, handler)
344233

345234
return inner
346235

@@ -352,6 +241,6 @@ def trace(self, endpoint):
352241
"""
353242

354243
def inner(handler):
355-
self.add_route("TRACE", endpoint, handler)
244+
self._add_route("TRACE", endpoint, handler)
356245

357246
return inner

0 commit comments

Comments
 (0)