Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

aklinkert/go-httputils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

2 Commits

Repository files navigation

go-httputils

This package provides some utility wrapper for the HTTP package. The major protagonist ist the Handler object, which wraps the default http.Server with a mux.Router and takes care to measure durations with prometheus for every HTTP call.

Usage example

package main

import (
"fmt"
"net/http"
"time"

"github.com/sirupsen/logrus"
"github.com/aklinkert/go-httputils"
)

func main() {
// handler is based on logrus for logging
logger := logrus.New()
httpLogger := logger.WithField("component", "http")

// first address is the main http listener, second one is for the prometheus endpoint
// I usually have both on different ports to have the prometheus metrics endpoint not exposed but
// only serve internal traffic, e.g. only inside a kubernetes cluster or other private network.
handler := httputils.NewHandler(httpLogger, ":8080", ":9090")

// handler provides some useful Handle*() methods that support both http.HandlerFunc http.Handler using
// Handle() and HandleFunc() respectively. Also HandlePrefix() and HandleFuncPrefix() support a prefixed
// catch-all approach, e.g. when you wanna do dynamic routes that are not handled by mux (serving images or so)
handler.HandleFunc("/example", func(rw http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprintf(rw, "Hello from example, current time is %v", time.Now())
if err != nil {
logger.Error(err)
}
})

// default graceful shutdown duration is 2 seconds
handler.SetGracefulShutdownDuration(10 * time.Second)

// Serve() is blocking the main routine until a shutdown is received, using https://github.com/aklinkert/go-exitcontext
// If you want to modify the context behavior use httputils.NewHandlerWithContext(...)
handler.Serve()
}

License

Apache 2.0 Licence

About

Custom wrapper around http.Server providing routing with gorilla/mux and automatic prometheus metrics exporting per http request.

Topics

Resources

Readme

License

Apache-2.0 license

Stars

Watchers

Forks

Packages

Contributors

Languages