ware

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

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

Go to latest
Published: Jun 23, 2014 License: MIT Imports: 4 Imported by: 6

README

Ware wercker status GoDoc

Easily create middleware layer in Golang.
Forked from martini.
Dependence inject package.

Getting Started

// ware.go
package main

import (
        "log"
        "github.com/futurespace/ware"
)

func main() {
        w := ware.New()

        w.Use(func(c ware.Context, log *log.Logger) {
            log.Println("before")
            c.Next()
            log.Println("after")
        })

        w.Run()
}

Install the Ware package:

go get github.com/futurespace/ware

Run test:

go run ware.go

Compose Wares:

package main

import (
        "log"

        "github.com/codegangsta/inject"
        . "github.com/futurespace/ware"
)

type Builder struct {
        inject.Injector
        *Ware
}

func NewBuilder() *Builder {
        w := New()
        b := &Builder{inject.New(), w}
        b.Map(w)
        return b
}

type Packer struct {
        inject.Injector
        *Ware
}

func (p *Packer) Handle() {
        p.Run()
}

func NewPacker() *Packer {
        w := New()
        p := &Packer{inject.New(), w}
        p.Map(w)
        return p
}
b := NewBuilder()
b.Use(func (log *log.Logger) {
        log.Println("build...")
})
b.Run()

// Compose other Ware
p := NewPacker()
p.Use(func (log *log.Logger) {
        log.Println("pack...")
})
b.Action(p.Handle)
b.Run()

Mapping values to interface

type Deploy interface {
        Do()
}
type deploy struct{}
func (d *deploy) Do() {}
func NewDeploy() Deploy {
        return &deploy{}
}
type Compress struct {
        inject.Injector
        *Ware
        d Deploy
}
func NewCompress() *Compress {
        w := New()
        d := NewDeploy()
        w.MapTo(d, (*Deploy)(nil))
        c := &Compress{inject.New(), w, d}
        c.Map(w)
        return c
}


c := NewCompress()
c.Use(func(d Deploy) {
        fmt.Println(d)
})
c.Run()

Sets the output prefix for the logger. (Default [ware])

w.Use(func(log *log.Logger) {
        log.SetPrefix("[martini]")
})

API

New()

Creates a Ware instance.

Ware.Handlers(handlers ...Handler)

Sets middlewares.

Ware.Action(Handler)

Sets the handler that will be called after all the middleware has been invoked.

Ware.Use(Handler)

Adds a middleware.

Ware.Run()

Invokes the ware app.

Ware.CreateContext()

Creates a new context.
NOTE: In martini, this api is private, but sometime we need to hack the context!

Context.Out(o interface{})

Sets response instance.

Context.Next()
Context.Written()

Stops to invoke next middleware handler, the response instance responded.

Context.Run()

Invokes the context.

Others

See inject.

License

MIT

Documentation

Overview

Package ware is a powerful package for easily create middleware layer in Golang.

For a full guide visit https://github.com/futurespace/ware

package main

import (

"log"
"github.com/futurespace/ware"

)

func main() {
  w := ware.New()

  w.Use(func(c ware.Context, log *log.Logger) {
    log.Println("before")
    c.Next()
    log.Println("after")
  })

  w.Run()
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context interface {
	inject.Injector
	// Next is an optional function that Middleware Handlers can call to yield the until after
	// the other Handlers have been executed. This works really well for any operations that must
	// happen after a request
	Next()
	// Written returns whether or not the response for this context has been written.
	Written() bool
	// The response instance.
	Out(interface{})
}

Context represents a request context. Services can be mapped on the request level from this interface.

type Handler

type Handler interface{}

Handler can be any callable function. Martini attempts to inject services into the handler's argument list. Martini will panic if an argument could not be fullfilled via dependency injection.

type ReturnHandler

type ReturnHandler func(Context, []reflect.Value)

ReturnHandler is a service that Martini provides that is called when a route handler returns something. The ReturnHandler is responsible for writing to the ResponseWriter based on the values that are passed into this function.

type Ware

type Ware struct {
	inject.Injector
	// contains filtered or unexported fields
}

Middleware inject.Injector methods can be invoked to map services on a global level.

func New

func New() *Ware

New creates a base bones Ware instance. Use this method if you want to have full control over the middleware that is used.

func (*Ware) Action

func (w *Ware) Action(handler Handler)

Action sets the handler that will be called after all the middleware has been invoked. This is set to other Ware in a Ware.

func (*Ware) CreateContext

func (w *Ware) CreateContext() *context

Creates a context.

func (*Ware) Handlers

func (w *Ware) Handlers(handlers ...Handler)

Handlers sets the entire middleware stack with the given Handlers. This will clear any current middleware handlers. Will panic if any of the handlers is not a callable function

func (*Ware) Run

func (w *Ware) Run()

Run the ware.

func (*Ware) Use

func (w *Ware) Use(handler Handler)

Use adds a middleware Handler to the stack. Will panic if the handler is not a callable func. Middleware Handlers are invoked in the order that they are added

Jump to

Keyboard shortcuts

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