rest

package
v0.0.37 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewServer

func NewServer(config ServerConfig, handler http.Handler) *http.Server

func RouteParamFromContext

func RouteParamFromContext(ctx context.Context, name string) string

RouteParamFromContext returns the value for the given route parameter name.

func Write

func Write(w http.ResponseWriter, r Response) error

Types

type ErrorCode

type ErrorCode string
const (
	ErrCodeMalformedBody    ErrorCode = "malformed_body"
	ErrCodeInvalidRequest   ErrorCode = "invalid_request"
	ErrCodeRouteNotFound    ErrorCode = "route_not_found"
	ErrCodeResourceNotFound ErrorCode = "resource_not_found"
	ErrCodeMethodNotAllowed ErrorCode = "method_not_allowed"
	ErrCodeConflict         ErrorCode = "conflict"
	ErrCodeInternal         ErrorCode = "internal"
)

type ErrorFieldMessage

type ErrorFieldMessage string
const (
	MsgInvalidValue ErrorFieldMessage = "Invalid value"
	MsgUnknownField ErrorFieldMessage = "Unknown field"
)

type ErrorMessage

type ErrorMessage string
const (
	ErrMgsMalformedBody    ErrorMessage = "A malformed body was provided"
	ErrMgsInvalidRequest   ErrorMessage = "An invalid request was provided"
	ErrMgsRouteNotFound    ErrorMessage = "The route cannot be found"
	ErrMgsResourceNotFound ErrorMessage = "The resource cannot be found"
	ErrMgsMethodNotAllowed ErrorMessage = "The method is not allowed"
	ErrMgsConflict         ErrorMessage = "A conflict has been detected"
	ErrMgsInternal         ErrorMessage = "An internal error has occurred"
)

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware represents server and route level middlewares that apply to all or partial routes respectively.

type Response

type Response struct {
	// Shared common fields.
	Status  int               `json:"-"`
	Headers map[string]string `json:"-"`

	// Success specific fields.
	Data interface{} `json:"data,omitempty"`
	Meta interface{} `json:"meta,omitempty"`

	// Failure specific fields.
	Code    ErrorCode                    `json:"code,omitempty"`
	Message ErrorMessage                 `json:"message,omitempty"`
	Errors  map[string]ErrorFieldMessage `json:"errors,omitempty"`
}

type Route

type Route struct {
	Method      string
	Path        string
	Handler     http.HandlerFunc
	Middlewares []Middleware
}

Route is used by application routes in order to get themselves registered into the `Router` type with the help of `AddRoutes` method.

type Router

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

Router represents HTTP router. It helps adding application routes, server level middlewares and some custom handler.

func NewRouter

func NewRouter() *Router

NewRouter returns `Router` type with some default configurations.

func (*Router) AddMiddlewares

func (r *Router) AddMiddlewares(middlewares ...Middleware)

AddMiddlewares adds new middlewares to the collection. The given middlewares are applicable to all routes.

func (*Router) AddRoutes

func (r *Router) AddRoutes(routes []Route)

AddRoutes adds new routes to the collection. It registers middlewares if there was any.

func (*Router) Handler

func (r *Router) Handler() http.Handler

Handler applies previously added middlewares to all routes and returns HTTP handler for HTTP server to use.

func (*Router) SetMethodNotAllowed

func (r *Router) SetMethodNotAllowed(handler http.Handler)

SetMethodNotAllowed sets a custom handler that handles 405 responses.

func (*Router) SetRouteNotFound

func (r *Router) SetRouteNotFound(handler http.Handler)

SetRouteNotFound sets a custom handler that handles 404 responses.

type ServerConfig

type ServerConfig struct {
	Host string
	Port string
}

Jump to

Keyboard shortcuts

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