std

package
v0.10.1-20221024-1 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package std is a helper package to get a standard `http.Handler` compatible middleware.

Example (StdMiddleware)

NegroniMiddleware shows how you would create a default middleware factory and use it to create a standdard `http.Handler` compatible middleware.

package main

import (
	"log"
	"net/http"

	"github.com/prometheus/client_golang/prometheus/promhttp"

	metrics "github.com/aserto-dev/go-http-metrics/metrics/prometheus"
	"github.com/aserto-dev/go-http-metrics/middleware"

	stdmiddleware "github.com/aserto-dev/go-http-metrics/middleware/std"
)

func main() {
	// Create our middleware factory with the default settings.
	mdlw := middleware.New(middleware.Config{
		Recorder: metrics.NewRecorder(metrics.Config{}),
	})

	// Create our handler.
	myHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		_, _ = w.Write([]byte("hello world!"))
	})

	// Wrap our handler with the middleware.
	h := stdmiddleware.Handler("", mdlw, myHandler)

	// Serve metrics from the default prometheus registry.
	log.Printf("serving metrics at: %s", ":8081")
	go func() {
		_ = http.ListenAndServe(":8081", promhttp.Handler())
	}()

	// Serve our handler.
	log.Printf("listening at: %s", ":8080")
	if err := http.ListenAndServe(":8080", h); err != nil {
		log.Panicf("error while serving: %s", err)
	}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(handlerID string, m middleware.Middleware, h http.Handler) http.Handler

Handler returns an measuring standard http.Handler.

func HandlerProvider

func HandlerProvider(handlerID string, m middleware.Middleware) func(http.Handler) http.Handler

HandlerProvider is a helper method that returns a handler provider. This kind of provider is a defacto standard in some frameworks (e.g: Gorilla, Chi...).

Types

type CapturedResponse

type CapturedResponse interface {
	StatusCode() int
	BytesWritten() int64
}

type ResponseWriterInterceptor

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

ResponseWriterInterceptor is a simple wrapper to intercept set data on a ResponseWriter.

func NewResponseWriterInterceptor

func NewResponseWriterInterceptor(w http.ResponseWriter) *ResponseWriterInterceptor

func (*ResponseWriterInterceptor) BytesWritten

func (w *ResponseWriterInterceptor) BytesWritten() int64

func (*ResponseWriterInterceptor) Flush

func (w *ResponseWriterInterceptor) Flush()

func (*ResponseWriterInterceptor) Hijack

func (*ResponseWriterInterceptor) StatusCode

func (w *ResponseWriterInterceptor) StatusCode() int

func (*ResponseWriterInterceptor) Write

func (w *ResponseWriterInterceptor) Write(p []byte) (int, error)

func (*ResponseWriterInterceptor) WriteHeader

func (w *ResponseWriterInterceptor) WriteHeader(statusCode int)

Jump to

Keyboard shortcuts

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