Documentation
¶
Index ¶
- func DynamicMount(ctx context.Context, ch <-chan Route, definitions ...*RoutingDefinition) *http.ServeMux
- func Mount(definitions ...*RoutingDefinition) *http.ServeMux
- func MountRoute(mux *http.ServeMux, r Route)
- func MountServices(services ...*ServiceBuilder) *http.ServeMux
- type Builder
- type Middleware
- type Route
- type Routing
- type RoutingDefinition
- func (m *RoutingDefinition) Add(method string, path string, h http.HandlerFunc, mid ...Middleware) *RoutingDefinition
- func (m *RoutingDefinition) AddMany(methods []string, path string, h http.HandlerFunc, mid ...Middleware) *RoutingDefinition
- func (m *RoutingDefinition) Detach(mid ...Middleware) *RoutingDefinition
- func (m *RoutingDefinition) Done() []Route
- type ServiceBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DynamicMount ¶
func DynamicMount(ctx context.Context, ch <-chan Route, definitions ...*RoutingDefinition) *http.ServeMux
DynamicMount uses definitions to register the input routes by using the Mount function. The channel is used to dynamically register additional routes in a separate go routine.
The goroutine will halt when context is done.
func Mount ¶
func Mount(definitions ...*RoutingDefinition) *http.ServeMux
Mount the provided routing definitions to `http.ServeMux` and returns it.
func MountRoute ¶
func MountServices ¶
func MountServices(services ...*ServiceBuilder) *http.ServeMux
MountServices to a new http.ServeMux
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder represents a helper struct to build a single Route
func NewBuilder ¶
NewBuilder start building a Route specifying the method parameters (POST, PUT, PATCH...)
func (*Builder) Build ¶
Build finalize the api build process applying a reverse function on middleware slice (to preserve the order) and returns the Route with the handler func concatenated with the middlewares
func (*Builder) Handler ¶
func (rb *Builder) Handler(h http.HandlerFunc) *Builder
Handler is a simple http.HandlerFunc
func (*Builder) Middleware ¶
func (rb *Builder) Middleware(middleware ...Middleware) *Builder
Middleware sets the middlewares to be injected to the http.HandlerFunc specified with Handler function. The slice of input `middleware` will be reversed to respect the sequentiality of the call stack.
Example:
Middleware(m1, m2, m3) => m1 -> m2 -> m3 Middleware(m7, m5, m9) => m7 -> m5 -> m9
type Middleware ¶
type Middleware func(h http.HandlerFunc) http.HandlerFunc
Middleware is an alias of http.HandlerFunc wrappers. For example the go chi compress (https://github.com/go-chi/chi/blob/1191921289e82fdc56f298a76ff254742f568ece/middleware/compress.go#L41-L44) is a Middleware too.
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
A Route consists of an HTTP method, a URL path, and a handler function to be executed when the route is matched.
Example: GET /api/v1/users func(http.ResponseWriter, *http.Request)
func Merge ¶
func Merge(definitions ...*RoutingDefinition) []Route
func (Route) Handler ¶
func (a Route) Handler() http.HandlerFunc
Handler describes a `func(http.ResponseWriter, *http.Request)`.
The final Handler become the result of merged middlewares plus the handler itself.
Example:
NewBuilder("GET").Path("/").Handler(myHandler).Middleware(mid1, mid2, midN...)
Result execution stack:
mid1 -> mid2 -> midN -> myHandler
type Routing ¶
type Routing interface {
Routes(with *RoutingDefinition) []Route
}
type RoutingDefinition ¶
type RoutingDefinition struct {
// contains filtered or unexported fields
}
RoutingDefinition represents a slice of Route
func NewDefinition ¶
func NewDefinition(m ...Middleware) *RoutingDefinition
NewDefinition returns a new *RoutingDefinition instance. The provided middleware functions are injected into the routes added after this call. The middleware functions are executed sequentially in the order they are provided.
Usage: - start with NewDefinition() and pass some middleware e.g. firstMiddleware, secondMiddleware, etc. - add routes using Add method. - invoke Build to return a slice of Route. Example: NewDefinition(firstMiddleware, secondMiddleware). Add(http.MethodGet, "/path", handler1). Add(http.MethodPost, "/path2", handler2, anotherMiddleware)
Resulting stack for routes:
- (GET /path): firstMiddleware => secondMiddleware => handler1
- (POST /path2): firstMiddleware => secondMiddleware => anotherMiddleware => handler2
func (*RoutingDefinition) Add ¶
func (m *RoutingDefinition) Add(method string, path string, h http.HandlerFunc, mid ...Middleware) *RoutingDefinition
Add saves a Route into *RoutingDefinition instance
Example:
def := NewDefinition() def.Route("GET", "/api/v1/users", func..., m1, m2, m3)
func (*RoutingDefinition) AddMany ¶
func (m *RoutingDefinition) AddMany(methods []string, path string, h http.HandlerFunc, mid ...Middleware) *RoutingDefinition
AddMany is like Add but this function can address more than one http method with the same handler.
func (*RoutingDefinition) Detach ¶
func (m *RoutingDefinition) Detach(mid ...Middleware) *RoutingDefinition
Detach creates a new RoutingDefinition by concatenating the current middlewares from m into the newly created RoutingDefinition
func (*RoutingDefinition) Done ¶
func (m *RoutingDefinition) Done() []Route
Done returns the slice of Route.
type ServiceBuilder ¶
type ServiceBuilder struct {
// contains filtered or unexported fields
}
func NewServiceBuilder ¶
func NewServiceBuilder(commonMiddlewares ...Middleware) *ServiceBuilder
func (*ServiceBuilder) Add ¶
func (s *ServiceBuilder) Add(routing ...Routing) *ServiceBuilder
func (*ServiceBuilder) MW ¶
func (s *ServiceBuilder) MW(mws ...Middleware) *ServiceBuilder
MW appends new middleware to the already known `middlewares` specified in NewServiceBuilder.
func (*ServiceBuilder) MountTo ¶
func (s *ServiceBuilder) MountTo(mux *http.ServeMux)