Documentation
¶
Index ¶
- Constants
- Variables
- func ConsoleLogger(ignorePaths ...string) rux.HandlerFunc
- func DefaultSkipper(_ *rux.Context) bool
- func DumpRoutesHandler() rux.HandlerFunc
- func GenRequestID(key string) rux.HandlerFunc
- func HTTPBasicAuth(accounts map[string]string) rux.HandlerFunc
- func HTTPMethodOverrideHandler(h http.Handler) http.Handler
- func IgnoreFavIcon() rux.HandlerFunc
- func PanicsHandler() rux.HandlerFunc
- func ParamRegex(name, pattern string) rux.HandlerFunc
- func Timeout(timeout time.Duration) rux.HandlerFunc
- type Skipper
Examples ¶
Constants ¶
const ( // HTTPMethodOverrideHeader is a commonly used http header to override a request method HTTPMethodOverrideHeader = "X-HTTP-Method-Override" // HTTPMethodOverrideFormKey is a commonly used HTML form key to override a request method HTTPMethodOverrideFormKey = "_method" // OriginalMethodContextKey record old original request method OriginalMethodContextKey contextKey = "originalMethod" )
const FavIcon = "/favicon.ico"
FavIcon uri for favicon.ico
Variables ¶
var IgnorePaths = []string{
"/health",
"/status",
}
var RequestLogger = ConsoleLogger
RequestLogger middleware. alias of ConsoleLogger
Functions ¶
func ConsoleLogger ¶
func ConsoleLogger(ignorePaths ...string) rux.HandlerFunc
ConsoleLogger middleware.
func DefaultSkipper ¶
DefaultSkipper returns false which processes the middleware.
func DumpRoutesHandler ¶
func DumpRoutesHandler() rux.HandlerFunc
DumpRoutesHandler dump all registered routes info
func HTTPBasicAuth ¶
func HTTPBasicAuth(accounts map[string]string) rux.HandlerFunc
HTTPBasicAuth for the request
Usage:
r.GET("/auth", func(c *rux.Context) {
c.WriteString("hello")
}, HTTPBasicAuth(map[string]string{"testuser": "123"}))
func HTTPMethodOverrideHandler ¶
HTTPMethodOverrideHandler wraps and returns a http.Handler which checks for the X-HTTP-Method-Override header or the _method form key, and overrides (if valid) request.Method with its value.
Refer from the https://github.com/gorilla/handlers
Example ¶
r := rux.New()
h := HTTPMethodOverrideHandler(r)
err := http.ListenAndServe(":8080", h)
if err != nil {
panic(err)
}
// can also:
h1 := r.WrapHTTPHandlers(HTTPMethodOverrideHandler)
goutil.PanicErr(http.ListenAndServe(":8080", h1))
func ParamRegex ¶ added in v2.1.0
func ParamRegex(name, pattern string) rux.HandlerFunc
ParamRegex returns a route middleware that requires the named path parameter to match pattern. It replaces the v1 inline constraint syntax (`{id:\d+}`), which v2 removed:
r.GET("/users/{id}", showUser, handlers.ParamRegex("id", `\d+`))
The pattern is matched against the whole parameter value: it is compiled as `^(?:pattern)$`, so `\d+` rejects "12abc". Writing the anchors yourself (`^\d+$`) behaves the same, so v1 patterns port over unchanged.
A missing or non-matching parameter aborts the request with 400 and a short text body. The pattern is compiled once, when the middleware is built, so an invalid pattern panics at registration time, like route registration does.
Use it per route, or on a group whose routes share the parameter:
r.Group("/users/{id}", func() {
r.GET("", showUser)
r.PUT("", updateUser)
}, handlers.ParamRegex("id", `\d+`))
For a custom failure response (JSON body, different status), write a small middleware of your own instead.
func Timeout ¶
func Timeout(timeout time.Duration) rux.HandlerFunc
Timeout is a middleware for handle logic. the method is referred from "github.com/go-chi/chi/middleware"
It's required that you select the ctx.Done() channel to check for the signal if the context has reached its deadline and return, otherwise the timeout signal will be just ignored.
a route/handler may look like:
r.GET("/long", func(c *rux.Context) {
ctx := c.Req.Context()
processTime := time.Duration(rand.Intn(4)+1) * time.Second
select {
case <-ctx.Done():
return
case <-time.After(processTime):
// The above channel simulates some hard work.
}
c.WriteBytes([]byte("done"))
})