rooter

package
v0.0.0-...-2d52d2e Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ResponseBadRequest = Response{
	StatusCode: http.StatusBadRequest,
	Message:    "bad request",
}

ResponseBadRequest is a standard way of serving a 400

View Source
var ResponseInternalServerError = Response{
	StatusCode: http.StatusInternalServerError,
	Message:    "internal server error",
}

ResponseInternalServerError is a standard way of serving a 500

View Source
var ResponseMethodNotAllowed = Response{
	StatusCode: http.StatusMethodNotAllowed,
	Message:    "method not allowed",
}

ResponseMethodNotAllowed is a standard way of serving a 405

View Source
var ResponseNotFound = Response{
	StatusCode: http.StatusNotFound,
	Message:    "not found",
}

ResponseNotFound is a standard way of serving a 404

View Source
var ResponseUnauthorized = Response{
	StatusCode: http.StatusUnauthorized,
	Message:    "unauthorized",
}

ResponseUnauthorized is a standard way of serving a 401

Functions

func MiddlewareLogger

func MiddlewareLogger(next http.Handler) http.Handler

MiddlewareLogger is a simple logging middleware

func RegisterRoutes

func RegisterRoutes(mux *http.ServeMux, routes []Route, middlewares []Middleware)

RegisterRoutes for a given mux

func ResponseFunc

func ResponseFunc(s ResponseHandler) http.Handler

ResponseFunc is a shortcut for making sure a ResponseHandler can be used as a http.Handler

Types

type Middleware

type Middleware func(next http.Handler) http.Handler

Middleware for wrapping http.Handlers

type Param

type Param struct {
	Name     string
	Required bool
}

Param is a route paramerter

type Response

type Response struct {
	StatusCode int         `json:"-"`
	State      bool        `json:"state"`
	Message    string      `json:"message"`
	Data       interface{} `json:"data"`
	ExtraData  interface{} `json:"extraData"`
}

Response is a standard response format to use for API responses

func (Response) Bytes

func (s Response) Bytes() []byte

Bytes return

func (Response) ServeHTTP

func (s Response) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP writes the response as JSON

type ResponseHandler

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

ResponseHandler is a simpler than http.HandlerFunc for enforcing the proper usage of Response

type Route

type Route struct {
	Path       string
	Handler    http.Handler
	Params     []Param
	Middleware []Middleware
	Payload    interface{}
	Response   interface{}
	// Extra Config
	ExcludeFromTypeScript bool
}

Route is an API Route

Jump to

Keyboard shortcuts

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