Documentation
¶
Index ¶
- func NewServer(config ServerConfig, handler http.Handler) *http.Server
- func RouteParamFromContext(ctx context.Context, name string) string
- func ValidateURLPageNumber(val string) (int, bool)
- func ValidateURLPageSize(val string, max int) (int, bool)
- func ValidateURLSortFields(val string, max int) ([]string, bool)
- func Write(w http.ResponseWriter, r Response) error
- type ErrorCode
- type ErrorFieldMessage
- type ErrorMessage
- type Middleware
- type Response
- type Route
- type Router
- type ServerConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RouteParamFromContext ¶
RouteParamFromContext returns the value for the given route parameter name.
func ValidateURLPageNumber ¶
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 ¶
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 ¶
AddRoutes adds new routes to the collection. It registers route specific middlewares if provided.
func (*Router) Handler ¶
Handler applies previously added middlewares to all routes and returns HTTP handler for HTTP server to use.
func (*Router) SetMethodNotAllowed ¶
SetMethodNotAllowed sets a custom handler that handles default 405 responses.
func (*Router) SetRouteNotFound ¶
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.