Categorygithub.com/sinnott74/go-http-middleware
repositorypackage
0.0.0-20181110020803-6b384d713097
Repository: https://github.com/sinnott74/go-http-middleware.git
Documentation: pkg.go.dev

# README

go-http-middleware

Build Status

Collection of Golang HTTP middlewares fo use with Go's net/http package

  • etag Adds ETag support for each resource.

  • https Forces X-Forwarded-Proto header to be set to HTTPS. Useful when behind a load balancer i.e. aws, cloudfoundry, etc.

  • auth handles authenication using a user supplied authenication function.

  • JWT handles JWT authenication which allows a user supplied validation function.

  • Transaction creates a request scoped sql transation.

Installation

go get https://github.com/sinnott74/go-http-middleware

Example Usage

package main

import (
	"net/http"

	"github.com/sinnott74/go-http-middleware"
)

func main() {
	http.Handle("/", middleware.DefaultEtag(helloWorldHandler()))
	http.ListenAndServe(":8080", nil)
}

// Simple hello world handler
func helloWorldHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("Hello, World!"))
	})
}

// Visiting localhost:8080/ returns a response body of "Hello, world" and an ETag header 'W/"d-ZajifYh5KDgxtmS9i38K1A=="'