wctx

package module
v0.0.0-...-183efbc Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2016 License: MIT Imports: 5 Imported by: 2

README

With ConTeXt

GoDoc

It's a github.com/julienschmidt/httprouter drop in replacement. Basically all methods from the original library are wrapped. It makes easy to us the httprouter on App Engine and outside of App Engine.

The problem with App Engine context is that it requires your app to hardcode the appengine.NewContext in your handlers. As stated in https://blog.golang.org/context inside Google's infrastructure they pass Context around. It's pretty useful for puting timeouts and other limits on a request and passing user permissions.

On App Engine the init code would look:

func SomeHandler(c context.Context, w http.ResponseWriter, r *http.Request) { /* some code */ }

func GaeContext(r *http.Request) context.Context {
	return appengine.NewContext(r)
}

func init() {
	r := New()
	r.ContextFactory = GaeContext
	r.GET("/page/:pageid/:mod", SomeHandler)
	http.Handler("", r)  // r may be replaced by r.R, the underlaying httprouter.
}

For more details please the doc of the original repo: GoDoc

The extra useful method indluce:

func (r *Router) WithMiddleware(m Middleware) *Router

the returned Router keeps the underlying httprouter.Router but extends the currently registered middlewares by passed generator.

Under the hood it keeps all added Middleware generators in list and builds the desired handle method when one adds a path to be handled.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultContextFactory

func DefaultContextFactory(req *http.Request) context.Context

func FromContext

func FromContext(c context.Context) (paramsHelper.Params, bool)

Types

type ContextProvider

type ContextProvider func(*http.Request) context.Context

type HandleFunc

type HandleFunc func(context.Context, http.ResponseWriter, *http.Request)

func (HandleFunc) ServeHTTP

func (h HandleFunc) ServeHTTP(c context.Context, w http.ResponseWriter, r *http.Request)

type Handler

type Handler interface {
	ServeHTTP(context.Context, http.ResponseWriter, *http.Request)
}

type Middleware

type Middleware func(HandleFunc) HandleFunc

type Router

type Router struct {
	R              *httprouter.Router
	ContextFactory ContextProvider
	// contains filtered or unexported fields
}

func New

func New() *Router

func (*Router) DELETE

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

func (*Router) GET

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

func (*Router) HEAD

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

func (*Router) Handle

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

func (*Router) Handler

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

func (*Router) HandlerFunc

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

func (*Router) Lookup

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

func (*Router) OPTIONS

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

func (*Router) PATCH

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

func (*Router) POST

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

func (*Router) PUT

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

func (*Router) ServeFiles

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

func (*Router) ServeHTTP

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

func (*Router) WithMiddleware

func (r *Router) WithMiddleware(m Middleware) *Router

Jump to

Keyboard shortcuts

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