httpmux

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2016 License: GPL-3.0 Imports: 9 Imported by: 0

README

HttpMux

HttpMux is a lightweight high performance HTTP request router (also called multiplexer or just mux for short) for Go.

Installation

go get github.com/niflheims-io/httpmux

Usage

    mux := httpmux.New()
    // set context
    mux.Ctx().Set("some key", "some interface")

	mux.Get("/1", func(r *httpmux.Request) {
	    // v, ok := r.Ctx().Get("some_key")
		r.Response().Status(http.StatusOK).String(time.Now().String())
		// also support json, xml, bytes, html/template..
	})

	mux.Get("/1/:k", func(r *httpmux.Request) {
		k := r.Query("k")
		r.Response().Status(http.StatusOK).String("[" + k + "]" + time.Now().String())
	})

    // also support http2
    srv := http.Server{
		Addr:        ":8080",
		Handler:     mux,
		ReadTimeout: 120 * time.Second,
	}

    fmt.Println(srv.ListenAndServe())

Status

  • Golang >= 1.6.2

License

GNU GENERAL PUBLIC LICENSE

Copyright (C) 2015-2016 niflheims-io

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanPath added in v1.0.0

func CleanPath(p string) string

Types

type Handle added in v1.0.0

type Handle func(*Request)

type Mux added in v1.0.0

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

func New

func New() *Mux

func (*Mux) Ctx added in v1.0.0

func (m *Mux) Ctx() *context

func (*Mux) Delete added in v1.0.0

func (m *Mux) Delete(path string, handle Handle)

func (*Mux) Get added in v1.0.0

func (m *Mux) Get(path string, handle Handle)

func (*Mux) Handle added in v1.0.0

func (m *Mux) Handle(method, path string, handle Handle)

func (*Mux) HandleMethodNotAllowed added in v1.0.0

func (m *Mux) HandleMethodNotAllowed(flag bool)

func (*Mux) Head added in v1.0.0

func (m *Mux) Head(path string, handle Handle)

func (*Mux) MethodNotAllowedHandler added in v1.0.0

func (m *Mux) MethodNotAllowedHandler(handle Handle)

func (*Mux) NotFoundHandler added in v1.0.0

func (m *Mux) NotFoundHandler(handle Handle)

func (*Mux) Options added in v1.0.0

func (m *Mux) Options(path string, handle Handle)

func (*Mux) PanicHandlerFunc added in v1.0.0

func (m *Mux) PanicHandlerFunc(handleFunc func(*Request, interface{}))

func (*Mux) Patch added in v1.0.0

func (m *Mux) Patch(path string, handle Handle)

func (*Mux) Put added in v1.0.0

func (m *Mux) Put(path string, handle Handle)

func (*Mux) RedirectFixedPath added in v1.0.0

func (m *Mux) RedirectFixedPath(flag bool)

func (*Mux) RedirectTrailingSlash added in v1.0.0

func (m *Mux) RedirectTrailingSlash(flag bool)

func (*Mux) ServeFiles added in v1.0.0

func (m *Mux) ServeFiles(path string, root http.FileSystem)

ServeFiles serves files from the given file system root. The path must end with "/*filepath", files are then served from the local path /defined/root/dir/*filepath. For example if root is "/etc" and *filepath is "passwd", the local file "/etc/passwd" would be served. Internally a http.FileServer is used, therefore http.NotFound is used instead of the Router's NotFound handler. To use the operating system's file system implementation, use http.Dir:

router.ServeFiles("/src/*filepath", http.Dir("/var/www"))

func (*Mux) ServeHTTP added in v1.0.0

func (m *Mux) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Mux) Template added in v1.0.0

func (m *Mux) Template(tpl *template.Template)

type Param added in v1.0.0

type Param struct {
	Key   string
	Value string
}

type Params added in v1.0.0

type Params []Param

func (Params) ByName added in v1.0.0

func (ps Params) ByName(name string) string

type Request added in v1.0.0

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

func (*Request) Ctx added in v1.0.0

func (r *Request) Ctx() *context

func (*Request) FormFile added in v1.0.0

func (r *Request) FormFile(key string) (*UploadFile, error)

func (*Request) FormValue added in v1.0.0

func (r *Request) FormValue(key string) string

func (*Request) HeadDel added in v1.0.0

func (r *Request) HeadDel(key string, val string)

func (*Request) HeadGet added in v1.0.0

func (r *Request) HeadGet(key string) string

func (*Request) HeadSet added in v1.0.0

func (r *Request) HeadSet(key string, val string)

func (*Request) HttpRequest added in v1.0.0

func (r *Request) HttpRequest() *http.Request

func (*Request) Query added in v1.0.0

func (r *Request) Query(key string) string

func (*Request) RawQuery added in v1.0.0

func (r *Request) RawQuery() string

func (*Request) Response added in v1.0.0

func (r *Request) Response() *Response

type Response added in v1.0.0

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

func (*Response) Gzip added in v1.0.0

func (r *Response) Gzip() *Response

func (*Response) Html added in v1.0.0

func (r *Response) Html(tplName string, o interface{}) error

func (*Response) Json added in v1.0.0

func (r *Response) Json(o interface{}) error

func (*Response) Status added in v1.0.0

func (r *Response) Status(status int) *Response

func (*Response) String added in v1.0.0

func (r *Response) String(o string) error

func (*Response) Write added in v1.0.0

func (r *Response) Write(data []byte, contentTypes []string) error

func (*Response) Xml added in v1.0.0

func (r *Response) Xml(o interface{}) error

type Router added in v1.0.0

type Router struct {

	// Enables automatic redirection if the current route can't be matched but a
	// handler for the path with (without) the trailing slash exists.
	// For example if /foo/ is requested but a route only exists for /foo, the
	// client is redirected to /foo with http status code 301 for GET requests
	// and 307 for all other request methods.
	RedirectTrailingSlash bool

	// If enabled, the router tries to fix the current request path, if no
	// handle is registered for it.
	// First superfluous path elements like ../ or // are removed.
	// Afterwards the router does a case-insensitive lookup of the cleaned path.
	// If a handle can be found for this route, the router makes a redirection
	// to the corrected path with status code 301 for GET requests and 307 for
	// all other request methods.
	// For example /FOO and /..//Foo could be redirected to /foo.
	// RedirectTrailingSlash is independent of this option.
	RedirectFixedPath bool

	// If enabled, the router checks if another method is allowed for the
	// current route, if the current request can not be routed.
	// If this is the case, the request is answered with 'Method Not Allowed'
	// and HTTP status code 405.
	// If no other Method is allowed, the request is delegated to the NotFound
	// handler.
	HandleMethodNotAllowed bool

	// Configurable http.HandlerFunc which is called when no matching route is
	// found. If it is not set, http.NotFound is used.
	NotFound Handle

	// Configurable http.HandlerFunc which is called when a request
	// cannot be routed and HandleMethodNotAllowed is true.
	// If it is not set, http.Error with http.StatusMethodNotAllowed is used.
	MethodNotAllowed Handle

	// Function to handle panics recovered from http handlers.
	// It should be used to generate a error page and return the http error code
	// 500 (Internal Server Error).
	// The handler can be used to keep your server from crashing because of
	// unrecovered panics.
	PanicHandler func(*Request, interface{})
	// contains filtered or unexported fields
}

Router is a http.Handler which can be used to dispatch requests to different handler functions via configurable routes

func (*Router) Lookup added in v1.0.0

func (r *Router) Lookup(method, path string) (Handle, Params, bool)

Lookup allows the manual lookup of a method + path combo. This is e.g. useful to build a framework around this router. If the path was found, it returns the handle function and the path parameter values. Otherwise the third return value indicates whether a redirection to the same path with an extra / without the trailing slash should be performed.

type UploadFile

type UploadFile struct {
	Data       []byte
	Name       string
	MIMEHeader textproto.MIMEHeader
}

Jump to

Keyboard shortcuts

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