mux

package module
v2.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2018 License: MIT Imports: 2 Imported by: 1

README

mux Build Status GoDoc

mux is a high performance HTTP request router, also called multiplexer or just mux.

Example

package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/pinub/mux"
)

func index() http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "Welcome!\n")
    })
}

func hello() http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "Hello\n")
    })
}

func main() {
    m := mux.New()
    m.Get("/", index())
    m.Get("/hello", hello())

    log.Fatal(http.ListenAndServe(":8080", m))
}

The Muxer matches incoming requests by the method and path and delegates to that assiciated function. Currently GET, POST, PUT, PATCH, DELETE and OPTIONS are supported methods.

Named parameters are not supported.

Path: /foo/bar

Requests:

    /foo/bar        matches the function
    /foo/bar/       doesn't match, but redirects to /foo/bar
    /foo/foo        doesn't match
    /foo            doesn't match

Acknowledge

Parts of the source are copied from Julien Schmidt famous httprouter. So parts of the code are Copyright (c) 2013 Julien Schmidt. All rights reserved.

Documentation

Overview

Package mux is a high performance HTTP request router, also called multiplexer or just mux.

Example:

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/pinub/mux"
)

func index() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Welcome!\n")
	})
}

func hello() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Hello\n")
	})
}

func main() {
	m := mux.New()
	m.Get("/", index())
	m.Get("/hello", hello())

	log.Fatal(http.ListenAndServe(":8080", m))
}

The Muxer matches incoming requests by the method and path and delegates to that assiciated handler. Currently GET, POST, PUT, PATCH, DELETE and OPTIONS are supported methods.

Named parameters are not supported.

Path: /foo/bar

Requests:

/foo/bar        matches the handler
/foo/bar/       doesn't match, but redirects to /foo/bar
/foo/foo        doesn't match
/foo            doesn't match

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Router

type Router struct {

	// Enables automatic redirection if the requested path doesn't match but
	// a handler with a path without the trailing slash exists. Default: true
	RedirectTrailingSlash bool

	// Enables 'Method Not Allowed' responses when a handler for the the
	// path, but not the requested method exists. Default: true
	HandleMethodNotAllowed bool

	// Custom http.Handler which is called when no handler was found for the
	// requested route. Defaults: http.NotFound.
	NotFound http.Handler
	// contains filtered or unexported fields
}

Router is a http.Handler used to dispatch request to different handlers.

func New

func New() *Router

New initializes the Router. All configurable options are enabled by default.

func (*Router) Delete

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

Delete registers a new request handle for the DELETE method and the given path.

func (*Router) Get

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

Get registers a new request handle for the GET method and the given path.

func (*Router) Options

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

Options registers a new request handle for the OPTIONS method and the given path.

func (*Router) Patch

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

Patch registers a new request handle for the PATCH method and the given path.

func (*Router) Post

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

Post registers a new request handle for the POST method and the given path.

func (*Router) Put

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

Put registers a new request handle for the PUT method and the given path.

func (*Router) ServeHTTP

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

ServeHTTP makes this router implement the http.Handler interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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