Skip to content

Commit

Permalink
Rename module
Browse files Browse the repository at this point in the history
  • Loading branch information
ariefrahmansyah committed Jan 27, 2018
1 parent 589e1db commit 3ec364d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 64 deletions.
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
# peacock [![GoDoc](https://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](https://godoc.org/github.com/ariefrahmansyah/peacock) [![CircleCI](https://circleci.com/gh/ariefrahmansyah/peacock/tree/master.png?style=shield)](https://circleci.com/gh/ariefrahmansyah/peacock/tree/master) [![Coverage Status](https://coveralls.io/repos/github/ariefrahmansyah/peacock/badge.svg?branch=master)](https://coveralls.io/github/ariefrahmansyah/peacock?branch=master) [![GoReportCard](https://goreportcard.com/badge/github.com/ariefrahmansyah/peacock)](https://goreportcard.com/report/github.com/ariefrahmansyah/peacock)
# nsqg [![GoDoc](https://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](https://godoc.org/github.com/ariefrahmansyah/nsqg) [![CircleCI](https://circleci.com/gh/ariefrahmansyah/nsqg/tree/master.png?style=shield)](https://circleci.com/gh/ariefrahmansyah/nsqg/tree/master) [![Coverage Status](https://coveralls.io/repos/github/ariefrahmansyah/nsqg/badge.svg?branch=master)](https://coveralls.io/github/ariefrahmansyah/nsqg?branch=master) [![GoReportCard](https://goreportcard.com/badge/github.com/ariefrahmansyah/nsqg)](https://goreportcard.com/report/github.com/ariefrahmansyah/nsqg)

Peacock is middleware for your NSQ consumer handler function. It's heavily inspired by negroni.

![Peacock](https://www.web-savvy-marketing.com/wp-content/uploads/2013/11/Be-the-Peacock.jpg)
Image source: https://www.web-savvy-marketing.com/2013/11/peacock/
NSQG is middleware for your NSQ consumer handler function. It's heavily inspired by [negroni](https://github.com/urfave/negroni).

## Usage
We can create new middleware object that implement nsq.Handler interface and use it in Peacock object using Use method.
We can create new middleware object that implement nsq.Handler interface and use it in NSQG object using Use method.

We also can use nsq.HandlerFunc that we already created before.

```go
// Create peacock object.
peacock := peacock.New()
// Create nsqg object.
nsqg := nsqg.New()

// Create middleware object that implement nsq.Handler interface.
type Middleware1 struct{}
Expand All @@ -22,7 +19,7 @@ func (m1 Middleware1) HandleMessage(message *nsq.Message, next nsq.HandlerFunc)
}

// Use Middleware1.
peacock.Use(Middleware1{})
nsqg.Use(Middleware1{})

// We may already have nsq.HandlerFunc.
func handlerFunc1(message *nsq.Message) error {
Expand All @@ -32,9 +29,9 @@ func handlerFunc1(message *nsq.Message) error {
}

// Let's use it too.
peacock.UseHandlerFunc(handlerFunc1)
nsqg.UseHandlerFunc(handlerFunc1)

consumer, _ := nsq.NewConsumer(topicName, channelName, nsq.NewConfig())
consumer.AddHandler(peacock)
consumer.AddHandler(nsqg)
consumer.ConnectToNSQD(nsqdAddress)
```
12 changes: 6 additions & 6 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"syscall"
"time"

"github.com/ariefrahmansyah/peacock"
"github.com/ariefrahmansyah/nsqg"
"github.com/nsqio/go-nsq"
)

Expand Down Expand Up @@ -54,17 +54,17 @@ func main() {
return nil
}

peacock := peacock.New()
peacock.Use(middleware1)
peacock.UseHandler(handler1)
peacock.UseHandlerFunc(handlerFunc1)
nsqg := nsqg.New()
nsqg.Use(middleware1)
nsqg.UseHandler(handler1)
nsqg.UseHandlerFunc(handlerFunc1)

consumer, err := nsq.NewConsumer(topic, channel, nsq.NewConfig())
if err != nil {
log.Fatalln(err)
}

consumer.AddHandler(peacock)
consumer.AddHandler(nsqg)

consumer.ConnectToNSQD(nsqd)

Expand Down
58 changes: 28 additions & 30 deletions peacock.go → nsqg.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package peacock
package nsqg

import (
"github.com/nsqio/go-nsq"
)
import "github.com/nsqio/go-nsq"

// Handler is an interface that objects can implement to be registered to serve as m
// in the Peacock m stack.
// HandleMessage should yield to the next m in the chain by invoking the next nsq.HandlerFunc
// Handler is an interface that objects can implement to be registered to serve as middleware
// in the NSQG middleware stack.
// HandleMessage should yield to the next middleware in the chain by invoking the next nsq.HandlerFunc
// passed in.
//
// If the Handler finishes the message, the next nsq.HandlerFunc is still be invoked.
type Handler interface {
HandleMessage(message *nsq.Message, next nsq.HandlerFunc) error
}

// WrapHandler converts a nsq.Handler into a peacock.Handler so it can be used as a Peacock
// m. The next nsq.HandlerFunc is automatically called after the Handler
// WrapHandler converts a nsq.Handler into a nsqg.Handler so it can be used as a NSQG
// middleware. The next nsq.HandlerFunc is automatically called after the Handler
// is executed.
func WrapHandler(handler nsq.Handler) Handler {
return HandlerFunc(func(message *nsq.Message, next nsq.HandlerFunc) error {
Expand All @@ -26,7 +24,7 @@ func WrapHandler(handler nsq.Handler) Handler {
})
}

// HandlerFunc is an adapter to allow the use of ordinary functions as Peacock handlers.
// HandlerFunc is an adapter to allow the use of ordinary functions as NSQG handlers.
// If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.
type HandlerFunc func(message *nsq.Message, next nsq.HandlerFunc) error

Expand Down Expand Up @@ -64,47 +62,47 @@ func emptyMiddleware() middleware {
}
}

// Peacock is a stack of Middleware Handlers that can be invoked as an nsq.Handler.
// Peacock middleware is evaluated in the order that they are added to the stack using
// NSQG is a stack of Middleware Handlers that can be invoked as an nsq.Handler.
// NSQG middleware is evaluated in the order that they are added to the stack using
// the Use, UseHandler and UseHandlerFunc methods.
type Peacock struct {
type NSQG struct {
handlers []Handler
middleware middleware
}

// New returns a new Peacock instance with no middleware preconfigured.
func New(handlers ...Handler) *Peacock {
return &Peacock{
// New returns a new NSQG instance with no middleware preconfigured.
func New(handlers ...Handler) *NSQG {
return &NSQG{
handlers: handlers,
middleware: buildMiddleware(handlers),
}
}

func (peacock Peacock) HandleMessage(message *nsq.Message) error {
return peacock.middleware.HandleMessage(message)
func (nsqg NSQG) HandleMessage(message *nsq.Message) error {
return nsqg.middleware.HandleMessage(message)
}

// Use adds a Handler onto the middleware stack. Handlers are invoked in the order they are added to a Peacock.
func (peacock *Peacock) Use(handler Handler) {
// Use adds a Handler onto the middleware stack. Handlers are invoked in the order they are added to a NSQG.
func (nsqg *NSQG) Use(handler Handler) {
if handler == nil {
panic("handler cannot be nil")
}

peacock.handlers = append(peacock.handlers, handler)
peacock.middleware = buildMiddleware(peacock.handlers)
nsqg.handlers = append(nsqg.handlers, handler)
nsqg.middleware = buildMiddleware(nsqg.handlers)
}

// UseFunc adds a Peacock-style handler function onto the middleware stack.
func (peacock *Peacock) UseFunc(handlerFunc func(message *nsq.Message, next nsq.HandlerFunc) error) {
peacock.Use(HandlerFunc(handlerFunc))
// UseFunc adds a NSQG-style handler function onto the middleware stack.
func (nsqg *NSQG) UseFunc(handlerFunc func(message *nsq.Message, next nsq.HandlerFunc) error) {
nsqg.Use(HandlerFunc(handlerFunc))
}

// UseHandler adds a nsq.Handler onto the middleware stack. Handlers are invoked in the order they are added to a Peacock.
func (peacock *Peacock) UseHandler(handler nsq.Handler) {
peacock.Use(WrapHandler(handler))
// UseHandler adds a nsq.Handler onto the middleware stack. Handlers are invoked in the order they are added to a NSQG.
func (nsqg *NSQG) UseHandler(handler nsq.Handler) {
nsqg.Use(WrapHandler(handler))
}

// UseHandlerFunc adds a nsq.HandlerFunc-style handler function onto the middleware stack.
func (peacock *Peacock) UseHandlerFunc(handlerFunc func(message *nsq.Message) error) {
peacock.UseHandler(nsq.HandlerFunc(handlerFunc))
func (nsqg *NSQG) UseHandlerFunc(handlerFunc func(message *nsq.Message) error) {
nsqg.UseHandler(nsq.HandlerFunc(handlerFunc))
}
34 changes: 17 additions & 17 deletions peacock_test.go → nsqg_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package peacock
package nsqg

import (
"errors"
Expand Down Expand Up @@ -213,7 +213,7 @@ func TestNew(t *testing.T) {
}
}

func TestPeacock_HandleMessage(t *testing.T) {
func TestNSQG_HandleMessage(t *testing.T) {
type fields struct {
handlers []Handler
middleware middleware
Expand Down Expand Up @@ -241,18 +241,18 @@ func TestPeacock_HandleMessage(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
peacock := Peacock{
nsqg := NSQG{
handlers: tt.fields.handlers,
middleware: tt.fields.middleware,
}
if err := peacock.HandleMessage(tt.args.message); (err != nil) != tt.wantErr {
t.Errorf("Peacock.HandleMessage() error = %v, wantErr %v", err, tt.wantErr)
if err := nsqg.HandleMessage(tt.args.message); (err != nil) != tt.wantErr {
t.Errorf("NSQG.HandleMessage() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestPeacock_Use(t *testing.T) {
func TestNSQG_Use(t *testing.T) {
type fields struct {
handlers []Handler
middleware middleware
Expand Down Expand Up @@ -303,16 +303,16 @@ func TestPeacock_Use(t *testing.T) {
}
}()

peacock := &Peacock{
nsqg := &NSQG{
handlers: tt.fields.handlers,
middleware: tt.fields.middleware,
}
peacock.Use(tt.args.handler)
nsqg.Use(tt.args.handler)
})
}
}

func TestPeacock_UseFunc(t *testing.T) {
func TestNSQG_UseFunc(t *testing.T) {
type fields struct {
handlers []Handler
middleware middleware
Expand All @@ -338,16 +338,16 @@ func TestPeacock_UseFunc(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
peacock := &Peacock{
nsqg := &NSQG{
handlers: tt.fields.handlers,
middleware: tt.fields.middleware,
}
peacock.UseFunc(tt.args.handlerFunc)
nsqg.UseFunc(tt.args.handlerFunc)
})
}
}

func TestPeacock_UseHandler(t *testing.T) {
func TestNSQG_UseHandler(t *testing.T) {
type fields struct {
handlers []Handler
middleware middleware
Expand All @@ -373,16 +373,16 @@ func TestPeacock_UseHandler(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
peacock := &Peacock{
nsqg := &NSQG{
handlers: tt.fields.handlers,
middleware: tt.fields.middleware,
}
peacock.UseHandler(tt.args.handler)
nsqg.UseHandler(tt.args.handler)
})
}
}

func TestPeacock_UseHandlerFunc(t *testing.T) {
func TestNSQG_UseHandlerFunc(t *testing.T) {
type fields struct {
handlers []Handler
middleware middleware
Expand All @@ -408,11 +408,11 @@ func TestPeacock_UseHandlerFunc(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
peacock := &Peacock{
nsqg := &NSQG{
handlers: tt.fields.handlers,
middleware: tt.fields.middleware,
}
peacock.UseHandlerFunc(tt.args.handlerFunc)
nsqg.UseHandlerFunc(tt.args.handlerFunc)
})
}
}

0 comments on commit 3ec364d

Please sign in to comment.