gonion

package module
v0.0.0-...-6f3959b Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2014 License: Apache-2.0 Imports: 2 Imported by: 0

README

Gonion

Build Status Coverage Status GoDoc

Gonion is a library for composing your routes and middleware. It is NOT a full-fledged framework, but rather allows you to compose things in a similar way that a framework might provide. Even though "idiomatic" go seems to encourage repeating yourself over and over again Gonion stays idiomatic without the repetition and loss of expressiveness.

Gonion has 0 reflection dependencies and adds 0 runtime alloc's to your routes and middleware.

Gonion is extremely fast!

Getting Started

Assuming you have installed gonion via go get github.com/CoreyKaylor/gonion

package main

import (
  "net/http"

  "github.com/CoreyKaylor/gonion"
	"github.com/julienschmidt/httprouter"
)

func main() {
	g := gonion.New()
	g.Get("/hello", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		rw.Write([]byte("Hello World!"))
	}))
	routes := g.BuildRoutes()
	router := httprouter.New() //use router of your choice
	for _, route := range routes {
		router.Handle(route.Method, route.Pattern, func(rw http.ResponseWriter, r *http.Request, m map[string]string) {
			route.Handler.ServeHTTP(rw, r)
		})
	}
	http.ListenAndServe(":3000", router)
}

Then go run server.go

You should now be able to open your browser to http://localhost:3000/

Middleware

Gonion handlers and middleware all take the form of the standard 'net/http' http.Handler

g.Use(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request){
	rw.Write([]byte("middleware"))
}))

When you need to wrap the downstream chain, rather than adding new signatures beyond the http.Handler gonion uses the middleware constructor method or in gonion terms the 'ChainLink'.

type ChainLink func(http.Handler) http.Handler

An example usage of a ChainLink

func wrappingHandler(inner http.Handler) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		//useless example, but this would be a good place to wrap the writer for something like gzip compression
		rw.Write([]byte("wrapping"))
		inner.ServeHTTP(rw, r)
	})
}

//and it's corresponding registration
g.Use().ChainLink(wrappingHandler)

Often it's useful to only apply middleware for 'POST' only routes. This removes the needless runtime checks for whether the current requests method is truly POST.

g.Only().Post().Use().ChainLink(wrappingHandler)

Typically middleware applies only to routes at a particular path.

g.Sub("/api", func(api *Composer){
	//middleware will only apply to routes with the prefix of /api
	api.Use().ChainLink(apiKeyHandler)
	api.Get("/users", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request){
		rw.Write([]byte("Full route is /api/users"))
	}))
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChainLink func(http.Handler) http.Handler

ChainLink is used when your http.Handler needs to wrap the rest of the handler chain.

type Composer

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

Composer is the main API in gonion and is responsible for composing the middleware and routes of your application

func New

func New() *Composer

New is a factory method for Composer

func (*Composer) BuildRoutes

func (composer *Composer) BuildRoutes() Routes

BuildRoutes returns routes with their corresponding handler chain. This is typically what you will call before delegating to the router you have chosen for your application.

func (*Composer) Delete

func (composer *Composer) Delete(pattern string, handler http.Handler)

Delete adds a route constrained to only 'DELETE' requests

func (*Composer) EachRoute

func (composer *Composer) EachRoute(router func(*Route))

EachRoute is a convenience method for BuildRoutes that you can pass a func to be called on each route from BuildRoutes.

func (*Composer) Get

func (composer *Composer) Get(pattern string, handler http.Handler)

Get adds a route constrained to only 'GET' requests

func (*Composer) Handle

func (composer *Composer) Handle(method string, pattern string, handler http.Handler)

Handle adds a route for the specified method and pattern

func (*Composer) Only

func (composer *Composer) Only() *RouteConstraint

Only allows you to constrain middleware for only certain types of routes. This isn't just a runtime filter, it excludes it from the chain while building the routes on startup.

func (*Composer) Patch

func (composer *Composer) Patch(pattern string, handler http.Handler)

Patch adds a route constrained to only 'PATCH' requests

func (*Composer) Post

func (composer *Composer) Post(pattern string, handler http.Handler)

Post adds a route constrained to only 'POST' requests

func (*Composer) Put

func (composer *Composer) Put(pattern string, handler http.Handler)

Put adds a route constrained to only 'PUT' requests

func (*Composer) Sub

func (composer *Composer) Sub(pattern string, sub func(*Composer))

Sub will allow you to specify middleware and routes that only apply for the specified path. This can be done recursively and at each level will inherit the previous path's middleware.

func (*Composer) Use

func (composer *Composer) Use() *MiddlewareOptions

Use is the entrypoint to adding middleware

type MiddlewareOptions

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

MiddlewareOptions is accessed from calling Use() and is how you specify which way to register a middleware handler.

func (mo *MiddlewareOptions) ChainLink(ctor func(http.Handler) http.Handler)

ChainLink is called when your middleware handler needs to wrap the rest of the handler chain.

func (*MiddlewareOptions) Func

func (mo *MiddlewareOptions) Func(handler func(http.ResponseWriter, *http.Request))

Func is a convenience method for a func that matches the signature of the standard http.HandlerFunc.

func (*MiddlewareOptions) Handler

func (mo *MiddlewareOptions) Handler(handler http.Handler)

Handler is middleware that conforms to the standard http.Handler interface

type Route

type Route struct {
	Method  string
	Pattern string
	Handler http.Handler
}

Route is the handler and route information after calling BuildRoutes. Handler is the entire chain of route handler and middleware.

type RouteConstraint

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

RouteConstraint is how middleware is constrained after calling Only()

func (*RouteConstraint) Delete

func (rc *RouteConstraint) Delete() *RouteConstraint

Delete constrains the middleware to only apply for 'DELETE' requests

func (*RouteConstraint) Get

func (rc *RouteConstraint) Get() *RouteConstraint

Get constrains the middleware to only apply for 'GET' requests

func (*RouteConstraint) Patch

func (rc *RouteConstraint) Patch() *RouteConstraint

Patch constrains the middleware to only apply for 'PATCH' requests

func (*RouteConstraint) Post

func (rc *RouteConstraint) Post() *RouteConstraint

Post constrains the middleware to only apply for 'POST' requests

func (*RouteConstraint) Put

func (rc *RouteConstraint) Put() *RouteConstraint

Put constrains the middleware to only apply for 'PUT' requests

func (*RouteConstraint) Use

Use is the entrypoint to defining your middleware, but only for the current defined route constraint

func (*RouteConstraint) When

func (rc *RouteConstraint) When(condition func() bool) *RouteConstraint

When is a constraint that gives you the option to only apply upon a condition being true

func (*RouteConstraint) WhenRouteMatches

func (rc *RouteConstraint) WhenRouteMatches(routeFilter func(*RouteModel) bool) *RouteConstraint

WhenRouteMatches is a constraint that gives you all the route information to filter upon.

type RouteModel

type RouteModel struct {
	Method  string
	Pattern string
	Handler http.Handler
}

RouteModel is the pre-build model representing a single handler without middleware.

type Routes

type Routes []*Route

Routes is the array of routes and built middleware. This will be what's returned after calling BuildRoutes

Directories

Path Synopsis
handlers
middleware

Jump to

Keyboard shortcuts

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