rip

package module
v0.0.0-...-39efcab Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2017 License: LGPL-3.0 Imports: 10 Imported by: 0

README

rip (rest in peace)

Allows to define REST API endpoints using a fluent nested interface:

api := rip.NewRIP()
api.Path("items").Do(func(api *rip.Route) {
    api.POST().Target(map[string]interface{}{}).Handler(addItemHandler, "Add an item")
    api.GET().Param("ascending", "Order items in ascending order if true").
        Param("tag", "Only list items with given tags").
        Handler(listItemHandler, "List Items")
    api.Var("id").Do(func(api *rip.Route) {
        api.Delete().Handler(deleteItemHandler, "Remove an item")
    })
})

Will create a http.Handler which allows adding items to the endpoint /items. When calling POST /items the sent body (JSON) will be parsed into a map[string]interface{}, defined by Target(). In the handler it can be retrieved like rip.Body(request).

It will also allow a GET /items which will return a list of items. The query parameters ascending and tag cn be given to refine the result. From the handlers the variables can be read by rip.Vars(request).MustGetVar("tag").

Additionally an Item can be deleted by using a DELETE /items/item-id request. The defined id var in the path can also be requested using rip.Vars(request).MustGetVar("id") in the handler.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Body

func Body(r *http.Request) interface{}

func DocHandler

func DocHandler(rip *rip) http.Handler

func NewRIP

func NewRIP() *rip

Types

type Parameter

type Parameter interface {
	Name() string
	Doc() string
}

type PathElement

type PathElement interface {
	Template() string
}

type RIPResponse

type RIPResponse struct {
	StatusCode int
	Data       interface{}
}

func Response

func Response(r *http.Request) *RIPResponse

type Route

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

func (Route) DELETE

func (r Route) DELETE() *Route

func (*Route) Do

func (r *Route) Do(subcall func(*Route)) *Route

func (Route) GET

func (r Route) GET() *Route

func (*Route) Handler

func (r *Route) Handler(handler http.Handler, doc string)

func (*Route) Method

func (r *Route) Method() string

func (Route) POST

func (r Route) POST() *Route

func (Route) PUT

func (r Route) PUT() *Route

func (Route) Param

func (r Route) Param(name string, doc string) *Route

func (Route) Path

func (r Route) Path(pathElements ...string) *Route

func (*Route) QueryTemplate

func (r *Route) QueryTemplate() string

func (Route) SetMethod

func (r Route) SetMethod(m string) *Route

func (Route) Target

func (r Route) Target(target interface{}) *Route

func (*Route) Template

func (r *Route) Template() string

func (Route) Var

func (r Route) Var(name string, doc string) *Route

type RouteMethodError

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

func (*RouteMethodError) Error

func (rem *RouteMethodError) Error() string

type RouteMissingMethodError

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

func (*RouteMissingMethodError) Error

func (rem *RouteMissingMethodError) Error() string

type Variables

type Variables map[string]string

func Vars

func Vars(r *http.Request) Variables

func (Variables) GetVar

func (v Variables) GetVar(name string) (string, error)

func (Variables) MustGetVar

func (v Variables) MustGetVar(name string) string

func (Variables) NumVars

func (v Variables) NumVars() int

Jump to

Keyboard shortcuts

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