whisper

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2019 License: MIT Imports: 8 Imported by: 0

README

Whisper

Whisper is labstack/echo json rest API wrapper

Install

dep ensure -add github.com/andrdru/whisper github.com/labstack/echo

Example

Parameter from URL, reply with json

package main

import "github.com/andrdru/whisper"

func main() {
	e := whisper.New()
	e.GET("/users/:id", getUser)
	e.Logger.Fatal(e.Start(":1323"))
}

func getUser(c whisper.Context) error {
	id := c.Param("id")
	ret := map[string]interface{}{"id": id}
	c.Mes.Set(ret)
	return c.Ret()
}

Post json data, bind it, reply with json

package main

import "github.com/andrdru/whisper"

type UserData struct {
	Name  string `json:"name"`
	Email string `json:"email"`
}

func main() {
	e := whisper.New()
	e.POST("/user", addUser)
	e.Logger.Fatal(e.Start(":1323"))
}

func addUser(c whisper.Context) error {
	in := UserData{}

	if err := c.Bind(&in); err != nil {
		c.Mes.SetErr(whisper.ErrWrongRequestParam)
		return c.Ret()
	}

	c.Mes.Set(in)
	return c.Ret()
}

Known issues

Echo methods

echo.Reverse() 
echo.URI() 
echo.URL()

are not implemented yet

License

MIT

Documentation

Index

Constants

View Source
const (
	NoErr                = http.StatusOK
	ErrInternal          = http.StatusInternalServerError
	ErrWrongRequest      = http.StatusMethodNotAllowed
	ErrWrongRequestParam = http.StatusNotAcceptable
	ErrWrongRequestData  = http.StatusBadRequest
	ErrForbidden         = http.StatusForbidden
	ErrNotFound          = http.StatusNotFound
)
View Source
const (
	PROPFIND = "PROPFIND"
)

Variables

View Source
var (
	NotFoundHandler = func(c Context) error {
		return ErrNotFound222
	}

	MethodNotAllowedHandler = func(c Context) error {
		return ErrMethodNotAllowed
	}
)

Error handlers

View Source
var (
	ErrNotFound222      = echo.NewHTTPError(http.StatusNotFound)
	ErrMethodNotAllowed = echo.NewHTTPError(http.StatusMethodNotAllowed)
)
View Source
var ErrorsDefault = map[int]string{
	NoErr:                "Ok",
	ErrInternal:          "Server error",
	ErrWrongRequest:      "Wrong API request",
	ErrWrongRequestParam: "Wrong request param",
	ErrWrongRequestData:  "Wrong request data",
	ErrForbidden:         "Access denied",
	ErrNotFound:          "Not found",
}

Functions

This section is empty.

Types

type Bind added in v1.3.1

type Bind struct {
	Data map[string]interface{}
}

func (*Bind) Bind added in v1.3.1

func (st *Bind) Bind(i interface{}, c Context) (err error)

func (*Bind) BindMap added in v1.3.1

func (st *Bind) BindMap(c Context) (err error)

func (*Bind) BindMapParam added in v1.4.0

func (st *Bind) BindMapParam(c Context)

func (*Bind) BindMultipart added in v1.5.0

func (st *Bind) BindMultipart(c Context) (err error)

type Context

type Context struct {
	echo.Context
	Mes  *Message
	Data interface{}
}

func (*Context) Ret

func (st *Context) Ret() error

type Group added in v1.2.0

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

func (*Group) Add added in v1.2.0

func (g *Group) Add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route

Add implements `Echo#Add()` for sub-routes within the Group.

func (*Group) CONNECT added in v1.2.0

func (g *Group) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

CONNECT implements `Echo#CONNECT()` for sub-routes within the Group.

func (*Group) DELETE added in v1.2.0

func (g *Group) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

DELETE implements `Echo#DELETE()` for sub-routes within the Group.

func (*Group) GET added in v1.2.0

func (g *Group) GET(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

GET implements `Echo#GET()` for sub-routes within the Group.

func (*Group) HEAD added in v1.2.0

func (g *Group) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

HEAD implements `Echo#HEAD()` for sub-routes within the Group.

func (*Group) OPTIONS added in v1.2.0

func (g *Group) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

OPTIONS implements `Echo#OPTIONS()` for sub-routes within the Group.

func (*Group) PATCH added in v1.2.0

func (g *Group) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

PATCH implements `Echo#PATCH()` for sub-routes within the Group.

func (*Group) POST added in v1.2.0

func (g *Group) POST(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

POST implements `Echo#POST()` for sub-routes within the Group.

func (*Group) PUT added in v1.2.0

func (g *Group) PUT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

PUT implements `Echo#PUT()` for sub-routes within the Group.

func (*Group) TRACE added in v1.2.0

func (g *Group) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

TRACE implements `Echo#TRACE()` for sub-routes within the Group.

func (*Group) Use added in v1.2.0

func (g *Group) Use(middleware ...MiddlewareFunc)

type HandlerFunc

type HandlerFunc func(Context) error

type Message

type Message struct {
	Err    int         `json:"error"`
	Data   interface{} `json:"data,omitempty"`
	ErrMes []string    `json:"error_description,omitempty"`
}

func NewMessage

func NewMessage() *Message

func (*Message) Get

func (st *Message) Get() []byte

func (*Message) Set

func (st *Message) Set(data interface{})

func (*Message) SetErr

func (st *Message) SetErr(err int)

func (*Message) SetErrCode

func (st *Message) SetErrCode(err int)

func (*Message) SetErrCustom

func (st *Message) SetErrCustom(err int, description string)

type MiddlewareFunc

type MiddlewareFunc func(HandlerFunc) HandlerFunc

type Route

type Route struct {
	echo.Route
}

type Whisper

type Whisper struct {
	*echo.Echo
}

func New

func New(d interface{}) *Whisper

func (*Whisper) Add

func (e *Whisper) Add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route

func (*Whisper) Any added in v1.2.0

func (e *Whisper) Any(path string, handler HandlerFunc, middleware ...MiddlewareFunc) []*Route

Any registers a new route for all HTTP methods and path with matching handler in the router with optional route-level middleware.

func (*Whisper) CONNECT

func (e *Whisper) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

CONNECT registers a new CONNECT route for a path with matching handler in the router with optional route-level middleware.

func (*Whisper) DELETE

func (e *Whisper) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

DELETE registers a new DELETE route for a path with matching handler in the router with optional route-level middleware.

func (*Whisper) GET

func (e *Whisper) GET(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

GET registers a new GET route for a path with matching handler in the router with optional route-level middleware.

func (*Whisper) Group added in v1.2.0

func (e *Whisper) Group(prefix string, m ...MiddlewareFunc) (g *Group)

Group creates a new router group with prefix and optional group-level middleware.

func (*Whisper) HEAD

func (e *Whisper) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

HEAD registers a new HEAD route for a path with matching handler in the router with optional route-level middleware.

func (*Whisper) OPTIONS

func (e *Whisper) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

OPTIONS registers a new OPTIONS route for a path with matching handler in the router with optional route-level middleware.

func (*Whisper) PATCH

func (e *Whisper) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

PATCH registers a new PATCH route for a path with matching handler in the router with optional route-level middleware.

func (*Whisper) POST

func (e *Whisper) POST(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

POST registers a new POST route for a path with matching handler in the router with optional route-level middleware.

func (*Whisper) PUT

func (e *Whisper) PUT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

PUT registers a new PUT route for a path with matching handler in the router with optional route-level middleware.

func (*Whisper) TRACE

func (e *Whisper) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

TRACE registers a new TRACE route for a path with matching handler in the router with optional route-level middleware.

Jump to

Keyboard shortcuts

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