Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yusukeiwaki/nano_router
https://github.com/yusukeiwaki/nano_router
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/yusukeiwaki/nano_router
- Owner: YusukeIwaki
- Created: 2018-05-30T10:02:50.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-06-01T01:47:34.000Z (over 6 years ago)
- Last Synced: 2024-10-18T08:31:43.370Z (3 months 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 subrouter = 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 RegexRouterrouter = 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 routerdef handle(obj):
print router.handle("%s %s"%(obj["method"], obj["path"]), obj)handle({ "method": "GET", "path": "/" })
# => indexhandle({ "method": "GET", "path": "/ping" })
# => ponghandle({ "method": "GET", "path": "/favicon.ico" })
# => 404handle({
"method": "POST", "path": "/users",
"body": {
"user": { "name": "YusukeIwaki" }
}
})
# => OKhandle({ "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