mirror of
https://github.com/labstack/echo.git
synced 2025-07-15 01:34:53 +02:00
Defaults for middleware config
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// BasicAuthConfig defines config for HTTP basic auth middleware.
|
// BasicAuthConfig defines the config for HTTP basic auth middleware.
|
||||||
BasicAuthConfig struct {
|
BasicAuthConfig struct {
|
||||||
// AuthFunc is the function to validate basic auth credentials.
|
// AuthFunc is the function to validate basic auth credentials.
|
||||||
AuthFunc BasicAuthFunc
|
AuthFunc BasicAuthFunc
|
||||||
|
@ -13,9 +13,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// GzipConfig defines config for gzip middleware.
|
// GzipConfig defines the config for gzip middleware.
|
||||||
GzipConfig struct {
|
GzipConfig struct {
|
||||||
// Level is the gzip level.
|
// Level is the gzip level.
|
||||||
|
// Optional with default value as `DefaultGzipConfig.Level`.
|
||||||
Level int
|
Level int
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,6 +42,11 @@ func Gzip() echo.MiddlewareFunc {
|
|||||||
// GzipFromConfig return gzip middleware from config.
|
// GzipFromConfig return gzip middleware from config.
|
||||||
// See `Gzip()`.
|
// See `Gzip()`.
|
||||||
func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc {
|
func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc {
|
||||||
|
// Defaults
|
||||||
|
if config.Level == 0 {
|
||||||
|
config.Level = DefaultGzipConfig.Level
|
||||||
|
}
|
||||||
|
|
||||||
pool := gzipPool(config)
|
pool := gzipPool(config)
|
||||||
scheme := "gzip"
|
scheme := "gzip"
|
||||||
|
|
||||||
|
@ -10,13 +10,12 @@ import (
|
|||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"github.com/labstack/gommon/color"
|
"github.com/labstack/gommon/color"
|
||||||
"github.com/mattn/go-isatty"
|
isatty "github.com/mattn/go-isatty"
|
||||||
"github.com/valyala/fasttemplate"
|
"github.com/valyala/fasttemplate"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// LoggerConfig defines config for logger middleware.
|
// LoggerConfig defines the config for logger middleware.
|
||||||
//
|
|
||||||
LoggerConfig struct {
|
LoggerConfig struct {
|
||||||
// Format is the log format which can be constructed using the following tags:
|
// Format is the log format which can be constructed using the following tags:
|
||||||
//
|
//
|
||||||
@ -30,9 +29,12 @@ type (
|
|||||||
// - response_size
|
// - response_size
|
||||||
//
|
//
|
||||||
// Example "${remote_id} ${status}"
|
// Example "${remote_id} ${status}"
|
||||||
|
//
|
||||||
|
// Optional with default value as `DefaultLoggerConfig.Format`.
|
||||||
Format string
|
Format string
|
||||||
|
|
||||||
// Output is the writer where logs are written.
|
// Output is the writer where logs are written.
|
||||||
|
// Optional with default value as `DefaultLoggerConfig.Output`.
|
||||||
Output io.Writer
|
Output io.Writer
|
||||||
|
|
||||||
template *fasttemplate.Template
|
template *fasttemplate.Template
|
||||||
@ -58,6 +60,14 @@ func Logger() echo.MiddlewareFunc {
|
|||||||
// LoggerFromConfig returns a logger middleware from config.
|
// LoggerFromConfig returns a logger middleware from config.
|
||||||
// See `Logger()`.
|
// See `Logger()`.
|
||||||
func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc {
|
func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc {
|
||||||
|
// Defaults
|
||||||
|
if config.Format == "" {
|
||||||
|
config.Format = DefaultLoggerConfig.Format
|
||||||
|
}
|
||||||
|
if config.Output == nil {
|
||||||
|
config.Output = DefaultLoggerConfig.Output
|
||||||
|
}
|
||||||
|
|
||||||
config.template = fasttemplate.New(config.Format, "${", "}")
|
config.template = fasttemplate.New(config.Format, "${", "}")
|
||||||
config.color = color.New()
|
config.color = color.New()
|
||||||
if w, ok := config.Output.(*os.File); ok && !isatty.IsTerminal(w.Fd()) {
|
if w, ok := config.Output.(*os.File); ok && !isatty.IsTerminal(w.Fd()) {
|
||||||
|
@ -9,16 +9,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// RecoverConfig defines config for recover middleware.
|
// RecoverConfig defines the config for recover middleware.
|
||||||
RecoverConfig struct {
|
RecoverConfig struct {
|
||||||
// StackSize is the stack size to be printed.
|
// StackSize is the stack size to be printed.
|
||||||
|
// Optional with default value as `DefaultRecoverConfig.StackSize`.
|
||||||
StackSize int
|
StackSize int
|
||||||
|
|
||||||
// StackAll is a flag to format stack traces of all other goroutines into
|
// StackAll is a flag to format stack traces of all other goroutines into
|
||||||
// buffer after the trace for the current goroutine, or not.
|
// buffer after the trace for the current goroutine, or not.
|
||||||
|
// Required.
|
||||||
StackAll bool
|
StackAll bool
|
||||||
|
|
||||||
// PrintStack is a flag to print stack or not.
|
// PrintStack is a flag to print stack or not.
|
||||||
|
// Required.
|
||||||
PrintStack bool
|
PrintStack bool
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -41,6 +44,11 @@ func Recover() echo.MiddlewareFunc {
|
|||||||
// RecoverFromConfig returns a recover middleware from config.
|
// RecoverFromConfig returns a recover middleware from config.
|
||||||
// See `Recover()`.
|
// See `Recover()`.
|
||||||
func RecoverFromConfig(config RecoverConfig) echo.MiddlewareFunc {
|
func RecoverFromConfig(config RecoverConfig) echo.MiddlewareFunc {
|
||||||
|
// Defaults
|
||||||
|
if config.StackSize == 0 {
|
||||||
|
config.StackSize = DefaultRecoverConfig.StackSize
|
||||||
|
}
|
||||||
|
|
||||||
return func(next echo.Handler) echo.Handler {
|
return func(next echo.Handler) echo.Handler {
|
||||||
return echo.HandlerFunc(func(c echo.Context) error {
|
return echo.HandlerFunc(func(c echo.Context) error {
|
||||||
defer func() {
|
defer func() {
|
||||||
|
@ -9,17 +9,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// StaticConfig defines config for static middleware.
|
// StaticConfig defines the config for static middleware.
|
||||||
StaticConfig struct {
|
StaticConfig struct {
|
||||||
// Root is the directory from where the static content is served.
|
// Root is the directory from where the static content is served.
|
||||||
|
// Optional with default value as `DefaultStaticConfig.Root`.
|
||||||
Root string `json:"root"`
|
Root string `json:"root"`
|
||||||
|
|
||||||
// Index is the list of index files to be searched and used when serving
|
// Index is the list of index files to be searched and used when serving
|
||||||
// a directory.
|
// a directory.
|
||||||
// Default value is `[]string{"index.html"}`.
|
// Optional with default value as `DefaultStaticConfig.Index`.
|
||||||
Index []string `json:"index"`
|
Index []string `json:"index"`
|
||||||
|
|
||||||
// Browse is a flag to enable/disable directory browsing.
|
// Browse is a flag to enable/disable directory browsing.
|
||||||
|
// Required.
|
||||||
Browse bool `json:"browse"`
|
Browse bool `json:"browse"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -44,6 +46,11 @@ func Static(root string) echo.MiddlewareFunc {
|
|||||||
// StaticFromConfig returns a static middleware from config.
|
// StaticFromConfig returns a static middleware from config.
|
||||||
// See `Static()`.
|
// See `Static()`.
|
||||||
func StaticFromConfig(config StaticConfig) echo.MiddlewareFunc {
|
func StaticFromConfig(config StaticConfig) echo.MiddlewareFunc {
|
||||||
|
// Defaults
|
||||||
|
if config.Index == nil {
|
||||||
|
config.Index = DefaultStaticConfig.Index
|
||||||
|
}
|
||||||
|
|
||||||
return func(next echo.Handler) echo.Handler {
|
return func(next echo.Handler) echo.Handler {
|
||||||
return echo.HandlerFunc(func(c echo.Context) error {
|
return echo.HandlerFunc(func(c echo.Context) error {
|
||||||
fs := http.Dir(config.Root)
|
fs := http.Dir(config.Root)
|
||||||
|
Reference in New Issue
Block a user