https://github.com/el1s7/sanic_routes
https://github.com/el1s7/sanic_routes
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/el1s7/sanic_routes
- Owner: el1s7
- Created: 2021-03-07T14:38:30.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-06-10T18:27:20.000Z (almost 4 years ago)
- Last Synced: 2025-02-16T05:42:12.362Z (3 months ago)
- Language: Python
- Size: 11.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Sanic Routes
Generate routes and validate parameters from a JSON schema.### Example
> routes.py
```python
from . import controllers, middlewares
from sanic_routes import make_routes
schema = {
'login': {
'method': 'POST',
'path': '/login',
'controller': 'login' # By default controller is the key name
'before': ['logged_check'] # Array of middlewares before request
'params': {
'username': {
'required': True,
'max': 20,
'min': 1,
'type': str, # Custom functions supported
'help': 'The login username',
# location: Either ('query','path','form','json','headers','cookies') Default is 'form' for post requests
}
}
}
'register': {
...another route here
}
}routes = make_routes(schema, controllers=controllers, middlewares=middlewares)
```> controllers.py
```python
async def login(request):
params = request.ctx.params
usename = params.username # Parsed from params
```> app.py
```python
from routes import routes
from sanic import Sanicdef run():
app = Sanic(__name__)
app.blueprint(routes)
app.run(host="0.0.0.0", port=8001, debug=True)
```