rest

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2021 License: MIT Imports: 11 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 AttachProfiler added in v0.1.3

func AttachProfiler(m *http.ServeMux)

AttachProfiler is responsible for adding the pprof routes to the server mux passed as a parameter

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, execOnRecover ...func())

RecoverFromPanic will handle panic

func UnmarshalERROR

func UnmarshalERROR(body io.Reader) error

UnmarshalERROR generates an ierror error with the response body created by the ERROR function.

Note that this function will always return an error no matter if it found the error on the body or not, that means that if the error details cannot be found on the body of the response body it will generate a unkown error

Types

type CRUDHandler

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

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 Healthz added in v0.1.3

func Healthz() Handler

Healthz handles health check requests

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(execOnRecover ...func()) 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