negroni

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2024 License: Apache-2.0 Imports: 4 Imported by: 10

Documentation

Overview

Package negroni is a helper package to get a negroni compatible middleware.

Example (NegroniMiddleware)

NegroniMiddleware shows how you would create a default middleware factory and use it to create a Negroni compatible middleware.

package main

import (
	"log"
	"net/http"

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

	metrics "github.com/slok/go-http-metrics/metrics/prometheus"
	"github.com/slok/go-http-metrics/middleware"

	negronimiddleware "github.com/slok/go-http-metrics/middleware/negroni"
)

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

	// Create our negroni instance.
	n := negroni.Classic()

	// Create our middleware.
	mdlw := middleware.New(middleware.Config{
		Recorder: metrics.NewRecorder(metrics.Config{}),
	})
	// Add the middleware to negroni.
	n.Use(negronimiddleware.Handler("", mdlw))

	// Finally set our router on negroni.
	n.UseHandler(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", n); err != nil {
		log.Panicf("error while serving: %s", err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(handlerID string, m middleware.Middleware) negroni.Handler

Handler returns a Negroni measuring middleware.

Types

This section is empty.

Jump to

Keyboard shortcuts

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