Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Interceptor ¶
Interceptor is a function that decorate handler with custom logic.
type ServeMux ¶
type ServeMux struct {
// contains filtered or unexported fields
}
ServeMux is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered patterns and calls the handler for the pattern that most closely matches the URL.
It support RESTful naming convention:
/users /users/action /users/:id /users/:id/comments /users/:id/comments/hide
func NewServeMux ¶
func NewServeMux(opts ServeMuxOpts) *ServeMux
NewServeMux allocates and returns a new ServeMux with default not found handler and context.
Example ¶
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"github.com/piotrkowalczuk/rmux"
"io/ioutil"
)
func main() {
mux := rmux.NewServeMux(rmux.ServeMuxOpts{})
mux.Handle("GET/user/deactivate", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusForbidden)
}))
mux.Handle("GET/user/:id", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
id := rmux.Params(r).Path.Get("id")
rw.WriteHeader(http.StatusOK)
io.WriteString(rw, `{"id": `+id+`}`)
}))
ts := httptest.NewServer(mux)
var (
res *http.Response
err error
pay []byte
)
if res, err = http.Get(ts.URL + "/user/9000"); err == nil {
defer res.Body.Close()
if pay, err = ioutil.ReadAll(res.Body); err == nil {
fmt.Println(string(pay))
}
}
if err != nil {
fmt.Println(err.Error())
}
}
Output: {"id": 9000}
type ServeMuxOpts ¶
type ServeMuxOpts struct {
NotFound http.Handler
Interceptor Interceptor
Context context.Context
}
ServeMuxOpts allow to pass extra options to the muxer.
Click to show internal directories.
Click to hide internal directories.