way

package module
v0.0.0-...-9632d0c Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2018 License: MIT Imports: 3 Imported by: 50

README

Way

HTTP router for Go 1.7

  • Deliberately simple
  • Extremely fast
  • Route based on HTTP methods and path
  • Path parameters via Context (e.g. /music/:band/:song)
  • Trailing / matches path prefixes

Install

There's no need to add a dependency to Way, just copy way.go and way_test.go into your project, or drop them in:

drop github.com/matryer/way

If you prefer, it is go gettable:

go get github.com/matryer/way

Usage

  • Use NewRouter to make a new Router
  • Call Handle and HandleFunc to add handlers
  • Specify HTTP method and path pattern for each route
  • Use Param function to get the path parameters from the context
func main() {
	router := way.NewRouter()

	router.HandleFunc("GET", "/music/:band/:song", handleReadSong)
	router.HandleFunc("PUT", "/music/:band/:song", handleUpdateSong)
	router.HandleFunc("DELETE", "/music/:band/:song", handleDeleteSong)

	log.Fatalln(http.ListenAndServe(":8080", router))
}

func handleReadSong(w http.ResponseWriter, r *http.Request) {
	band := way.Param(r.Context(), "band")
	song := way.Param(r.Context(), "song")
	// use 'band' and 'song' parameters...
}
  • Prefix matching

To match any path that has a specific prefix, use the ... prefix indicator:

func main() {
	router := way.NewRouter()

	router.HandleFunc("GET", "/images...", handleImages)
	log.Fatalln(http.ListenAndServe(":8080", router))
}

In the above example, the following paths will match:

  • /images

  • /images/

  • /images/one/two/three.jpg

  • Set Router.NotFound to handle 404 errors manually

func main() {
	router := way.NewRouter()
	
	router.NotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNotFound)
		fmt.Fprintf(w, "This is not the page you are looking for")
	})
	
	log.Fatalln(http.ListenAndServe(":8080", router))
}

Why another HTTP router?

I know, I know. But no routers offer the simplicity of path parameters via Context, and HTTP method matching. Which covers 100% of my use cases so far.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Param

func Param(ctx context.Context, param string) string

Param gets the path parameter from the specified Context. Returns an empty string if the parameter was not found.

Types

type Router

type Router struct {

	// NotFound is the http.Handler to call when no routes
	// match. By default uses http.NotFoundHandler().
	NotFound http.Handler
	// contains filtered or unexported fields
}

Router routes HTTP requests.

func NewRouter

func NewRouter() *Router

NewRouter makes a new Router.

func (*Router) Handle

func (r *Router) Handle(method, pattern string, handler http.Handler)

Handle adds a handler with the specified method and pattern. Method can be any HTTP method string or "*" to match all methods. Pattern can contain path segments such as: /item/:id which is accessible via the Param function. If pattern ends with trailing /, it acts as a prefix.

func (*Router) HandleFunc

func (r *Router) HandleFunc(method, pattern string, fn http.HandlerFunc)

HandleFunc is the http.HandlerFunc alternative to http.Handle.

func (*Router) ServeHTTP

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

ServeHTTP routes the incoming http.Request based on method and path extracting path parameters as it goes.

Jump to

Keyboard shortcuts

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