README

routebuilder

routebuilder_gopher

This utility provides some methods to build your http routes fluently.

Single route

In the example section there is also a Middleware() calls, those middleware are built at Build(), in that way you can easily add middleware to your route.

Multi routes

If you need to build a lot of routes, with the same middleware under a specific path (e.g. /api/users) the package also provide a routebuilder.Parent builder.

Examples

Builder
package main

import "net/http"


// main func

func GetUsers() *routebuilder.Route {
  return routebuilder.New(routebuilder.GET).
    Path("/api/v1/users").
    Middleware(middleware.BasicAuth(myAuthFunc)).
    HandlerFunc(
      func(w http.ResponseWriter, r *http.Request) {
        // TODO: implement
      },
    ).
    Build()
}
Parent
package main
// main func

func UserRoutes() []*routebuilder.Route {
  return routebuilder.WithParent("/api/v1/users").
    Middleware(someMiddleware...).
    Get("", getUsersHandler).        // /api/v1/users
    Post(":id", createUserHandler). // /api/v1/users/:id
    Routes()
	
}

func getUsersHandler() http.HandlerFunc {
  panic("not implemented yet")
}

func createUserHandler() http.HandlerFunc {
  panic("not implemented yet")
}
Assembling with goji
Single
package main

import (
    "fmt"
    "net/http"
    
    "github.com/askm3/routebuilder"
    "goji.io"
    "goji.io/pat"
)

func main() {
    mux := goji.NewMux()
    r := GetUsers()
    path := pat.NewWithMethods(r.GetPath(), r.GetMethod())
    mux.HandleFunc(path, rb.GetHandler())
    http.ListenAndServe("localhost:8000", mux)
}
Multi
package main

import (
    "fmt"
    "net/http"
    
    "github.com/askm3/routebuilder"
    "goji.io"
    "goji.io/pat"
)

func main() {
    mux := goji.NewMux()
    for _, r := range UserRoutes() {
      path := pat.NewWithMethods(r.GetPath(), r.GetMethod())
      mux.HandleFunc(path, r.HandlerFunc())
    }
    
    http.ListenAndServe("localhost:8000", mux)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

func New

func New(method Method) *Builder

func (*Builder) Build

func (rb *Builder) Build() *Route

func (*Builder) Handler

func (rb *Builder) Handler(h http.HandlerFunc) *Builder

func (*Builder) Middleware

func (rb *Builder) Middleware(middleware ...Middleware) *Builder

func (*Builder) Path

func (rb *Builder) Path(p string) *Builder

type Method

type Method string
const (
	GET     Method = "GET"
	POST    Method = "POST"
	PUT     Method = "PUT"
	PATCH   Method = "PATCH"
	DELETE  Method = "DELETE"
	OPTIONS Method = "OPTIONS"
	HEAD    Method = "HEAD"
	CONNECT Method = "CONNECT"
)

type Middleware

type Middleware func(h http.HandlerFunc) http.HandlerFunc

type Multi

type Multi struct {
	// contains filtered or unexported fields
}

func NewMulti

func NewMulti() *Multi

func (*Multi) AddRoute

func (m *Multi) AddRoute(me Method, path string, h http.HandlerFunc, midd ...Middleware) *Multi

func (*Multi) Done

func (m *Multi) Done() []*Route

type Parent

type Parent struct {
	// contains filtered or unexported fields
}

func WithParent

func WithParent(basePath string) *Parent

func (*Parent) Connect

func (pb *Parent) Connect(subPath string, fn http.HandlerFunc) *Parent

func (*Parent) Delete

func (pb *Parent) Delete(subPath string, fn http.HandlerFunc) *Parent

func (*Parent) Get

func (pb *Parent) Get(subPath string, fn http.HandlerFunc) *Parent

func (*Parent) Head

func (pb *Parent) Head(subPath string, fn http.HandlerFunc) *Parent

func (*Parent) Middleware

func (pb *Parent) Middleware(middleware ...Middleware) *Parent

func (*Parent) Options

func (pb *Parent) Options(subPath string, fn http.HandlerFunc) *Parent

func (*Parent) Patch

func (pb *Parent) Patch(subPath string, fn http.HandlerFunc) *Parent

func (*Parent) Post

func (pb *Parent) Post(subPath string, fn http.HandlerFunc) *Parent

func (*Parent) Put

func (pb *Parent) Put(subPath string, fn http.HandlerFunc) *Parent

func (*Parent) Routes

func (pb *Parent) Routes() []*Route

type Route

type Route struct {
	// contains filtered or unexported fields
}

func (*Route) GetHandler

func (r *Route) GetHandler() http.HandlerFunc

func (*Route) GetMethod

func (r *Route) GetMethod() string

func (*Route) GetPath

func (r *Route) GetPath() string

Directories

Path Synopsis