rest

package
v0.0.40 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2021 License: MIT Imports: 10 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 ValidateURLPageNumber

func ValidateURLPageNumber(val string) (int, bool)

func ValidateURLPageSize

func ValidateURLPageSize(val string, max int) (int, bool)

func ValidateURLSortFields

func ValidateURLSortFields(val string, max int) ([]string, bool)

func Write

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

Types

type ErrorCode

type ErrorCode string
const (
	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 (
	ErrMsgInvalidRequest   ErrorMessage = "An invalid request was provided"
	ErrMsgRouteNotFound    ErrorMessage = "The route cannot be found"
	ErrMsgResourceNotFound ErrorMessage = "The resource cannot be found"
	ErrMsgMethodNotAllowed ErrorMessage = "The method is not allowed"
	ErrMsgConflict         ErrorMessage = "A conflict has been detected"
	ErrMsgInternal         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 specific 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 them 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) AddRoutes

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

AddRoutes adds new routes to the collection. It registers route specific middlewares if provided.

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 default 405 responses.

func (*Router) SetRouteNotFound

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

SetRouteNotFound sets a custom handler that handles default 404 responses.

func (*Router) UseMiddleware

func (r *Router) UseMiddleware(middleware Middleware)

UseMiddleware adds a new middleware to the collection. The given middleware is applicable to all registered routes.

type ServerConfig

type ServerConfig struct {
	Host         string
	Port         string
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
}

Jump to

Keyboard shortcuts

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