rest

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package rest contains the functions that make it easier to manager api handler functions

Index

Constants

View Source
const (
	// HeaderScopeKey is used to obtain or to change the scope in the http
	// request header, usually used in the handlers of the api package and the
	// client of the controller package.
	HeaderScopeKey = "Scope"
)

Variables

This section is empty.

Functions

func ERROR

func ERROR(w http.ResponseWriter, err error)

ERROR reports the error back to the user within a JSON format

func JSON

func JSON(w http.ResponseWriter, statusCode int, data interface{})

JSON writes the data into the response writer with a JSON format

func RecoverFromPanic

func RecoverFromPanic(w http.ResponseWriter)

RecoverFromPanic will handle panic

func UnmarshalERROR

func UnmarshalERROR(body io.Reader) error

UnmarshalERROR generates a golang error with the response body created by the ERROR function

Types

type CRUDHandler

type CRUDHandler interface {
	HandleCreate() Handler
	HandleDelete() Handler
	HandleUpdate() Handler
	HandleGet() Handler
	GetAuth() auth.Auth
}

CRUDHandler handles crud requests to a given resource

type Handler

type Handler func(w http.ResponseWriter, r *http.Request)

Handler is an alias of the api router function. It acts as the function that handles the routes but at the same time it contains certain methods attached to it that allows for more utility. For an example check the JSON function.

func HandleCRUD

func HandleCRUD(handler CRUDHandler) Handler

HandleCRUD uses a CRUDHandler to handle HTTP requests for a CRUD resource

func (Handler) Delete

func (h Handler) Delete() Handler

Delete runs function if the request method is DELETE

func (Handler) Get

func (h Handler) Get() Handler

Get runs function if the request method is GET

func (Handler) HTTPHandlerFunc

func (h Handler) HTTPHandlerFunc() http.HandlerFunc

HTTPHandlerFunc transforms the rest.handler to a http.handlerFunc type

func (Handler) JSON

func (h Handler) JSON() Handler

JSON specifies in the header that the response content is a json

func (Handler) Methods

func (h Handler) Methods(methods ...string) http.HandlerFunc

Methods allows for generic method definition

func (Handler) Post

func (h Handler) Post() Handler

Post runs function if the request method is POST

func (Handler) Put

func (h Handler) Put() Handler

Put runs function if the request method is PUT

func (Handler) Recover

func (h Handler) Recover() Handler

Recover adds a recover function to the handler

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (Handler) Validate

func (h Handler) Validate(auth auth.Auth) Handler

Validate handles the token validation of the http requests made, it receives an implementation of the auth interface as a parameter.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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