compose

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

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

Go to latest
Published: Nov 29, 2016 License: MIT Imports: 3 Imported by: 0

README

compose

Build Status Coverage Status

Compose your http middlewares with happiness.

Installation

go get -u github.com/go-http-utils/compose

Documentation

Documentation can be found here: https://godoc.org/github.com/go-http-utils/compose

Usage

It transforms:

mux := http.NewServeMux()

mux.HandleFunc("/", middleware1(middleware2(middleware3(handler1, arg3)), arg1, arg2))
mux.HandleFunc("/example", middleware3(middleware4(handler2, arg2, arg3), arg1))

http.ListenAndServe(":8080", middleware4(middleware5(mux)))

to:

mux := http.NewServeMux()

mux.HandleFunc("/", compose.New(handler1).
  Use(middleware1, arg1, arg2).
  Use(middleware2).
  Use(middleware3, arg3).
  Handler())

mux.HandleFunc("/example", compose.New(handler2).
  UseMiddlewares([]Middleware{
    Middleware{Func: middleware3, Opts: []interface{arg1}},
    Middleware{Func: middleware4, Opts: []interface{arg2, arg3}},
  }).
  Handler())

http.ListenAndServe(":8080", compose.New(mux).
  Use(middleware4).
  Use(middleware5).
  Handler()
)

Documentation

Overview

Example
package main

import (
	"net/http"

	"github.com/go-http-utils/compose"
)

var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

const (
	middleware1 = iota
	middleware2
	middleware3
	middleware4
	middleware5
	arg1
	arg2
	arg3
	arg4
	arg5
)

func main() {
	mux := http.NewServeMux()

	mux.Handle("/example1", compose.New(handler).
		Use(middleware1, arg1, arg2).
		Use(middleware2, arg3).
		Use(middleware3).
		Handler())

	mux.Handle("/example2", compose.New(handler).
		UseMiddlewares([]compose.Middleware{
			compose.Middleware{Fun: middleware3, Opts: []interface{}{arg4, arg5}},
			compose.Middleware{Fun: middleware4},
		}).
		Handler())

	http.ListenAndServe(":8080", compose.New(mux).Use(middleware5).Handler())
}

Index

Examples

Constants

View Source
const Version = "0.1.0"

Version is this package's version

Variables

This section is empty.

Functions

This section is empty.

Types

type Composer

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

Composer wraps the inner http.Handler and can be used to append middlewares to it.

func New

func New(h http.Handler) *Composer

New returns a new Composer instance.

func (Composer) Handler

func (c Composer) Handler() http.Handler

Handler returns the inner handler.

func (*Composer) Use

func (c *Composer) Use(fun interface{}, opts ...interface{}) *Composer

Use appends the middleware to the inner handler. Accept the middleware function as the first argument and the first argument of the middleware function should be a http.Handler and the return value should also be http.Handler ( middlewareFunc(h http.Handler, ...) http.Handler ).

Example
package main

import (
	"net/http"

	"github.com/go-http-utils/compose"
)

var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

const (
	middleware1 = iota
	middleware2
	middleware3
	middleware4
	middleware5
	arg1
	arg2
	arg3
	arg4
	arg5
)

func main() {
	mux := http.NewServeMux()

	mux.Handle("/example1", compose.New(handler).
		Use(middleware1, arg1, arg2).
		Use(middleware2, arg3).
		Use(middleware3).
		Handler())

	http.ListenAndServe(":8080", compose.New(mux).Use(middleware5).Handler())
}

func (*Composer) UseMiddlewares

func (c *Composer) UseMiddlewares(ms []Middleware) *Composer

UseMiddlewares appends the specfied middlewares to the inner handler.

Example
package main

import (
	"net/http"

	"github.com/go-http-utils/compose"
)

var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

const (
	middleware1 = iota
	middleware2
	middleware3
	middleware4
	middleware5
	arg1
	arg2
	arg3
	arg4
	arg5
)

func main() {
	mux := http.NewServeMux()

	mux.Handle("/example2", compose.New(handler).
		UseMiddlewares([]compose.Middleware{
			compose.Middleware{Fun: middleware3, Opts: []interface{}{arg4, arg5}},
			compose.Middleware{Fun: middleware4},
		}).
		Handler())

	http.ListenAndServe(":8080", compose.New(mux).Use(middleware5).Handler())
}

type Middleware

type Middleware struct {
	// Func is the middleware function. The first argument of the
	// function should be a http.Handler and the return value should also be a
	// http.Handler ( middlewareFunc(h http.Handler, ...) http.Handler ).
	Fun interface{}
	// Opts are the arguments to pass to the middleware function.
	Opts []interface{}
}

Middleware includes the middleware function and the arguments to be passed.

Jump to

Keyboard shortcuts

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