gin

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2020 License: Apache-2.0 Imports: 3 Imported by: 17

Documentation

Overview

Package gin is a helper package to get a gin compatible handler/middleware from the standard net/http Middleware factory.

Example (GinMiddleware)

GinMiddleware shows how you would create a default middleware factory and use it to create a Gin compatible middleware.

package main

import (
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/prometheus/client_golang/prometheus/promhttp"

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

	ginmiddleware "github.com/slok/go-http-metrics/middleware/gin"
)

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

	// Create our gin instance.
	engine := gin.New()

	// Add our handler and middleware
	h := func(c *gin.Context) {
		c.String(http.StatusOK, "Hello world")
	}
	engine.GET("/", ginmiddleware.Handler("", mdlw), h)

	// 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", engine); 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) gin.HandlerFunc

Handler returns a Gin compatible middleware from a Middleware factory instance. The first handlerID argument is the same argument passed on Middleware.Handler method.

Types

This section is empty.

Jump to

Keyboard shortcuts

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