mux

package module
Version: v0.0.0-...-90c3d0e Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2020 License: MIT Imports: 5 Imported by: 8

README

mux docs

Request routers.

  1. Route based on method:

    http.Handle("/items", mux.Method{
      "GET": getHandler,
      "PUT": putHandler,
    })
    

    If no match return a 405 Method Not Allowed. A default implementation of the OPTIONS method will return an Allow: ... header listing the defined methods.

  2. Route based on ContentType header:

    http.Handle("/items", mux.ContentType{
      "application/xml": xmlItemsGet,
      "application/json": jsonItemsGet,
      "*/*": itemsGet,
    })
    

    If no match return a 415 Unsupported Media Type.

  3. Route based on Accept header:

    http.Handle("/items", mux.Accept{
      "application/xml": xmlItemsPut,
      "application/json": jsonItemsPut,
      "application/x-www-form-urlencoded": itemsPut,
    })
    

    If no match return a 406 Not Acceptable.

Documentation

Overview

Package mux implements request routers.

Example
http.Handle("/items", Method{
	"GET": Accept{
		"application/xml":  XmlGetItemsHandler,
		"application/json": JsonGetItemsHandler,
	},
	"POST": ContentType{
		"application/xml":  XmlAddItemsHandler,
		"application/json": JsonAddItemsHandler,
	},
	"PUT": ContentType{
		"application/xml":  XmlEditItemsHandler,
		"application/json": JsonEditItemsHandler,
	},
})
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Accept

type Accept map[string]http.Handler

Accept maps Accept header values to different handlers. This will attempt to match the acceptable content type with the highest requested quality and greatest specificity. A fallback of */* can be specified which will always match if no others do, otherwise a 406 Not Acceptable response is returned.

Example
http.Handle("/items", Accept{
	"application/xml":  XmlItemsHandler,
	"application/json": JsonItemsHandler,
})
Output:

func (Accept) ServeHTTP

func (route Accept) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ContentType

type ContentType map[string]http.Handler

ContentType maps media-types to different handlers. Keys can contain wildcards, so application/* will be routed to for application/xml, application/json, etc. but only if there is no specific match. You can also define */* as a fallback handler, otherwise when no match is found a 415 Unsupported Media Type response is returned.

Example
http.Handle("/items", ContentType{
	"application/xml":  XmlItemsHandler,
	"application/json": JsonItemsHandler,
})
Output:

func (ContentType) ServeHTTP

func (route ContentType) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Method

type Method map[string]http.Handler

Method maps http methods to different handlers. If no match is found a 405 Method Not Allowed response is returned.

Example
http.Handle("/items", Method{
	"GET": GetItemsHandler,
	"PUT": PutItemsHandler,
})
Output:

func (Method) ServeHTTP

func (route Method) ServeHTTP(w http.ResponseWriter, r *http.Request)

Source Files

Jump to

Keyboard shortcuts

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