mux

package
v2.0.0-...-9629169 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package mux provides some basic implementations for building routers based on net/http mux

Index

Constants

View Source
const (
	DefaultDebugPattern = "/__debug/"
	DefaultEchoPattern  = "/__echo/"
)

DefaultDebugPattern is the default pattern used to define the debug endpoint

View Source
const NEGOTIATE = "negotiate"

NEGOTIATE defines the value of the OutputEncoding for the negotiated render

Variables

EndpointHandler is a HandlerFactory that adapts the mux router with the injected proxy and the default RequestBuilder

NewRequest is a RequestBuilder that creates a proxy request from the received http request without processing the uri params

Functions

func DebugHandler

func DebugHandler(logger logging.Logger) http.HandlerFunc

DebugHandler creates a dummy handler function, useful for quick integration tests

func DefaultFactory

func DefaultFactory(pf proxy.Factory, logger logging.Logger) router.Factory

DefaultFactory returns a net/http mux router factory with the injected proxy factory and logger

func EchoHandler

func EchoHandler() http.HandlerFunc

EchoHandler creates a dummy handler function, useful for quick integration tests

func HealthHandler

func HealthHandler(w http.ResponseWriter, _ *http.Request)

HealthHandler is a dummy http.HandlerFunc implementation for exposing a health check endpoint

func NewFactory

func NewFactory(cfg Config) router.Factory

NewFactory returns a net/http mux router factory with the injected configuration

func NoopParamExtractor

func NoopParamExtractor(_ *http.Request) map[string]string

NoopParamExtractor is a No Op ParamExtractor (returns an empty map of params)

func RegisterRender

func RegisterRender(name string, r Render)

RegisterRender allows clients to register their custom renders

Types

type BasicEngine

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

BasicEngine is a slightly customized http.ServeMux router

func DefaultEngine

func DefaultEngine() *BasicEngine

DefaultEngine returns a new engine using BasicEngine

func (*BasicEngine) Handle

func (e *BasicEngine) Handle(pattern, method string, handler http.Handler)

Handle registers a handler at a given url pattern and http method

func (*BasicEngine) ServeHTTP

func (e *BasicEngine) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP adds a error interceptor and delegates the request dispatching to the internal request multiplexer.

type Config

type Config struct {
	Engine         Engine
	Middlewares    []HandlerMiddleware
	HandlerFactory HandlerFactory
	ProxyFactory   proxy.Factory
	Logger         logging.Logger
	DebugPattern   string
	EchoPattern    string
	RunServer      RunServerFunc
}

Config is the struct that collects the parts the router should be builded from

type Engine

type Engine interface {
	http.Handler
	Handle(pattern, method string, handler http.Handler)
}

Engine defines the minimun required interface for the mux compatible engine

type HTTPErrorInterceptor

type HTTPErrorInterceptor struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

HTTPErrorInterceptor is a reposnse writer that adds a header signaling incomplete response in case of seeing a status code not equal to 200

func NewHTTPErrorInterceptor

func NewHTTPErrorInterceptor(w http.ResponseWriter) *HTTPErrorInterceptor

NewHTTPErrorInterceptor returns a HTTPErrorInterceptor over the injected response writer

func (*HTTPErrorInterceptor) WriteHeader

func (i *HTTPErrorInterceptor) WriteHeader(code int)

WriteHeader records the status code and adds a header signaling incomplete responses

type HandlerFactory

type HandlerFactory func(*config.EndpointConfig, proxy.Proxy) http.HandlerFunc

HandlerFactory creates a handler function that adapts the mux router with the injected proxy

func CustomEndpointHandler

func CustomEndpointHandler(rb RequestBuilder) HandlerFactory

CustomEndpointHandler returns a HandlerFactory with the received RequestBuilder using the default ToHTTPError function

func CustomEndpointHandlerWithHTTPError

func CustomEndpointHandlerWithHTTPError(rb RequestBuilder, errF server.ToHTTPError) HandlerFactory

CustomEndpointHandlerWithHTTPError returns a HandlerFactory with the received RequestBuilder

type HandlerMiddleware

type HandlerMiddleware interface {
	Handler(h http.Handler) http.Handler
}

HandlerMiddleware is the interface for the decorators over the http.Handler

type ParamExtractor

type ParamExtractor func(r *http.Request) map[string]string

ParamExtractor is a function that extracts query params from the requested uri

type Render

type Render func(http.ResponseWriter, *proxy.Response)

Render defines the signature of the functions to be use for the final response encoding and rendering

type RequestBuilder

type RequestBuilder func(r *http.Request, queryString, headersToSend []string) *proxy.Request

RequestBuilder is a function that creates a proxy.Request from the received http request

func NewRequestBuilder

func NewRequestBuilder(paramExtractor ParamExtractor) RequestBuilder

NewRequestBuilder gets a RequestBuilder with the received ParamExtractor as a query param extraction mechanism

type RunServerFunc

type RunServerFunc func(context.Context, config.ServiceConfig, http.Handler) error

RunServerFunc is a func that will run the http Server with the given params.

Jump to

Keyboard shortcuts

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