squeaky-pl / japronto

Screaming-fast Python 3.5+ HTTP toolkit integrated with pipelining HTTP server based on uvloop and picohttpparser.
MIT License
8.61k stars 581 forks source link

add route decorators #26

Open r0fls opened 7 years ago

r0fls commented 7 years ago

Addresses issue https://github.com/squeaky-pl/japronto/issues/5

r0fls commented 7 years ago

Usage example:

from japronto import Application

app = Application()

@app.get('/')
def hello(request):
    return request.Response(text='Hello world!')

app.run(debug=True)
r0fls commented 7 years ago

Oh, it looks like you're not ready to implement this :) Didn't read the whole thread... feel free to close.

ojss commented 7 years ago

@r0fls we can keep it open and maybe modify it as and when @squeaky-pl revamps the routing?

squeaky-pl commented 7 years ago

Yes, I would like to keep it open, if it takes too much time to revamp routing I am gonna covert it into a recipe.

Martmists-GH commented 6 years ago

What's the progress on this?

gugadev commented 6 years ago

@squeaky-pl @r0fls Any update about this feature? This is the most "pythonic" and practic way to do routing, like Flask or Sanic.

sharkguto commented 6 years ago

Any update?

Detrous commented 5 years ago

Any update ? @squeaky-pl

devblack commented 3 years ago

Looks great, add this if to support awaitable handlers.

# import
from inspect import iscoroutinefunction
####

def route(self, path: str = '/', methods: list = []):
        '''
        Shorthand route decorator. Avoids need to register
        handlers to the router directly with `app.router.add_route()`.
        '''
        def decorator(handler):
            async def wrapper(*args, **kwargs):
                # check awaitable handler
                if iscoroutinefunction(handler):
                    return await handler(*args, **kwargs)
                return handler(*args, **kwargs)
            self.router.add_route(path, wrapper, methods=methods)
            return wrapper
        return decorator