kopia lustrzana https://github.com/cirospaciari/socketify.py
rename middleware with async, sync and auto
rodzic
e6d67a7f2b
commit
c40b056433
|
@ -1,11 +1,26 @@
|
||||||
from socketify import App
|
from socketify import App
|
||||||
|
import inspect
|
||||||
|
|
||||||
def middleware(*functions):
|
def middleware(*functions):
|
||||||
def middleware_route(res, req):
|
async def middleware_route(res, req):
|
||||||
data = None
|
data = None
|
||||||
|
some_async_as_run = False
|
||||||
#cicle to all middlewares
|
#cicle to all middlewares
|
||||||
for function in functions:
|
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
|
#call middlewares
|
||||||
data = function(res, req, data)
|
data = function(res, req, data)
|
||||||
#stops if returns Falsy
|
#stops if returns Falsy
|
||||||
|
@ -14,13 +29,14 @@ def middleware(*functions):
|
||||||
|
|
||||||
return middleware_route
|
return middleware_route
|
||||||
|
|
||||||
def get_user(authorization_header):
|
async def get_user(authorization):
|
||||||
if authorization_header:
|
if authorization:
|
||||||
|
#you can do something async here
|
||||||
return { 'greeting': 'Hello, World' }
|
return { 'greeting': 'Hello, World' }
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def auth(res, req, data=None):
|
async def auth(res, req, data=None):
|
||||||
user = get_user(req.get_header('authorization'))
|
user = await get_user(req.get_header('authorization'))
|
||||||
if not user:
|
if not user:
|
||||||
res.write_status(403).end("not authorized")
|
res.write_status(403).end("not authorized")
|
||||||
return False
|
return False
|
||||||
|
@ -28,10 +44,17 @@ def auth(res, req, data=None):
|
||||||
#returns extra data
|
#returns extra data
|
||||||
return user
|
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):
|
def home(res, req, user=None):
|
||||||
res.end(user.get('greeting', None))
|
res.cork_end(user.get('greeting', None))
|
||||||
|
|
||||||
app = App()
|
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.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
|
||||||
app.run()
|
app.run()
|
|
@ -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()
|
|
|
@ -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()
|
Ładowanie…
Reference in New Issue