gorestful

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2021 License: Apache-2.0 Imports: 3 Imported by: 3

Documentation

Overview

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

Example (GorestfulMiddleware)

GorestfulMiddleware shows how you would create a default middleware factory and use it to create a Gorestful compatible middleware.

package main

import (
	"log"
	"net/http"

	gorestful "github.com/emicklei/go-restful/v3"
	"github.com/prometheus/client_golang/prometheus/promhttp"

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

	gorestfulmiddleware "github.com/slok/go-http-metrics/middleware/gorestful"
)

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

	// Create our gorestful instance.
	c := gorestful.NewContainer()

	// Add the middleware for all routes.
	c.Filter(gorestfulmiddleware.Handler("", mdlw))

	// Add our handler,
	ws := &gorestful.WebService{}
	ws.Route(ws.GET("/").To(func(_ *gorestful.Request, resp *gorestful.Response) {
		_ = resp.WriteEntity("Hello world")
	}))
	c.Add(ws)

	// 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", c); 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) gorestful.FilterFunction

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