Giter Site home page Giter Site logo

ware's Introduction

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

ware's People

Contributors

fundon avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.