Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CorrelationId ¶ added in v0.4.0
func CorrelationId(config *correlation_id.Config) httpware.Middleware
CorrelationId middleware get request id header if provided or generate a request id It will add the request ID to request context and add it to response header to
Example ¶
package main import ( "fmt" "net/http" "github.com/gol4ng/httpware" "github.com/gol4ng/httpware/correlation_id" "github.com/gol4ng/httpware/middleware" ) func main() { port := ":5001" config := correlation_id.NewConfig() // you can override default header name config.HeaderName = "my-personal-header-name" // you can override default id generator config.IdGenerator = func(request *http.Request) string { return "my-fixed-request-id" } // we recommend to use MiddlewareStack to simplify managing all wanted middlewares // caution middleware order matters stack := httpware.MiddlewareStack( middleware.CorrelationId(config), ) srv := http.NewServeMux() go func() { if err := http.ListenAndServe(port, stack.DecorateHandler(srv)); err != nil { panic(err) } }() resp, err := http.Get("http://localhost" + port) fmt.Printf("%s: %v %v\n", config.HeaderName, resp.Header.Get(config.HeaderName), err) }
Output: my-personal-header-name: my-fixed-request-id <nil>
func Metrics ¶
func Metrics(config *metrics.Config) httpware.Middleware
Example ¶
package main import ( "net/http" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/gol4ng/httpware" "github.com/gol4ng/httpware/metrics" prom "github.com/gol4ng/httpware/metrics/prometheus" "github.com/gol4ng/httpware/middleware" ) func main() { port := ":5002" recorder := prom.NewRecorder(prom.Config{}).RegisterOn(nil) metricsConfig := metrics.NewConfig(recorder) // you can override default identifier provider metricsConfig.IdentifierProvider = func(req *http.Request) string { return req.URL.Host + " -> " + req.URL.Path } // we recommend to use MiddlewareStack to simplify managing all wanted middleware // caution middleware order matter stack := httpware.MiddlewareStack( middleware.Metrics(metricsConfig), ) srv := http.NewServeMux() srv.Handle("/metrics", promhttp.Handler()) go func() { if err := http.ListenAndServe(port, stack.DecorateHandler(srv)); err != nil { panic(err) } }() }
Output:
func NewResponseWriterInterceptor ¶
func NewResponseWriterInterceptor(writer http.ResponseWriter) *responseWriterInterceptor
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.