Skip to content

nowk/ripple

Repository files navigation

ripple

Structured controllers for Echo


CircleCI Build Status GoDoc

Install

go get github.com/nowk/ripple

gopkg.in version

go get gopkg.in/nowk/ripple.v0

Usage

Create your Controller

type PostsController struct {
    Index echo.HandlerFunc `ripple:"GET /"`
    Show  echo.HandlerFunc `ripple:"GET /:id"`
    ...
}

func (PostsController) Path() string {
    return "/posts"
}

func (PostsController) IndexFunc(w http.ResponseWriter, req *http.Request) {
    ...
}

func (PostsController) ShowFunc(c *echo.Context) error {
    ...
}

...

"Group" your Controller onto an instance of Echo.

echoMux := echo.New()

ripple.Group(&PostsController{}, echoMux)

This creates a new Echo group at the Controller#Path, in our example /posts, with all the defined actions.

// GET /posts     => #IndexFunc
// GET /posts/:id => #ShowFunc

Controller interface:

Structs must implement the Controller interface.

type Controller interface {
    Path() string
}

#Path() is the namespace for the new Echo Group the controller will be created on.

Ripple tag format:

The ripple field tag format is a simple <HTTP METHOD> /path

POST /posts

The path must be a valid Echo path.

The path is relative to the Controller's defined #Path() method

func (Controller) Path() string {
    return "/posts"
}

Given the above #Path() any defined paths will be relative to /posts.

Index  http.Handler `ripple:"GET /"`
Update http.Handler `ripple:"PUT /:id"`
...

// GET /posts
// PUT /posts/:id

Defining handlers:

Handlers are defined through a Field Name to <Field Name>Func association.

Index  echo.HandlerFunc `ripple:"GET /"`
Update echo.HandlerFunc `ripple:"PUT /:id"`
...

Will look for a method within the struct that matches <Field Name>Func

func (Controller) IndexFunc(...) { ... }
func (Controller) UpdateFunc(...) { ... }
...

You can also define the handler to the Field itself during construction.

&PostsController{
    Index: func(w http.ResponseWriter, req *http.Request) {
        ...
    },
}

The associated <Field Name>Func will always be used over any Field assignment if the associated method exists.

Defining middlewares:

Middlewares are defined very much like handlers, with the exception of the ripple tag format which must be defined as such:

Log echo.Middleware `ripple:",middleware"`

And like their handler counter parts are also looked up in the same <Field Name>Func manner.

func (Controller) LogFunc(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWrite, req *http.Request) {
        ...
        next.ServeHTTP(w, req)
        ...
    })
}

Field types matter:

The field types must be a type the associated method either is or can be converted to and must ultimately be a pattern that can be mounted onto an Echo Group.

Example

A basic example can be found @ ripple-example

Roadmap

  • Embeddable structs, especially for middleware resusability.

License

MIT

About

Use structured controllers for Echo

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages