Documentation ¶
Overview ¶
Package router is service Routes using as a regular REST-API with support for typed parameters and selectable request and response body-encodings
Index ¶
- Constants
- Variables
- func CorrIDFromCtx(ctx context.Context) (id string)
- func CorrIDFromRequest(r *http.Request) (id string)
- func CtxWithCorrID(ctx context.Context, id string) context.Context
- func CtxWithReqID(ctx context.Context, id string) context.Context
- func IDHandler() func(next http.Handler) http.Handler
- func ReqIDFromCtx(ctx context.Context) (id string)
- func ReqIDFromRequest(r *http.Request) (id string)
- func Serve(routes []Route, opts ...Option) error
- func Shutdown()
- type ErrUnmarshal
- type Error
- type FieldError
- type Option
- func WithExposedErrors() Option
- func WithHealth(path string) Option
- func WithName(name string) Option
- func WithPort(port int) Option
- func WithPrefix(path string) Option
- func WithReady(path string) Option
- func WithStrictSlash(flag bool) Option
- func Without204() Option
- func WithoutHealth() Option
- func WithoutReady() Option
- type Route
- type Router
Constants ¶
const (
All = "*"
)
Router constants
Variables ¶
var ( ErrRouterAlreadyRunning = fmt.Errorf("router is already running") ErrInvalidMatch = fmt.Errorf("invalid match") ErrRouterDuplicateName = fmt.Errorf("duplicate router name") )
Our errors
var ( // Ready is the flag for the readiness-probe Ready bool = true // Healty is the flag for the liveness-probe Healty bool = true )
Functions ¶
func CorrIDFromCtx ¶
CorrIDFromCtx returns the unique id associated to the context if any.
func CorrIDFromRequest ¶
CorrIDFromRequest returns the unique id associated to the request if any.
func CtxWithCorrID ¶
CtxWithCorrID adds the given correlation-id to the context
func CtxWithReqID ¶
CtxWithReqID adds the given request-id to the context
func IDHandler ¶
IDHandler returns a handler setting a unique id to the request which can be gathered using IDFromRequest(req). This generated id is added as a field to the logger using the passed fieldKey as field name. The id is also added as a response header if the headerName is not empty.
The generated id is a URL safe base64 encoded mongo object-id-like unique id. Mongo unique id generation algorithm has been selected as a trade-off between size and ease of use: UUID is less space efficient and snowflake requires machine configuration.
func ReqIDFromCtx ¶
ReqIDFromCtx returns the unique id associated to the context if any.
func ReqIDFromRequest ¶
ReqIDFromRequest returns the unique id associated to the request if any.
Types ¶
type ErrUnmarshal ¶ added in v0.2.0
type ErrUnmarshal ctFormat
ErrUnmarshal is an error when parsing the request-body according to the "Content-Type" header
func (ErrUnmarshal) Error ¶ added in v0.2.0
func (ctf ErrUnmarshal) Error() string
type Error ¶ added in v0.3.0
type Error int
Error is when a router is unable to handle to handle options or requests
Errors for router-options
type FieldError ¶
type FieldError struct { Name string `json:"name"` Message string `json:"message"` Value interface{} `json:"value,omitempty"` }
FieldError is the error-message returned when a parameter (query och path) is invalid
type Option ¶ added in v0.3.0
Option is for 'functional options' to the New and Serve-methods
func WithExposedErrors ¶ added in v0.3.0
func WithExposedErrors() Option
WithExposedErrors will send any panic-errors as request-body
func WithHealth ¶ added in v0.3.0
WithHealth sets the path (with leading /) that the health-probe should listen on
func WithPrefix ¶ added in v0.3.0
WithPrefix assigns a prefix to all router (ex "/prefix")
func WithReady ¶ added in v0.3.0
WithReady sets the path (with leading /) that the ready-probe should listen on
func WithStrictSlash ¶ added in v0.3.0
WithStrictSlash sets the StrictSlash-option on the gorilla/mux router
func Without204 ¶ added in v0.4.0
func Without204() Option
Without204 removes the automatic health-probe from the router
func WithoutHealth ¶ added in v0.3.0
func WithoutHealth() Option
WithoutHealth removes the automatic health-probe from the router
func WithoutReady ¶ added in v0.3.0
func WithoutReady() Option
WithoutReady removes the automatic ready-probe from the router
type Route ¶
type Route struct { Name string Method string Path string Handler interface{} // contains filtered or unexported fields }
Route contains the definition of a REST-API with method, path, handler and more