middleware

package
v0.0.0-...-99b33a3 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package middleware is the middlewares of takanawa.

Index

Constants

This section is empty.

Variables

View Source
var (
	// CommonLogFormat formats access logs according to the
	// Apache's Common Log Format.
	//
	// Common Log Format: https://httpd.apache.org/docs/2.4/en/logs.html#common
	CommonLogFormat = func(t *time.Time, w *util.ResponseSniffer, r *http.Request) string {
		remoteAddr := strings.SplitN(r.RemoteAddr, ":", 2)
		return fmt.Sprintf(
			"%s - - [%s] \"%s %s %s\" %d %d",
			remoteAddr[0],
			t.Format("02/Jan/2006:15:04:05 -0700"),
			r.Method,
			r.RequestURI,
			r.Proto,
			w.StatusCode,
			w.Length,
		)
	}

	// CombinedLogFormat formats access logs according to the
	// Apache's Combined Log Format.
	//
	// Combined Log Format: https://httpd.apache.org/docs/2.4/en/logs.html#combined
	CombinedLogFormat = func(t *time.Time, w *util.ResponseSniffer, r *http.Request) string {
		referer := r.Referer()
		if len(referer) == 0 {
			referer = "-"
		}
		userAgent := r.UserAgent()
		if len(userAgent) == 0 {
			userAgent = "-"
		}
		return CommonLogFormat(t, w, r) + fmt.Sprintf(" \"%s\" \"%s\"", referer, userAgent)
	}
)
View Source
var (
	// DefaultAccessLogOptions is used by AccessLog as default options.
	DefaultAccessLogOptions = AccessLogOptions{
		Logger: log.New(ioutil.Discard, "", 0),
		Format: CommonLogFormat,
	}
)

Functions

func AccessLog

func AccessLog(opt *AccessLogOptions) takanawa.Middleware

AccessLog returns the middleware.

func ChangeOrigin

func ChangeOrigin(host string) takanawa.Middleware

ChangeOrigin changes the Host request header.

func Cors

func Cors(opt *CorsOption) takanawa.Middleware

Cors returns the middleware.

func RequestID

func RequestID() takanawa.Middleware

RequestID returns the middleware.

func ReverseProxy

func ReverseProxy(target *url.URL) takanawa.Middleware

ReverseProxy returns the middleware that invokes httputil.ReverseProxy.

func StripPrefix

func StripPrefix(prefix string) takanawa.Middleware

StripPrefix returns a middleware that invokes http.StripPrefix(prefix, handler).

Types

type AccessLogFormat

type AccessLogFormat func(t *time.Time, s *util.ResponseSniffer, r *http.Request) string

AccessLogFormat formats access logs.

type AccessLogOptions

type AccessLogOptions struct {
	Logger *log.Logger
	Format AccessLogFormat
}

AccessLogOptions is options of AccessLog.

type CorsOption

type CorsOption struct {
	takanawa.MiddlewareFunc
	AllowedOrigins   []string
	AllowedMethods   []string
	AllowedHeaders   []string
	ExposedHeaders   []string
	AllowCredentials bool
}

CorsOption is options of Cors.

Jump to

Keyboard shortcuts

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