Documentation
¶
Index ¶
- Variables
- func CompressMiddleware(next http.Handler) http.Handler
- func MetricsMiddleware(handler http.Handler) http.Handler
- func RegisterEntityRoutes(router *Router, resourceService services.ResourceService, ...)
- func ResetMetricCollectors()
- func WithNotFoundHandler(mux *http.ServeMux) http.Handler
- type APIServer
- type ListenNotifier
- type Middleware
- type RouteRegistrar
- type Router
- func (r *Router) Group(prefix ...string) *Router
- func (r *Router) Handle(pattern string, handler http.Handler)
- func (r *Router) HandleFunc(pattern string, handler http.HandlerFunc)
- func (r *Router) Handler(req *http.Request) (http.Handler, string)
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) Use(mw ...Middleware)
- type Server
Constants ¶
This section is empty.
Variables ¶
var MetricsLabels = []string{
metricsComponentLabel,
metricsVersionLabel,
metricsMethodLabel,
metricsPathLabel,
metricsCodeLabel,
}
MetricsLabels - Array of labels added to metrics:
var MetricsNames = []string{
requestCount,
requestDuration,
}
MetricsNames - Array of Names of the metrics:
var PathVarSub = "-"
PathVarSub replaces path variables to a same character
Functions ¶
func CompressMiddleware ¶ added in v0.3.1
CompressMiddleware gzip-encodes the response body when the client indicates support for it via the Accept-Encoding header.
func MetricsMiddleware ¶
MetricsMiddleware creates a new handler that collects metrics for the requests processed by the given handler.
func RegisterEntityRoutes ¶ added in v0.3.1
func RegisterEntityRoutes( router *Router, resourceService services.ResourceService, adapterStatusService services.AdapterStatusService, schemaValidator *validators.SchemaValidator, )
RegisterEntityRoutes creates handlers and registers routes for every entity descriptor in the registry. Called at startup after config-driven descriptors have been loaded via registry.LoadDescriptors.
Top-level entities get routes at /{plural}. Child entities (ParentKind != "") get nested routes under /{parent_plural}/{parent_id}/{plural} plus flat read/update/delete access at /{plural} (POST rejected - needs parent context). All entities get /{id}/statuses sub-routes for adapter status reporting.
The kind-agnostic /resources root endpoint is registered separately.
func ResetMetricCollectors ¶
func ResetMetricCollectors()
ResetMetricCollectors resets all prometheus collectors
func WithNotFoundHandler ¶ added in v0.3.1
WithNotFoundHandler wraps mux so that requests matching no registered pattern get api.SendNotFound's JSON body instead of net/http's default plain-text 404.
It must NOT be implemented by registering a catch-all "/" pattern on mux: net/http.ServeMux falls back to any matching less-specific pattern (like "/") when a request's method doesn't match a more specific one, which would silently swallow the automatic 405 Method Not Allowed responses (and their Allow header) stdlib provides for registered paths. Instead, mux.Handler is used to check ahead of time whether the request matches no pattern at all (as opposed to matching a pattern with the wrong method), and only that case is rewritten.
Types ¶
type APIServer ¶ added in v0.3.1
type APIServer struct {
// contains filtered or unexported fields
}
func NewAPIServer ¶
func (*APIServer) Listen ¶ added in v0.3.1
Listen only start the listener, not the server. Useful for breaking up ListenAndServer (Start) when you require the server to be listening before continuing
func (*APIServer) Serve ¶ added in v0.3.1
Serve start the blocking call to Serve. Useful for breaking up ListenAndServer (Start) when you require the server to be listening before continuing
type ListenNotifier ¶
type ListenNotifier interface {
NotifyListening() <-chan struct{}
}
ListenNotifier is an optional interface that servers can implement to signal when they are ready to accept connections
type Middleware ¶ added in v0.3.1
Middleware wraps an http.Handler to produce a new http.Handler, allowing cross-cutting behavior (logging, auth, tracing, ...) to be composed around route handlers.
type RouteRegistrar ¶ added in v0.3.1
func NewEntityRouteRegistrar ¶ added in v0.3.1
func NewEntityRouteRegistrar( resourceService services.ResourceService, adapterStatusService services.AdapterStatusService, schemaValidator *validators.SchemaValidator, ) RouteRegistrar
type Router ¶ added in v0.3.1
type Router struct {
// contains filtered or unexported fields
}
Router is a thin wrapper around http.ServeMux that adds gorilla/mux-style middleware chaining (.Use()) and grouping (.Group()) on top of Go's stdlib method-prefixed routing patterns (e.g. "GET /clusters/{id}").
Groups can carry a path prefix (via Group("/v1")) that is automatically prepended to every pattern registered through that group, so route handlers only declare their own path suffix.
Middlewares registered via Use() are captured at HandleFunc/Handle time, so Use() must be called before registering the routes it should apply to.
func NewRouter ¶ added in v0.3.1
func NewRouter() *Router
NewRouter creates a new top-level Router backed by a fresh http.ServeMux.
func NewRouterFromConfig ¶ added in v0.3.1
func NewRouterFromConfig( mainMiddleware []Middleware, apiMiddleware []Middleware, protectedAPIMiddleware []Middleware, authMiddleware []Middleware, registrars []RouteRegistrar, ) (*Router, error)
func (*Router) Group ¶ added in v0.3.1
Group returns a child Router sharing the same underlying ServeMux but with an independent copy of the current middleware chain, so further Use() calls on the child don't affect the parent or any sibling groups.
An optional path prefix can be passed to scope routes registered on the child. The prefix is cumulative: if a parent already carries "/api/v1" and the child adds "/clusters", routes on the child are prefixed with "/api/v1/clusters". Omitting the prefix inherits the parent's prefix as-is.
func (*Router) Handle ¶ added in v0.3.1
Handle registers handler for pattern, wrapped with this router's current middleware chain. If the router carries a prefix, it is prepended to the path component of the pattern automatically.
func (*Router) HandleFunc ¶ added in v0.3.1
func (r *Router) HandleFunc(pattern string, handler http.HandlerFunc)
HandleFunc registers handler for pattern, wrapped with this router's current middleware chain.
func (*Router) Handler ¶ added in v0.3.1
Handler returns the handler and matched pattern for req without invoking it, delegating to the underlying http.ServeMux. Mainly useful in tests that want to assert a route was registered without executing its side effects.
func (*Router) ServeHTTP ¶ added in v0.3.1
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP satisfies http.Handler, allowing a Router to be used directly as an http.Server's Handler or wrapped by further http.Handler middleware.
func (*Router) Use ¶ added in v0.3.1
func (r *Router) Use(mw ...Middleware)
Use appends middlewares to this router's chain. Only routes registered after this call (on this router or a Group() derived from it afterwards) will be wrapped by them.