middleware

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2020 License: BSD-3-Clause Imports: 4 Imported by: 0

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())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BasicAuth

func BasicAuth(validate BasicAuthValidate, opts ...BasicAuthOption) func(http.Handler) http.Handler

BasicAuth returns a basic auth middleware.

func BasicAuthHandler

func BasicAuthHandler(next http.Handler, validate BasicAuthValidate, opts ...BasicAuthOption) http.Handler

BasicAuthHandler returns a basic auth handler.

func CombinedLogging

func CombinedLogging(out io.Writer) func(http.Handler) http.Handler

CombinedLogging is a combined logging middleware.

func Compress

func Compress(level int) func(http.Handler) http.Handler

Compress is a compress middleware.

func CustomerLogging

func CustomerLogging(out io.Writer, formatter handlers.LogFormatter) func(http.Handler) http.Handler

CustomerLogging is a customer logging middleware.

func GetBasicAuthUser

func GetBasicAuthUser(r *http.Request) string

GetBasicAuthUser returns the basic auth username.

func Header(f HeaderFunc) func(http.Handler) http.Handler

Header is a HTTP midldeware that changes response header.

func Logging

func Logging(out io.Writer) func(http.Handler) http.Handler

Logging is a logging middleware.

Types

type BasicAuthOption

type BasicAuthOption func(*basicAuth)

BasicAuthOption is a function that apply on basic auth.

func BasicAuthErrorHandler

func BasicAuthErrorHandler(h http.Handler) BasicAuthOption

BasicAuthErrorHandler is an option to handle validation failed.

func BasicAuthRealm

func BasicAuthRealm(v string) BasicAuthOption

BasicAuthRealm is an option to sets realm.

type BasicAuthValidate

type BasicAuthValidate func(username, password string) bool

BasicAuthValidate is a function that validate username and password.

type HeaderFunc

type HeaderFunc func(http.Header)

HeaderFunc is a function that recieves a http.Header.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL