From c40b056433f37a847086317c2551c6a7e9da04d9 Mon Sep 17 00:00:00 2001 From: Ciro Date: Tue, 15 Nov 2022 09:22:09 -0300 Subject: [PATCH] rename middleware with async, sync and auto --- examples/middleware.py | 43 +++++++++++++++++++------- examples/middleware_auto.py | 60 ------------------------------------- examples/middleware_sync.py | 37 +++++++++++++++++++++++ 3 files changed, 70 insertions(+), 70 deletions(-) delete mode 100644 examples/middleware_auto.py create mode 100644 examples/middleware_sync.py diff --git a/examples/middleware.py b/examples/middleware.py index 682bc2d..6122959 100644 --- a/examples/middleware.py +++ b/examples/middleware.py @@ -1,26 +1,42 @@ from socketify import App - +import inspect def middleware(*functions): - def middleware_route(res, req): + async def middleware_route(res, req): data = None + some_async_as_run = False #cicle to all middlewares for function in functions: - #call middlewares - data = function(res, req, data) + #detect if is coroutine or not + if inspect.iscoroutinefunction(function): + #in async query string, arguments and headers are only valid until the first await + if not some_async_as_run: + #get_headers will preserve headers (and cookies) inside req, after await + headers = req.get_headers() + #get_parameters will preserve all params inside req after await + params = req.get_parameters() + #get queries will preserve all queries inside req after await + queries = req.get_queries() + #mark to only grab header, params and queries one time + some_async_as_run = True + data = await function(res, req, data) + else: + #call middlewares + data = function(res, req, data) #stops if returns Falsy if not data: break return middleware_route -def get_user(authorization_header): - if authorization_header: +async def get_user(authorization): + if authorization: + #you can do something async here return { 'greeting': 'Hello, World' } return None -def auth(res, req, data=None): - user = get_user(req.get_header('authorization')) +async def auth(res, req, data=None): + user = await get_user(req.get_header('authorization')) if not user: res.write_status(403).end("not authorized") return False @@ -28,10 +44,17 @@ def auth(res, req, data=None): #returns extra data return user +def another_middie(res, req, data=None): + #now we can mix sync and async and change the data here + if isinstance(data, dict): + gretting = data.get('greeting', '') + data['greeting'] = f"{gretting} from another middie ;)" + return data + def home(res, req, user=None): - res.end(user.get('greeting', None)) + res.cork_end(user.get('greeting', None)) app = App() -app.get("/", middleware(auth, home)) +app.get("/", middleware(auth, another_middie, home)) app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port)) app.run() \ No newline at end of file diff --git a/examples/middleware_auto.py b/examples/middleware_auto.py deleted file mode 100644 index 6122959..0000000 --- a/examples/middleware_auto.py +++ /dev/null @@ -1,60 +0,0 @@ -from socketify import App -import inspect - -def middleware(*functions): - async def middleware_route(res, req): - data = None - some_async_as_run = False - #cicle to all middlewares - for function in functions: - #detect if is coroutine or not - if inspect.iscoroutinefunction(function): - #in async query string, arguments and headers are only valid until the first await - if not some_async_as_run: - #get_headers will preserve headers (and cookies) inside req, after await - headers = req.get_headers() - #get_parameters will preserve all params inside req after await - params = req.get_parameters() - #get queries will preserve all queries inside req after await - queries = req.get_queries() - #mark to only grab header, params and queries one time - some_async_as_run = True - data = await function(res, req, data) - else: - #call middlewares - data = function(res, req, data) - #stops if returns Falsy - if not data: - break - - return middleware_route - -async def get_user(authorization): - if authorization: - #you can do something async here - return { 'greeting': 'Hello, World' } - return None - -async def auth(res, req, data=None): - user = await get_user(req.get_header('authorization')) - if not user: - res.write_status(403).end("not authorized") - return False - - #returns extra data - return user - -def another_middie(res, req, data=None): - #now we can mix sync and async and change the data here - if isinstance(data, dict): - gretting = data.get('greeting', '') - data['greeting'] = f"{gretting} from another middie ;)" - return data - -def home(res, req, user=None): - res.cork_end(user.get('greeting', None)) - -app = App() -app.get("/", middleware(auth, another_middie, home)) -app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port)) -app.run() \ No newline at end of file diff --git a/examples/middleware_sync.py b/examples/middleware_sync.py new file mode 100644 index 0000000..682bc2d --- /dev/null +++ b/examples/middleware_sync.py @@ -0,0 +1,37 @@ +from socketify import App + + +def middleware(*functions): + def middleware_route(res, req): + data = None + #cicle to all middlewares + for function in functions: + #call middlewares + data = function(res, req, data) + #stops if returns Falsy + if not data: + break + + return middleware_route + +def get_user(authorization_header): + if authorization_header: + return { 'greeting': 'Hello, World' } + return None + +def auth(res, req, data=None): + user = get_user(req.get_header('authorization')) + if not user: + res.write_status(403).end("not authorized") + return False + + #returns extra data + return user + +def home(res, req, user=None): + res.end(user.get('greeting', None)) + +app = App() +app.get("/", middleware(auth, home)) +app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port)) +app.run() \ No newline at end of file