https://github.com/yusukeiwaki/nano_router
https://github.com/yusukeiwaki/nano_router
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/yusukeiwaki/nano_router
- Owner: YusukeIwaki
- Created: 2018-05-30T10:02:50.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-06-01T01:47:34.000Z (over 7 years ago)
- Last Synced: 2024-10-18T08:31:43.370Z (about 1 year ago)
- Language: Python
- Size: 7.81 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Nano Router: A very tiny router library.
## Say "Good-bye!" to boring if-clauses!
```py
def handle_request(obj):
if obj["method"] == "GET":
return handle_get(obj)
elif obj["method"] == "POST":
return handle_post(obj)
else:
return handle_unknown(obj)
def handle_get(obj):
if obj["path"] == "/":
return "OK"
elif obj["path"] == "/ping":
return "pong"
else:
return "404"
:
:
:
:
```
## Do it smart and simple way!
### main_router.py
```py
from nano_router import RegexRouter
from sub_router import router as sub
router = RegexRouter()
@router.Route("^GET /.*")
def handle_get(obj):
return sub.handle(obj["path"], obj)
@router.Route("POST /.*")
def handle_post(obj):
return "OK"
@router.DefaultRoute
def handle_known_request(obj):
return "500"
```
### sub_router.py
```py
from nano_router import RegexRouter
router = RegexRouter()
@router.Route("/")
def get_index(obj):
return "index"
@router.Route("/ping")
def get_ping(obj):
return "pong"
@router.DefaultRoute
def get_default(obj):
return "404"
```
### main.py
```py
from main_router import router
def handle(obj):
print router.handle("%s %s"%(obj["method"], obj["path"]), obj)
handle({ "method": "GET", "path": "/" })
# => index
handle({ "method": "GET", "path": "/ping" })
# => pong
handle({ "method": "GET", "path": "/favicon.ico" })
# => 404
handle({
"method": "POST", "path": "/users",
"body": {
"user": { "name": "YusukeIwaki" }
}
})
# => OK
handle({ "method": "DELETE", "path": "/users/1" })
# => 500
```
## Install
### via 'pip install'
```
pip install git+https://github.com/YusukeIwaki/nano_router
```
### or "Copy & Paste" simply 😁
* https://github.com/YusukeIwaki/nano_router/blob/master/nano_router/router.py
* https://github.com/YusukeIwaki/nano_router/blob/master/nano_router/regex_router.py