Router Configurator for you project.
Project description
This is POC, not for production use
Installation
pip install urouter
Quickstart
Define handler in router
from aiohttp import web
from urouter.exporters.aiohttp_exporter import AioHttpRouter
app = web.Application()
async def handler(request):
return web.Response(text="Hello World")
router = AioHttpRouter(app)
router.get("/home", handler)
# At this point aiohttp is ready to register all routes
router.export()
Define global middleware for all routes
from aiohttp import web
from urouter.exporters.aiohttp_exporter import AioHttpRouter
app = web.Application()
@web.middleware
async def info_middleware(request, handler):
# some operation before handle request
response = await handler(request)
return response
async def handler(request):
return web.Response(text="Hello World")
router = (AioHttpRouter(app)
.use(info_middleware))
# Register handler
router.get("/home", handler)
# At this point aiohttp is ready to register all routes
router.export()
Define inline middleware (run only for specific handler)
from aiohttp import web
from urouter.exporters.aiohttp_exporter import AioHttpRouter
app = web.Application()
@web.middleware
async def info_middleware(request, handler):
# some operation before handle request
response = await handler(request)
return response
async def handler(request):
return web.Response(text="Hello World")
router = AioHttpRouter(app)
# Middleware registered only for `/home` route
router.include(info_middleware).get("/home", handler)
# At this point aiohttp is ready to register all routes
router.export()
Define subrouter
from aiohttp import web
from urouter.exporters.aiohttp_exporter import AioHttpRouter
app = web.Application()
@web.middleware
async def info_middleware(request, handler):
# some operation before handle request
response = await handler(request)
return response
async def handler(request):
return web.Response(text="Hello World")
async def sub_handler(request):
return web.Response(text="Hello World")
router = AioHttpRouter(app)
router.include(info_middleware).get("/home", handler)
# Create new router
subrouter = router.make_router()
subrouter.get("/hello", sub_handler)
# All registered router become starts with prefix `/sub/`
# `/hello` becomes `/sub/hello`
router.mount("/sub", subrouter)
# At this point aiohttp is ready to register all routes
router.export()
Private/Public router
from aiohttp import web
from urouter.exporters.aiohttp_exporter import AioHttpRouter
app = web.Application()
@web.middleware
async def auth_middleware(request, handler):
return web.HTTPForbidden()
async def public(request):
return web.Response(text="Hello World")
async def private(request):
return web.Response(text="Private Zone")
router = AioHttpRouter(app)
private_route = router.make_router().use(auth_middleware)
# Adding public handler
router.get("/home", public)
# Adding public handler
private_route.get("/private", private)
# Mount private router to main router
router.mount("/", private_route)
# At this point aiohttp is ready to register all routes
router.export()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
urouter-0.0.1.tar.gz
(5.0 kB
view hashes)
Built Distribution
Close
Hashes for urouter-0.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f688a2d7c5a33fca54d19096932ee56d8e9be0308705c6a2b31a010139affeb |
|
MD5 | 6e35645d829172efc30d6b36a95bfa29 |
|
BLAKE2b-256 | 938e80306082c4065c7fbace4765ac0336095ebac6e92d79273ed8e8f8f0350d |