ehttprouter

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2016 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package ehttprouter wraps httprouter with an error management middleware.

Example (Panic)
package main

import (
	"log"
	"net/http"

	"github.com/creack/ehttp"
	"github.com/creack/ehttp/ehttprouter"
	"github.com/julienschmidt/httprouter"
)

func main() {
	hdlr := func(w http.ResponseWriter, req *http.Request, p httprouter.Params) error {
		panic(ehttp.NewErrorf(http.StatusTeapot, "fail"))
	}
	router := httprouter.New()
	router.GET("/", ehttprouter.MWErrorPanic(hdlr))
	log.Fatal(http.ListenAndServe(":8080", router))
}
Output:

Example (Simple)
package main

import (
	"log"
	"net/http"

	"github.com/creack/ehttp"
	"github.com/creack/ehttp/ehttprouter"
	"github.com/julienschmidt/httprouter"
)

func main() {
	hdlr := func(w http.ResponseWriter, req *http.Request, p httprouter.Params) error {
		return ehttp.NewErrorf(http.StatusTeapot, "fail")
	}
	router := httprouter.New()
	router.GET("/", ehttprouter.MWError(hdlr))
	log.Fatal(http.ListenAndServe(":8080", router))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultRouter = &Router{
	Router:       httprouter.New(),
	mux:          ehttp.DefaultServeMux,
	recoverPanic: false,
}

DefaultRouter is the default router for direct access.

Functions

func MWError

func MWError(handle Handle) httprouter.Handle

MWError exposes the default router MWError method.

func MWErrorPanic

func MWErrorPanic(handle Handle) httprouter.Handle

MWErrorPanic exposes the default router MWErrorPanic method.

Types

type Handle

Handle wraps httprouter's Handle and extends it with an error return.

type Router added in v0.4.1

type Router struct {
	*httprouter.Router // Underlying httprouter.Router.
	// contains filtered or unexported fields
}

Router wraps *github.com/julienschmidt/httprouter.Router with error management.

func New added in v0.4.1

func New(sendErrorCallback func(ehttp.ResponseWriter, *http.Request, error), errorContentType string, recoverPanic bool, logger *log.Logger) *Router

New instantiates a new ehttprouter.Router.

func (*Router) DELETE added in v0.4.1

func (r *Router) DELETE(path string, handle Handle)

DELETE wraps underlying method.

func (*Router) GET added in v0.4.1

func (r *Router) GET(path string, handle Handle)

GET wraps underlying method.

func (*Router) HEAD added in v0.4.1

func (r *Router) HEAD(path string, handle Handle)

HEAD wraps underlying method.

func (*Router) Handle added in v0.4.1

func (r *Router) Handle(method, path string, handle Handle)

Handle wraps the httprouter Handle.

func (*Router) Handler added in v0.4.1

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

Handler exposes the httprouter Handler method. NOTE: does not handle erors nor panics.

func (*Router) HandlerFunc added in v0.4.1

func (r *Router) HandlerFunc(method string, path string, handler http.HandlerFunc)

HandlerFunc exposes the httprouter HandlerFunc method. NOTE: does not handle erors nor panics.

func (*Router) Lookup added in v0.4.1

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

Lookup exposes the httprouter Lookup method. NOTE: Return the wrapped handler.

func (*Router) MWError added in v0.4.1

MWError is the middleware taking care of the returned error.

func (*Router) MWErrorPanic added in v0.4.1

func (r *Router) MWErrorPanic(handle Handle) httprouter.Handle

MWErrorPanic wraps MWError and recovers from panic.

func (*Router) OPTIONS added in v0.4.1

func (r *Router) OPTIONS(path string, handle Handle)

OPTIONS wraps underlying method.

func (*Router) PATCH added in v0.4.1

func (r *Router) PATCH(path string, handle Handle)

PATCH wraps underlying method.

func (*Router) POST added in v0.4.1

func (r *Router) POST(path string, handle Handle)

POST wraps underlying method.

func (*Router) PUT added in v0.4.1

func (r *Router) PUT(path string, handle Handle)

PUT wraps underlying method.

func (*Router) PanicHandler added in v0.4.2

func (r *Router) PanicHandler()

PanicHandler is a place holder to disable unwanted access to the underlying field. Panic is handled via ehttprouter instead.

func (*Router) ServeFiles added in v0.4.1

func (r *Router) ServeFiles(path string, root http.FileSystem)

ServeFiles exposes the httprouter ServeFiles method. NOTE: does not handle errors nor panics.

func (*Router) ServeHTTP added in v0.4.1

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

ServeHTTP exposes the underlying router's http.Handler interface.

Jump to

Keyboard shortcuts

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