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