fasthttp

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: Apache-2.0 Imports: 3 Imported by: 3

Documentation

Overview

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

Example (FasthttpMiddleware)

FasthttpMiddleware shows how you would create a default middleware factory and use it to create a fasthttp compatible middleware.

package main

import (
	"log"
	"net/http"

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

	fasthttpMiddleware "github.com/slok/go-http-metrics/middleware/fasthttp"
	"github.com/valyala/fasthttp"
)

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

	// Add our handler and middleware
	h := func(rCtx *fasthttp.RequestCtx) {
		rCtx.SetStatusCode(fasthttp.StatusOK)
		rCtx.SetBodyString("OK")
	}

	// Create our fasthttp instance.
	srv := &fasthttp.Server{
		Handler: fasthttpMiddleware.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 := srv.ListenAndServe(":8080"); 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

Handler returns a fasthttp 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