Help us shape the future of Go! Take the 2026 Go Developer Survey

rmux

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2016 License: MIT Imports: 5 Imported by: 0

README

rmux GoDoc Build Status codecov.io

RESTful router, that supports path variables. Requires Go version 1.7+.

Example

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)

Benchmarks

rmux without context manipulation
BenchmarkRMUX_GithubAll               	   20000       	     85549 ns/op       	   12208 B/op  	     203 allocs/op
BenchmarkRMUX_GithubParam             	 3000000       	       434 ns/op       	      64 B/op  	       1 allocs/op
BenchmarkRMUX_GithubStatic            	 5000000       	       303 ns/op       	      32 B/op  	       1 allocs/op
BenchmarkRMUX_GPlusParam              	10000000       	       258 ns/op       	      32 B/op  	       1 allocs/op
BenchmarkRMUX_GPlusStatic             	20000000       	       129 ns/op       	      16 B/op  	       1 allocs/op
BenchmarkRMUX_Param                   	10000000       	       217 ns/op       	      32 B/op  	       1 allocs/op
BenchmarkRMUX_Param20                 	 1000000       	      1170 ns/op       	     320 B/op  	       1 allocs/op
BenchmarkRMUX_Param5                  	 5000000       	       377 ns/op       	      80 B/op  	       1 allocs/op
BenchmarkRMUX_ParamWrite              	 1000000       	      2514 ns/op       	     880 B/op  	       8 allocs/op
rmux with context manipulation
BenchmarkRMUX_GithubAll               	   10000       	    266757 ns/op       	   98224 B/op  	    1182 allocs/op
BenchmarkRMUX_GithubParam             	 1000000       	      1173 ns/op       	     496 B/op  	       6 allocs/op
BenchmarkRMUX_GithubStatic            	 2000000       	       818 ns/op       	     400 B/op  	       5 allocs/op
BenchmarkRMUX_GPlus2Params            	 1000000       	      1061 ns/op       	     496 B/op  	       6 allocs/op
BenchmarkRMUX_GPlusParam              	 2000000       	       865 ns/op       	     432 B/op  	       6 allocs/op
BenchmarkRMUX_GPlusStatic             	 2000000       	       653 ns/op       	     384 B/op  	       5 allocs/op
BenchmarkRMUX_Param                   	 2000000       	       999 ns/op       	     432 B/op  	       6 allocs/op
BenchmarkRMUX_Param20                 	 1000000       	      2451 ns/op       	    1008 B/op  	       6 allocs/op
BenchmarkRMUX_Param5                  	 1000000       	      1438 ns/op       	     528 B/op  	       6 allocs/op
BenchmarkRMUX_ParamWrite              	 1000000       	      1322 ns/op       	     432 B/op  	       6 allocs/op
httprouter
BenchmarkHttpRouter_GithubAll          	   30000       	     49574 ns/op       	   13792 B/op  	     167 allocs/op
BenchmarkHttpRouter_GithubParam        	 5000000       	       289 ns/op       	      96 B/op  	       1 allocs/op
BenchmarkHttpRouter_GithubStatic       	20000000       	        62.6 ns/op     	       0 B/op  	       0 allocs/op
BenchmarkHttpRouter_GPlus2Params       	10000000       	       215 ns/op       	      64 B/op  	       1 allocs/op
BenchmarkHttpRouter_GPlusParam         	10000000       	       225 ns/op       	      64 B/op  	       1 allocs/op
BenchmarkHttpRouter_GPlusStatic        	50000000       	        36.9 ns/op     	       0 B/op  	       0 allocs/op
BenchmarkHttpRouter_Param              	20000000       	       124 ns/op       	      32 B/op  	       1 allocs/op
BenchmarkHttpRouter_Param20            	 1000000       	      1208 ns/op       	     640 B/op  	       1 allocs/op
BenchmarkHttpRouter_Param5             	 5000000       	       409 ns/op       	     160 B/op  	       1 allocs/op
BenchmarkHttpRouter_ParamWrite         	10000000       	       165 ns/op       	      32 B/op  	       1 allocs/op
httptreemux
BenchmarkHttpTreeMux_GithubAll         	   10000       	    185566 ns/op       	   65856 B/op  	     671 allocs/op
BenchmarkHttpTreeMux_GithubParam       	 1000000       	      1086 ns/op       	     384 B/op  	       4 allocs/op
BenchmarkHttpTreeMux_GithubStatic      	20000000       	        66.7 ns/op     	       0 B/op  	       0 allocs/op
BenchmarkHttpTreeMux_GPlus2Params      	 2000000       	       969 ns/op       	     384 B/op  	       4 allocs/op
BenchmarkHttpTreeMux_GPlusParam        	 2000000       	       736 ns/op       	     352 B/op  	       3 allocs/op
BenchmarkHttpTreeMux_GPlusStatic       	50000000       	        39.7 ns/op     	       0 B/op  	       0 allocs/op
BenchmarkHttpTreeMux_Param             	 2000000       	       835 ns/op       	     352 B/op  	       3 allocs/op
BenchmarkHttpTreeMux_Param20           	  200000       	      9043 ns/op       	    3196 B/op  	      10 allocs/op
BenchmarkHttpTreeMux_Param5            	 1000000       	      1664 ns/op       	     576 B/op  	       6 allocs/op
BenchmarkHttpTreeMux_ParamWrite        	 2000000       	       812 ns/op       	     352 B/op  	       3 allocs/op

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Interceptor

type Interceptor func(http.ResponseWriter, *http.Request, http.Handler)

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}

func (*ServeMux) GoString

func (sm *ServeMux) GoString() string

GoString implements fmt GoStringer interface.

func (*ServeMux) Handle

func (sm *ServeMux) Handle(p string, h http.Handler)

Handle registers the handler for the given pattern.

func (*ServeMux) ServeHTTP

func (sm *ServeMux) ServeHTTP(rw http.ResponseWriter, r *http.Request)

ServeHTTP dispatches the request to the handler whose pattern most closely matches the request URL.

type ServeMuxOpts

type ServeMuxOpts struct {
	NotFound    http.Handler
	Interceptor Interceptor
	Context     context.Context
}

ServeMuxOpts allow to pass extra options to the muxer.

type Values

type Values struct {
	Path  url.Values
	Query url.Values
}

Values is a handy container for url.Values.

func Params

func Params(r *http.Request) *Values

Params allocates new Values based on given http Request object.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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