Skip to content

Commit

Permalink
release 0.0.303 (#34)
Browse files Browse the repository at this point in the history
* add add_middleware helper

---------

Co-authored-by: nggit <[email protected]>
  • Loading branch information
nggit and nggit committed Nov 18, 2023
1 parent d0abc0a commit 470427e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion example_uvloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ async def app(scope, receive, send):
})

if __name__ == '__main__':
tremolo.run(app, host='0.0.0.0', port=8000, debug=True)
tremolo.run(app, host='0.0.0.0', port=8000, debug=True, reload=True)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='tremolo',
version='0.0.302',
version='0.0.303',
license='MIT',
author='nggit',
author_email='[email protected]',
Expand Down
4 changes: 4 additions & 0 deletions tests/test_tremolo_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ def test_middleware(self):
self.assertEqual(func(), b'Halt!')
self.assertEqual(options, {})

def test_invalidmiddleware(self):
with self.assertRaises(ValueError):
app.add_middleware(middlewares.on_request, 'invalid')

@function
async def test_handler(self):
for handler in app.routes[1]:
Expand Down
2 changes: 1 addition & 1 deletion tremolo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.0.302'
__version__ = '0.0.303'

from .tremolo import Tremolo # noqa: E402
from . import exceptions # noqa: E402,F401
Expand Down
16 changes: 13 additions & 3 deletions tremolo/tremolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def decorator(func):
def wrapper(**kwargs):
return func(**kwargs)

self.add_route(path, wrapper, self.getoptions(func))
self.add_route(wrapper, path, self.getoptions(func))
return wrapper

return decorator
Expand Down Expand Up @@ -134,7 +134,7 @@ def decorator(func):
def wrapper(**kwargs):
return func(**kwargs)

self.middlewares[name].append((wrapper, self.getoptions(func)))
self.add_middleware(wrapper, name, self.getoptions(func))
return wrapper

return decorator
Expand Down Expand Up @@ -176,7 +176,17 @@ def getoptions(self, func):

return options

def add_route(self, path, func, kwargs={}):
def add_middleware(self, func, name='request', kwargs={}):
if name not in self.middlewares:
raise ValueError('%s is not one of the: %s' %
(name, ', '.join(self.middlewares)))

self.middlewares[name].append((func, kwargs or self.getoptions(func)))

def add_route(self, func, path='/', kwargs={}):
if not kwargs:
kwargs = self.getoptions(func)

if path.startswith('^') or path.endswith('$'):
pattern = path.encode('latin-1')
self.routes[-1].append((pattern, func, kwargs))
Expand Down
4 changes: 2 additions & 2 deletions websocket_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ async def ws_handler(websocket=None, request=None, stream=False, **_):
"""

if __name__ == '__main__':
# don't forget to disable debug on production!
app.run('0.0.0.0', 8000, debug=True)
# don't forget to disable debug and reload on production!
app.run('0.0.0.0', 8000, debug=True, reload=True)

0 comments on commit 470427e

Please sign in to comment.