mixmux

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2019 License: MIT Imports: 4 Imported by: 2

README

mixmux

go get "github.com/codemodus/mixmux"

Package mixmux wraps HTTPRouter and HTTPTreeMux to provide consistent and idiomatic APIs, along with route grouping. Multiplexer-based parameter handling is bypassed.

Usage

type Mux
type Options
type Router
    func NewRouter(opts *Options) *Router
    func (m *Router) CORSMethods(path string, handlerWrappers ...func(http.Handler) http.Handler)
    func (m *Router) Connect(path string, h http.Handler)
    func (m *Router) Delete(path string, h http.Handler)
    func (m *Router) Get(path string, h http.Handler)
    func (m *Router) Group(path string) *Router
    func (m *Router) GroupMux(path string) Mux
    func (m *Router) Head(path string, h http.Handler)
    func (m *Router) Options(path string, h http.Handler)
    func (m *Router) Patch(path string, h http.Handler)
    func (m *Router) Post(path string, h http.Handler)
    func (m *Router) Put(path string, h http.Handler)
    func (m *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)
    func (m *Router) Trace(path string, h http.Handler)
type TreeMux
    func NewTreeMux(opts *Options) *TreeMux
    func (m *TreeMux) CORSMethods(path string, handlerWrappers ...func(http.Handler) http.Handler)
    func (m *TreeMux) Connect(path string, h http.Handler)
    func (m *TreeMux) Delete(path string, h http.Handler)
    func (m *TreeMux) Get(path string, h http.Handler)
    func (m *TreeMux) Group(path string) *TreeMux
    func (m *TreeMux) GroupMux(path string) Mux
    func (m *TreeMux) Head(path string, h http.Handler)
    func (m *TreeMux) Options(path string, h http.Handler)
    func (m *TreeMux) Patch(path string, h http.Handler)
    func (m *TreeMux) Post(path string, h http.Handler)
    func (m *TreeMux) Put(path string, h http.Handler)
    func (m *TreeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
    func (m *TreeMux) Trace(path string, h http.Handler)
Setup
import (
    "net/http"
    "net/http/httptest"

    "github.com/codemodus/mixmux"
)

func main() {
    handler := http.HandlerFunc(methodHandler)

    mux := mixmux.NewRouter()
    mux.Get("/get", handler)
    mux.Post("/post", handler)

    muxGroup := mux.Group("/grouped")
    muxGroup.Get("/get0", handler) // path = "/grouped/get0"
    muxGroup.Get("/get1", handler) // path = "/grouped/get1"

    // ...
}

More Info

Why is multiplexer-based parameter handling bypassed?

Multiplexer-based parameter handling is bypassed in favor of a single solution that is multiplexer-agnostic. Please review codemodus/parth for a simple and effective package covering this need.

Documentation

View the GoDoc

Benchmarks

These results demonstrate that mixmux does not increase resource usage. The digit suffix indicates how many named parameters are used. http.ServeMux is included for reference.

benchmark                       iter      time/iter   bytes alloc         allocs
---------                       ----      ---------   -----------         ------
BenchmarkHTTPServeMux0        200000    55.55 μs/op     3547 B/op   54 allocs/op
BenchmarkHTTPTreeMux2         200000    55.57 μs/op     3879 B/op   56 allocs/op
BenchmarkHTTPRouter2          200000    54.16 μs/op     3600 B/op   55 allocs/op
BenchmarkMixmuxTreeMux2       200000    55.26 μs/op     3869 B/op   56 allocs/op
BenchmarkMixmuxRouter2        200000    54.75 μs/op     3593 B/op   55 allocs/op
BenchmarkMixmuxRouterGroup2   200000    54.48 μs/op     3592 B/op   55 allocs/op

Documentation

Overview

Package mixmux wraps HTTPRouter and HTTPTreeMux to provide consistent and idiomatic APIs, along with route grouping. Multiplexer-based parameter handling is bypassed.

Example
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/httptest"

	"github.com/codemodus/mixmux"
)

const (
	Get  = "GET"
	Post = "POST"

	Head = "HEAD"
)

func main() {
	handler := http.HandlerFunc(methodHandler)

	mux := mixmux.NewRouter(nil)
	mux.Get("/get", handler)
	mux.Post("/post", handler)

	muxGroup := mux.Group("/grouped")
	muxGroup.Get("/get0", handler)
	muxGroup.Get("/get1", handler)

	server := httptest.NewServer(mux)

	rBody0, err := getReqBody(server.URL+"/get", Get)
	if err != nil {
		fmt.Println(err)
	}

	rBody1, err := getReqBody(server.URL+"/post", Post)
	if err != nil {
		fmt.Println(err)
	}

	rBodyGrouped0, err := getReqBody(server.URL+"/grouped/get0", Get)
	if err != nil {
		fmt.Println(err)
	}

	rBodyGrouped1, err := getReqBody(server.URL+"/grouped/get1", Get)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("Get Body:", rBody0)
	fmt.Println("Post Body:", rBody1)
	fmt.Println("Grouped Bodies:", rBodyGrouped0, rBodyGrouped1)

}

func getReqBody(url, method string) (string, error) {
	r, err := http.NewRequest(method, url, nil)
	cl := &http.Client{}
	resp, err := cl.Do(r)
	if err != nil {
		return "", err
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}
	if len(body) == 0 {
		body = []byte(resp.Header.Get(Head))
	}
	_ = resp.Body.Close()
	return string(body), nil
}

func methodHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method == Head {
		w.Header().Add(Head, Head)
	}
	_, _ = w.Write([]byte(r.Method))
	return
}
Output:

Get Body: GET
Post Body: POST
Grouped Bodies: GET GET

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mux

type Mux interface {
	http.Handler

	GroupMux(path string) Mux
	Any(path string, h http.Handler)
	Options(path string, h http.Handler)
	Get(path string, h http.Handler)
	Post(path string, h http.Handler)
	Put(path string, h http.Handler)
	Patch(path string, h http.Handler)
	Delete(path string, h http.Handler)
	Head(path string, h http.Handler)
	Trace(path string, h http.Handler)
	Connect(path string, h http.Handler)
	CORSMethods(path string, handlerWrappers ...func(http.Handler) http.Handler)
}

Mux ...

type Options

type Options struct {
	RedirectTrailingSlash  bool
	RedirectFixedPath      bool
	HandleMethodNotAllowed bool
	NotFound               http.Handler
	MethodNotAllowed       http.Handler
}

Options holds available options for a new Router.

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router wraps HTTPRouter.

func NewRouter

func NewRouter(opts *Options) *Router

NewRouter returns a wrapped HTTPRouter.

func (*Router) Any added in v0.2.0

func (m *Router) Any(path string, h http.Handler)

Any takes a path and http.Handler and adds them to the mux.

func (*Router) CORSMethods

func (m *Router) CORSMethods(path string, handlerWrappers ...func(http.Handler) http.Handler)

CORSMethods ... TODO:

func (*Router) Connect

func (m *Router) Connect(path string, h http.Handler)

Connect takes a path and http.Handler and adds them to the mux.

func (*Router) Delete

func (m *Router) Delete(path string, h http.Handler)

Delete takes a path and http.Handler and adds them to the mux.

func (*Router) Get

func (m *Router) Get(path string, h http.Handler)

Get takes a path and http.Handler and adds them to the mux.

func (*Router) Group

func (m *Router) Group(path string) *Router

Group takes a path and returns a new Router wrapping the original Router.

func (*Router) GroupMux

func (m *Router) GroupMux(path string) Mux

GroupMux takes a path and returns a new Router wrapping the original Router.

func (*Router) Head

func (m *Router) Head(path string, h http.Handler)

Head takes a path and http.Handler and adds them to the mux.

func (*Router) Options

func (m *Router) Options(path string, h http.Handler)

Options takes a path and http.Handler and adds them to the mux.

func (*Router) Patch

func (m *Router) Patch(path string, h http.Handler)

Patch takes a path and http.Handler and adds them to the mux.

func (*Router) Post

func (m *Router) Post(path string, h http.Handler)

Post takes a path and http.Handler and adds them to the mux.

func (*Router) Put

func (m *Router) Put(path string, h http.Handler)

Put takes a path and http.Handler and adds them to the mux.

func (*Router) ServeHTTP

func (m *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP satisfies the http.Handler interface.

func (*Router) Trace

func (m *Router) Trace(path string, h http.Handler)

Trace takes a path and http.Handler and adds them to the mux.

type TreeMux

type TreeMux struct {
	// contains filtered or unexported fields
}

TreeMux wraps HTTPTreeMux.

func NewTreeMux

func NewTreeMux(opts *Options) *TreeMux

NewTreeMux returns a wrapped HTTPTreeMux.

func (*TreeMux) Any added in v0.2.0

func (m *TreeMux) Any(path string, h http.Handler)

Any takes a path and http.Handler and adds them to the mux.

func (*TreeMux) CORSMethods

func (m *TreeMux) CORSMethods(path string, handlerWrappers ...func(http.Handler) http.Handler)

CORSMethods ... TODO:

func (*TreeMux) Connect

func (m *TreeMux) Connect(path string, h http.Handler)

Connect takes a path and http.Handler and adds them to the mux.

func (*TreeMux) Delete

func (m *TreeMux) Delete(path string, h http.Handler)

Delete takes a path and http.Handler and adds them to the mux.

func (*TreeMux) Get

func (m *TreeMux) Get(path string, h http.Handler)

Get takes a path and http.Handler and adds them to the mux.

func (*TreeMux) Group

func (m *TreeMux) Group(path string) *TreeMux

Group takes a path and returns a new TreeMux wrapping the original TreeMux.

func (*TreeMux) GroupMux

func (m *TreeMux) GroupMux(path string) Mux

GroupMux takes a path and returns a new TreeMux wrapping the original TreeMux.

func (*TreeMux) Head

func (m *TreeMux) Head(path string, h http.Handler)

Head takes a path and http.Handler and adds them to the mux.

func (*TreeMux) Options

func (m *TreeMux) Options(path string, h http.Handler)

Options takes a path and http.Handler and adds them to the mux.

func (*TreeMux) Patch

func (m *TreeMux) Patch(path string, h http.Handler)

Patch takes a path and http.Handler and adds them to the mux.

func (*TreeMux) Post

func (m *TreeMux) Post(path string, h http.Handler)

Post takes a path and http.Handler and adds them to the mux.

func (*TreeMux) Put

func (m *TreeMux) Put(path string, h http.Handler)

Put takes a path and http.Handler and adds them to the mux.

func (*TreeMux) ServeHTTP

func (m *TreeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP satisfies the http.Handler interface.

func (*TreeMux) Trace

func (m *TreeMux) Trace(path string, h http.Handler)

Trace takes a path and http.Handler and adds them to the mux.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL