Skip to content

Generalized middleware implementation for GoLang.

Notifications You must be signed in to change notification settings

jcarley/middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Middleware

Build Status

Middleware provides a convenient way to chain generic middleware functions.

In short, it transforms

Middleware1(Middleware2(Middleware3(App)))

to

middleware.New(Middleware1, Middleware2, Middleware3)

Why?

There are several middleware solutions out there, but all that I've found deal primarily with HTTP. I wanted something where I could use generic non-HTTP specific middleware components. This project is influenced by https://github.com/mitchellh/middleware and https://github.com/codegangsta/negroni

Usage

Install the package with

go get github.com/jcarley/middleware

Then import

import "github.com/jcarley/middleware"

Your middleware handlers should have the form of

func(env map[string]interface{}, next middleware.HandlerFunc)

Example

package main

import (
    "github.com/jcarley/middleware"
)

func getFileSize(env map[string]interface{}, next middleware.HandlerFunc) {
  // ... do some work
  env["fileSize"] = 1000
  next(env)
}

func doSomethingElse(env map[string]interface{}, next middleware.HandlerFunc) {
  // ... do some other work
  env["newResult"] = "successful"
  next(env)
}

func doWork(env map[string]interface{}, next middleware.HandlerFunc) {
  initialState := env["initialState"]
  newResult := env["newResult"];

  // ... log initialState and newResult

  next(env)
}

func main() {

    env := make(map[string]interface{})
    env["initialState"] = "somevalue"

    chain := middleware.New()
    chain.UseFunc(getFileSizeHandler)
    chain.UseFunc(doSomethingElseHandler)
    chain.UseFunc(doWork)
    chain.Call(env)
}

About

Generalized middleware implementation for GoLang.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages