quincy

package module
v0.0.0-...-8df21d4 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2017 License: MIT Imports: 3 Imported by: 0

README

quincy

DEPRECATED: This repo has been merged into github.com/chrisolsen/ae

quincy 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/quincy"
    "github.com/chrisolsen/quincy/urlparams"
)

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

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

func middleware1(c context.Context, w http.ResponseWriter, r *http.Request) context.Context {
    // access url params with helper method
    id := urlparams.ByName("id") 

    // get context values
    foo := c.ValueOf("foo") 

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

    return c
}

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

This section is empty.

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(fns ...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(fns ...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.Then(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)

func (*Q) Then

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

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

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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