Categorygithub.com/clevergo/middleware
modulepackage
1.0.1
Repository: https://github.com/clevergo/middleware.git
Documentation: pkg.go.dev

# README

Middleware Build Status Coverage Status Go Report Card GoDoc Release

Collections of HTTP middlewares.

Middlewares

Example

package main

import (
	"compress/gzip"
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/clevergo/clevergo"
	"github.com/clevergo/middleware"
	"github.com/gorilla/handlers"
)

var users = map[string]string{
	"foo": "bar",
}

func main() {
	app := clevergo.New(":1234")
	app.Use(
		handlers.RecoveryHandler(),
		middleware.Compress(gzip.DefaultCompression),
		middleware.Logging(os.Stdout),
	)
	app.Get("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "hello world")
	})
	auth := app.Group("/auth", clevergo.RouteGroupMiddleware(
		middleware.BasicAuth(func(username, password string) bool {
			if passwd, exists := users[username]; exists && passwd == password {
				return true
			}
			return false
		}),
	))
	auth.Get("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "authenticated")
	})
	log.Fatal(app.ListenAndServe())
}

# Functions

BasicAuth returns a basic auth middleware.
BasicAuthErrorHandler is an option to handle validation failed.
BasicAuthHandler returns a basic auth handler.
BasicAuthRealm is an option to sets realm.
CombinedLogging is a combined logging middleware.
Compress is a compress middleware.
CustomerLogging is a customer logging middleware.
GetBasicAuthUser returns the basic auth username.
Header is a HTTP midldeware that changes response header.
Logging is a logging middleware.

# Type aliases

BasicAuthOption is a function that apply on basic auth.
BasicAuthValidate is a function that validate username and password.
HeaderFunc is a function that recieves a http.Header.