que

package
v0.0.0-...-4ecb35b Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2017 License: MIT Imports: 4 Imported by: 0

README

que

que allows middleware sequenced functionality for AppEngine Go services. Url params are made available, via the urlparams package, by using the Go context library that is already used within AppEngine.

Since context is not available via the http.Request until Go 1.7, and AppEngine usually lags behind in regards to the version of Go made available, this library fills the hole until then.

Example

import (
    "github.com/chrisolsen/ae/que"
)

func init() {
    r := httprouter.New()
    q := que.New(middleware1, middleware2)

    r.GET("/accounts/{id}", q.Then(accountHandler))
}

func middleware1(c context.Context, w http.ResponseWriter, r *http.Request) context.Context {
    // get context values
    foo := c.ValueOf("foo") 

    // set context param
    c2 := context.WithValue(c, "key", "some value")

    return c2
}

func accountHandler(c context.Context, w http.ResponseWriter, r *http.Request) {
    // access context values here the same as the middleware    
}

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

Types

type Handler

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

Handler much like the standard http.Handler, but includes the request context in the ServeHTTP method

type HandlerFunc

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

HandlerFunc much like the standard http.HandlerFunc, but includes the request context

type Middleware

Middleware is a http.HandlerFunc that also includes a context and url params variables

type Q

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

Q allows a list middleware functions to be created and run

func New

func New(ops ...Middleware) *Q

New initializes the middleware chain with one or more handler functions. The returned pointer allows for additional middleware methods to be added or for the chain to be run.

q := que.New(foo, bar)

func (*Q) Add

func (q *Q) Add(ops ...Middleware)

Add allows for one or more middleware handler functions to be added to the existing chain

q := que.New(cors, format)
q.Add(auth)

func (*Q) Handle

func (q *Q) Handle(h Handler) http.Handler

Handle accepts a Handler interface and returns the chain of existing middleware that includes the final Handler argument.

	q := que.New(foo, bar)
 router.Get("/", q.Handle(handleRoot))

func (*Q) HandleFunc

func (q *Q) HandleFunc(fn HandlerFunc) func(http.ResponseWriter, *http.Request)

HandleFunc returns the chain of existing middleware that includes the final HandlerFunc argument.

	q := que.New(foo, bar)
 router.Get("/", q.HandleFunc(handleRoot))

func (*Q) Run

func (q *Q) Run(c context.Context, w http.ResponseWriter, r *http.Request)

Run executes the handler chain, which is most useful in tests

	q := que.New(foo, bar)
	q.Add(func(c context.Context, w http.ResponseWriter, r *http.Request) {
		// perform tests here
	})
 inst := aetest.NewInstance(nil)
	r := inst.NewRequest("GET", "/", nil)
	w := httpTest.NewRecorder()
	c := appengine.NewContext(r)
	q.Run(c, w, r)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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