kopia lustrzana https://github.com/cirospaciari/socketify.py
add offical helper for middlewares
rodzic
c40b056433
commit
376b087e86
|
@ -1,33 +1,5 @@
|
||||||
from socketify import App
|
from socketify import App, middleware
|
||||||
import inspect
|
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):
|
async def get_user(authorization):
|
||||||
if authorization:
|
if authorization:
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from socketify import App
|
from socketify import App
|
||||||
|
|
||||||
|
#this is just an example of implementation you can just import using from socketify import middleware for an more complete version
|
||||||
|
|
||||||
async def get_user(authorization):
|
async def get_user(authorization):
|
||||||
if authorization:
|
if authorization:
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from socketify import App
|
from socketify import App
|
||||||
|
|
||||||
|
#this is just an example of implementation you can just import using from socketify import middleware for an more complete version
|
||||||
|
|
||||||
def middleware(*functions):
|
def middleware(*functions):
|
||||||
def middleware_route(res, req):
|
def middleware_route(res, req):
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
from .socketify import App, AppOptions, AppListenOptions, OpCode, SendStatus, CompressOptions
|
from .socketify import App, AppOptions, AppListenOptions, OpCode, SendStatus, CompressOptions, middleware
|
||||||
from .helpers import sendfile
|
from .helpers import sendfile
|
|
@ -1826,3 +1826,30 @@ class AppOptions:
|
||||||
self.ssl_ciphers = ssl_ciphers
|
self.ssl_ciphers = ssl_ciphers
|
||||||
self.ssl_prefer_low_memory_usage = ssl_prefer_low_memory_usage
|
self.ssl_prefer_low_memory_usage = ssl_prefer_low_memory_usage
|
||||||
|
|
||||||
|
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
|
Ładowanie…
Reference in New Issue