router

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: MIT Imports: 27 Imported by: 0

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

View Source
const (
	All = "*"
)

Router constants

Variables

View Source
var (
	ErrRouterAlreadyRunning = fmt.Errorf("router is already running")
	ErrInvalidMatch         = fmt.Errorf("invalid match")
	ErrRouterDuplicateName  = fmt.Errorf("duplicate router name")
)

Our errors

View Source
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

func CorrIDFromCtx(ctx context.Context) (id string)

CorrIDFromCtx returns the unique id associated to the context if any.

func CorrIDFromRequest

func CorrIDFromRequest(r *http.Request) (id string)

CorrIDFromRequest returns the unique id associated to the request if any.

func CtxWithCorrID

func CtxWithCorrID(ctx context.Context, id string) context.Context

CtxWithCorrID adds the given correlation-id to the context

func CtxWithReqID

func CtxWithReqID(ctx context.Context, id string) context.Context

CtxWithReqID adds the given request-id to the context

func IDHandler

func IDHandler() func(next http.Handler) http.Handler

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

func ReqIDFromCtx(ctx context.Context) (id string)

ReqIDFromCtx returns the unique id associated to the context if any.

func ReqIDFromRequest

func ReqIDFromRequest(r *http.Request) (id string)

ReqIDFromRequest returns the unique id associated to the request if any.

func Serve

func Serve(routes []Route, opts ...Option) error

Serve starts the default router with the supplied routes on the specified port

func Shutdown

func Shutdown()

Shutdown does a graceful shutdown on the default router

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

const (
	ErrorRequireLeadingSlash Error = 1
	ErrorNotValidURL         Error = 2
	ErrorInvalidPort         Error = 3
)

Errors for router-options

func (Error) Error added in v0.3.0

func (err Error) Error() string

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

func (*FieldError) Error

func (fe *FieldError) Error() string

Error to act as an 'error'-type

type Option added in v0.3.0

type Option func(*Router) error

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

func WithHealth(path string) Option

WithHealth sets the path (with leading /) that the health-probe should listen on

func WithName added in v0.4.2

func WithName(name string) Option

WithName assigns a prefix to all router (ex "/prefix")

func WithPort added in v0.3.0

func WithPort(port int) Option

WithPort tells what port to listen on for requests

func WithPrefix added in v0.3.0

func WithPrefix(path string) Option

WithPrefix assigns a prefix to all router (ex "/prefix")

func WithReady added in v0.3.0

func WithReady(path string) Option

WithReady sets the path (with leading /) that the ready-probe should listen on

func WithStrictSlash added in v0.3.0

func WithStrictSlash(flag bool) Option

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

type Router

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

Router is the handler which serves your routes

func New

func New(routes []Route, opts ...Option) (*Router, error)

New creates a custom Router using the supplied routes

func (*Router) Serve

func (r *Router) Serve() error

Serve starts the http-server on the router

func (*Router) Shutdown

func (r *Router) Shutdown()

Shutdown does a graceful shutdown of the router

Jump to

Keyboard shortcuts

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