Documentation
¶
Index ¶
- func NewServer(config ServerConfig, handler http.Handler) *http.Server
- func RouteParamFromContext(ctx context.Context, name string) string
- 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.
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 ¶
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 ¶
AddRoutes adds new routes to the collection. It registers middlewares if there was any.
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 405 responses.
func (*Router) SetRouteNotFound ¶
SetRouteNotFound sets a custom handler that handles 404 responses.