项目作者: gbrlsnchs

项目描述 :
Golang HTTP request multiplexer that enables router nesting and middleware stacking
高级语言: Go
项目地址: git://github.com/gbrlsnchs/httpmux.git
创建时间: 2017-09-25T20:07:39Z
项目社区:https://github.com/gbrlsnchs/httpmux

开源协议:MIT License

下载


DEPRECATED (and archived): use mux instead!

httpmux (HTTP request multiplexer)

Build Status
GoDoc

About

This package is an HTTP request multiplexer that enables router nesting
and middleware stacking for Go (or Golang) HTTP servers.

It uses standard approaches, such as the context package for retrieving
route parameters and short-circuiting middlewares, what makes it easy to be used
on both new and old projects, since it doesn’t present any new pattern.

Usage

Full documentation here.

Example (from example_test.go)

  1. package httpmux_test
  2. import (
  3. "log"
  4. "net/http"
  5. "github.com/gbrlsnchs/httpmux"
  6. "github.com/gbrlsnchs/httpmux/internal"
  7. )
  8. func Example() {
  9. rt := httpmux.NewRouter()
  10. rt.SetParamsKey(internal.ParamsKey)
  11. rt.HandleMiddlewares(http.MethodGet, "/:path",
  12. // Logger.
  13. func(w http.ResponseWriter, r *http.Request) {
  14. log.Printf("r.URL.Path = %s\n", r.URL.Path)
  15. },
  16. // Guard.
  17. func(w http.ResponseWriter, r *http.Request) {
  18. if params, ok := r.Context().Value(internal.ParamsKey).(map[string]string); ok {
  19. if params["path"] == "forbidden" {
  20. w.WriteHeader(http.StatusForbidden)
  21. httpmux.Cancel(r)
  22. }
  23. return
  24. }
  25. httpmux.Cancel(r)
  26. },
  27. // Handler.
  28. func(w http.ResponseWriter, r *http.Request) {
  29. w.WriteHeader(http.StatusOK)
  30. },
  31. )
  32. http.ListenAndServe("/", rt)
  33. }

Contribution

How to help:

  • Pull Requests
  • Issues
  • Opinions