Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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 main

import (
"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)
}
}
```