goMiddlewareChain

package module
v0.0.0-...-aacc5d2 Latest Latest
Warning

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

Go to latest
Published: May 19, 2017 License: MIT Imports: 3 Imported by: 1

README

goMiddlewareChain Build Status

This is an express.js-like-middleware-chain for julienschmidt's httprouter

You can write your own middleware, and chain this to a lot of other middlewares (logging, auth,...).

Getting started

Install goMiddlewareChain

go get github.com/TobiEiss/goMiddlewareChain

Your first API

Here a simple example with a simple Ping-Pong-Handler chained with a JSONResponseHandler (from templates).

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/julienschmidt/httprouter"
	"github.com/TobiEiss/goMiddlewareChain"
	"github.com/TobiEiss/goMiddlewareChain/templates"
)

// Ping return a simply pong
func Ping(response *goMiddlewareChain.Response, request *http.Request, params httprouter.Params) {
	// simply pong
	response.Status.Code = http.StatusOK
	response.Data = "pong"
}

func main() {
	router := httprouter.New()
	router.GET("/api/v0/ping", goMiddlewareChain.RequestChainHandler(templates.JSONResponseHandler, Ping))

	log.Fatal(http.ListenAndServe(":8080", router))
}

After running this code, run curl localhost:8080/api/v0/ping in a terminal. You will get the following:

{
    "data":"pong",
    "msg":"OK",
    "status":200
}

Isn't it cool?

restricted-requestChainHandler

In some cases you need a restriction to apply requestChain. For example an auth-restriction. You can use the RestrictedRequestChainHandler. If the RestrictHandler failed, the code doesn't pass the chain.

Same example with Auth:

package main

import (
	"log"
	"net/http"

	"github.com/TobiEiss/goMiddlewareChain"
	"github.com/TobiEiss/goMiddlewareChain/templates"
	"github.com/julienschmidt/httprouter"
)

// Ping return a simply pong
func Ping(response *goMiddlewareChain.Response, request *http.Request, params httprouter.Params) {
	// simply pong
	response.Status.Code = http.StatusOK
	response.Data = "pong"
}

func Auth(response *goMiddlewareChain.Response, request *http.Request, params httprouter.Params) bool {
	user := request.Header.Get("X-User")
	return user == "HomerSimpson"
}

func main() {
	router := httprouter.New()
	router.GET("/api/v0/ping", goMiddlewareChain.RestrictedRequestChainHandler(Auth, templates.JSONResponseHandler, Ping))

	log.Fatal(http.ListenAndServe(":8080", router))
}

Now run curl --header "X-User: HomerSimpson" localhost:8080/api/v0/ping in your terminal. You will get:

{
    "data":"pong",
    "msg":"OK",
    "status":200
}

If you run curl --header "X-User: BartSimpson" localhost:8080/api/v0/ping, you get:

{
	"msg":"failed by passing restrictHandler",
	"status":401
}

handler from templates

You need more handler? Just let us now this.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenericRequestChainHandler

func GenericRequestChainHandler(checkResponseOfEveryHandler bool, restrictHandler RestrictContextHandler, responseHandler ResponseHandler, handlers ...ContextHandler) httprouter.Handle

GenericRequestChainHandler is the generic requestChainHandler.

checkResponseOfEveryHandler -> if true: check the responseCode after every handler. If httpStatus is != 0 or 200, stop to move on. restrictHandler -> if != nil: pass first this handler. Use this for example for AuthHandler responseHandler -> if != nil: pass at last this handler. Use this for example for a JSON-converter handlers -> all your handler-chain or middlewares or or or

func RequestChainContextHandler

func RequestChainContextHandler(responseHandler ResponseHandler, handlers ...ContextHandler) httprouter.Handle

RequestChainContextHandler chains all handler

func RequestChainHandler

func RequestChainHandler(responseHandler ResponseHandler, handlers ...Handler) httprouter.Handle

RequestChainHandler chains all handler

func RequestChainHandlerWithResponseCheck

func RequestChainHandlerWithResponseCheck(checkResponseOfEveryHandler bool, responseHandler ResponseHandler, handlers ...Handler) httprouter.Handle

RequestChainHandlerWithResponseCheck chains all handler and check every response

func RestrictedRequestChainContextHandler

func RestrictedRequestChainContextHandler(restrictHandler RestrictContextHandler, responseHandler ResponseHandler, handlers ...ContextHandler) httprouter.Handle

RestrictedRequestChainContextHandler need a RestrictHandler. A RestrictHandler returns bool if call is allowed.

func RestrictedRequestChainContextHandlerWithResponseCheck

func RestrictedRequestChainContextHandlerWithResponseCheck(checkResponseOfEveryHandler bool, restrictHandler RestrictContextHandler, responseHandler ResponseHandler, handlers ...ContextHandler) httprouter.Handle

RestrictedRequestChainContextHandlerWithResponseCheck exec all handlers If checkResponseOfEveryHandler is true, handler check every response.

func RestrictedRequestChainHandler

func RestrictedRequestChainHandler(restrictHandler RestrictHandler, responseHandler ResponseHandler, handlers ...Handler) httprouter.Handle

RestrictedRequestChainHandler need a RestrictHandler. A RestrictHandler returns bool if call is allowed.

func RestrictedRequestChainHandlerWithResponseCheck

func RestrictedRequestChainHandlerWithResponseCheck(checkResponseOfEveryHandler bool, restrictHandler RestrictHandler, responseHandler ResponseHandler, handlers ...Handler) httprouter.Handle

RestrictedRequestChainHandlerWithResponseCheck need a RestrictHandler. If checkResponseOfEveryHandler is true, handler check every response.

Types

type ContextHandler

ContextHandler a handler with the go-lang-context

type ContextKey

type ContextKey struct {
	Key string
}

ContextKey to map ContextValues

type Handler

type Handler func(*Response, *http.Request, httprouter.Params)

Handler represent a chainable Handler (middleware-like)

type Response

type Response struct {
	Data   interface{}
	Status Status
}

Response struct for Handlers

type ResponseHandler

type ResponseHandler func(*Response, http.ResponseWriter, *http.Request, httprouter.Params)

ResponseHandler required for every Endpoint

type RestrictContextHandler

type RestrictContextHandler func(context.Context, *Response, *http.Request, httprouter.Params) (context.Context, bool)

RestrictContextHandler restrict handler for contextHandler

type RestrictHandler

type RestrictHandler func(*Response, *http.Request, httprouter.Params) bool

RestrictHandler restricts to handle following handlers

type Status

type Status struct {
	Code    int
	Message string
}

Status represents the handler status

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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