Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/neonxp/mux
Simple regexp based HTTP muxer for Go
https://github.com/neonxp/mux
Last synced: about 1 month ago
JSON representation
Simple regexp based HTTP muxer for Go
- Host: GitHub
- URL: https://github.com/neonxp/mux
- Owner: neonxp
- License: mit
- Created: 2019-09-18T23:43:16.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-09-22T20:54:20.000Z (over 5 years ago)
- Last Synced: 2024-06-20T01:53:41.868Z (7 months ago)
- Language: Go
- Size: 13.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple HTTP muxer with greedy params
This is a simple HTTP muxer. It works like [pat](https://github.com/bmizerany/pat) with important difference - all url params greedy, see above.
## Why another router???
I just need a simple router that can work on nested routes `/head/elem1/elem2/.../elemN/tail`. That's all!
Routers like [pat](https://github.com/bmizerany/pat), [chi](https://github.com/go-chi/chi), [echo](https://github.com/labstack/echo) are awesome, but they cannot cover this particular use case.
## Match example
PatternPathMatch?Params
/simple/simple/testYes{}
/simple/s1mp1eNo{}
/one/:param1/three/one/two/threeYes{param1:"two"}
/one/:param1/five/one/two/three/four/fiveYes{param1:"two/three/four"}
/1/:param1/5/:param2/10/:param3/1/2/3/4/5/6/7/8/9/10/11/12Yes{param1:"2/3/4", param2:"6/7/8/9", param3:"11/12"}## Params
```go
func(w http.ResponseWriter, r *http.Request) {
params := mux.GetParams(r)
...
```## Example
```go
package mainimport (
"fmt"
"log"
"net/http""github.com/neonxp/mux"
)func main() {
m := mux.New()middleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Middleware", "hello!")
next.ServeHTTP(w, r)
})
}m.Get("/head/:param1/middle/:param2/tail", func(writer http.ResponseWriter, reader *http.Request) {
params := mux.GetParams(reader)
mux.Error(mux.Plain(fmt.Sprintf("param1=%s, param2=%s", params["param1"], params["param2"]), writer), writer)
}, middleware)http.Handle("/", m)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}
```