Skip to content
This repository has been archived by the owner on Aug 25, 2018. It is now read-only.
/ httpmux Public archive

Golang HTTP request multiplexer that enables router nesting and middleware stacking

License

Notifications You must be signed in to change notification settings

gbrlsnchs/httpmux

Repository files navigation

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)

package httpmux_test

import (
	"log"
	"net/http"

	"github.com/gbrlsnchs/httpmux"
	"github.com/gbrlsnchs/httpmux/internal"
)

func Example() {
	rt := httpmux.NewRouter()

	rt.SetParamsKey(internal.ParamsKey)
	rt.HandleMiddlewares(http.MethodGet, "/:path",
		// Logger.
		func(w http.ResponseWriter, r *http.Request) {
			log.Printf("r.URL.Path = %s\n", r.URL.Path)
		},
		// Guard.
		func(w http.ResponseWriter, r *http.Request) {
			if params, ok := r.Context().Value(internal.ParamsKey).(map[string]string); ok {
				if params["path"] == "forbidden" {
					w.WriteHeader(http.StatusForbidden)
					httpmux.Cancel(r)
				}

				return
			}

			httpmux.Cancel(r)
		},
		// Handler.
		func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(http.StatusOK)
		},
	)

	http.ListenAndServe("/", rt)
}

Contribution

How to help:

  • Pull Requests
  • Issues
  • Opinions

About

Golang HTTP request multiplexer that enables router nesting and middleware stacking

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published