fusion

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

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

Go to latest
Published: Oct 31, 2016 License: Apache-2.0 Imports: 2 Imported by: 0

README

Fusion

Build Status Code Climate GoDoc

Middleware chaining for valyala/fasthttp. Works for both:

  1. Traditional Middlewares: func Middleware(h RequestHandler) RequestHandler
  2. Pure RequestHandler: func Handler(ctx *RequestCtx)

This library is very much inspired by [alice]

Usage

Sample code:

import (
    "github.com/valyala/fasthttp"
    "github.com/gofury/fusion"
)

func main() {
	router := furyrouter.New()
	
    router.GET("/admin", 
        // chain handlers is a simple function call
        fusion.Handlers(Handler1, Handler2, Handler2)
    )

    
    fasthttp.ListenAndServe(":8000", 
        // chain middleware requires a new struct
        fusion.New(Middleware1, Middleware2, Middleware3).Then(router)
    )
}

Documentation

Overview

package fusion provides chaing for both fasthttp RequestHandler and RequestHandler based Middleware

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handlers

Handlers acts as a simple function that

Types

type Middleware

type definition for Middleware that takes in a RequestHandler

type Middlewares

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

Abstraction for RequestHandler Middleware slice.

func New

func New(ms ...Middleware) *Middlewares

New creates a new Chain with given Middlewares Middlewares are only called upon a call to Then().

func (*Middlewares) Handler

Handler chains the middlewares and returns the final http.Handler.

New(m1, m2, m3).Handle(h)

is equivalent to:

m1(m2(m3(h)))

When the request comes in, it will be passed m1 -> m2 -> m3 -> handler (assuming every middleware calls the following one).

A chain can be safely reused by calling Handle() several times.

stdStack := fusion.New(ratelimitHandler, csrfHandler)
indexPipe = stdStack.Handler(indexHandler)
authPipe = stdStack.Handler(authHandler)

For proper middleware, this should cause no problems.

Handler() treats nil as fasthttprouter.New().Handler

Jump to

Keyboard shortcuts

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