mirror of
https://github.com/hacdias/webdav.git
synced 2026-09-22 03:20:41 +08:00
feat!: further log customizations
This commit is contained in:
@@ -81,6 +81,31 @@ modify: true
|
|||||||
# Default permissions rules to apply at the paths.
|
# Default permissions rules to apply at the paths.
|
||||||
rules: []
|
rules: []
|
||||||
|
|
||||||
|
# Logging configuration
|
||||||
|
log:
|
||||||
|
# Logging format ('console', 'json'). Default is 'console'.
|
||||||
|
format: console
|
||||||
|
# Enable or disable colors. Default is 'true'. Only applied if format is 'console'.
|
||||||
|
colors: true
|
||||||
|
# Logging outputs. You can have more than one output. Default is only 'stderr'.
|
||||||
|
outputs:
|
||||||
|
- stderr
|
||||||
|
|
||||||
|
# CORS configuration
|
||||||
|
cors:
|
||||||
|
# Whether or not CORS configuration should be applied. Default is 'false'.
|
||||||
|
enabled: true
|
||||||
|
credentials: true
|
||||||
|
allowed_headers:
|
||||||
|
- Depth
|
||||||
|
allowed_hosts:
|
||||||
|
- http://localhost:8080
|
||||||
|
allowed_methods:
|
||||||
|
- GET
|
||||||
|
exposed_headers:
|
||||||
|
- Content-Length
|
||||||
|
- Content-Range
|
||||||
|
|
||||||
# The list of users. If users is empty, then there will be no authentication.
|
# The list of users. If users is empty, then there will be no authentication.
|
||||||
users:
|
users:
|
||||||
# Example 'admin' user with plaintext password.
|
# Example 'admin' user with plaintext password.
|
||||||
@@ -108,20 +133,6 @@ users:
|
|||||||
# a regular expression.
|
# a regular expression.
|
||||||
- regex: "^.+\.js$"
|
- regex: "^.+\.js$"
|
||||||
modify: true
|
modify: true
|
||||||
|
|
||||||
# CORS configuration
|
|
||||||
cors:
|
|
||||||
enabled: true
|
|
||||||
credentials: true
|
|
||||||
allowed_headers:
|
|
||||||
- Depth
|
|
||||||
allowed_hosts:
|
|
||||||
- http://localhost:8080
|
|
||||||
allowed_methods:
|
|
||||||
- GET
|
|
||||||
exposed_headers:
|
|
||||||
- Content-Length
|
|
||||||
- Content-Range
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### CORS
|
### CORS
|
||||||
|
|||||||
+7
-24
@@ -13,19 +13,17 @@ import (
|
|||||||
"github.com/hacdias/webdav/v5/lib"
|
"github.com/hacdias/webdav/v5/lib"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"go.uber.org/zap/zapcore"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flags := rootCmd.Flags()
|
flags := rootCmd.Flags()
|
||||||
flags.StringP("config", "c", "", "config file path")
|
flags.StringP("config", "c", "", "config file path")
|
||||||
|
flags.StringP("address", "a", lib.DefaultAddress, "address to listen on")
|
||||||
|
flags.IntP("port", "p", lib.DefaultPort, "port to listen on")
|
||||||
flags.BoolP("tls", "t", lib.DefaultTLS, "enable TLS")
|
flags.BoolP("tls", "t", lib.DefaultTLS, "enable TLS")
|
||||||
flags.String("cert", lib.DefaultCert, "path to TLS certificate")
|
flags.String("cert", lib.DefaultCert, "path to TLS certificate")
|
||||||
flags.String("key", lib.DefaultKey, "path to TLS key")
|
flags.String("key", lib.DefaultKey, "path to TLS key")
|
||||||
flags.StringP("address", "a", lib.DefaultAddress, "address to listen on")
|
|
||||||
flags.IntP("port", "p", lib.DefaultPort, "port to listen on")
|
|
||||||
flags.StringP("prefix", "P", lib.DefaultPrefix, "URL path prefix")
|
flags.StringP("prefix", "P", lib.DefaultPrefix, "URL path prefix")
|
||||||
flags.String("log_format", lib.DefaultLogFormat, "logging format")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
@@ -57,14 +55,15 @@ set WD_CERT.`,
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create HTTP handler from the config
|
// Setup the logger based on the configuration
|
||||||
handler, err := lib.NewHandler(cfg)
|
logger, err := cfg.GetLogger()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
zap.ReplaceGlobals(logger)
|
||||||
|
|
||||||
// Setup the logger based on the configuration
|
// Create HTTP handler from the config
|
||||||
err = setupLogger(cfg)
|
handler, err := lib.NewHandler(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -126,19 +125,3 @@ func getListener(cfg *lib.Config) (net.Listener, error) {
|
|||||||
|
|
||||||
return net.Listen(network, address)
|
return net.Listen(network, address)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupLogger(cfg *lib.Config) error {
|
|
||||||
loggerConfig := zap.NewProductionConfig()
|
|
||||||
loggerConfig.DisableCaller = true
|
|
||||||
if cfg.Debug {
|
|
||||||
loggerConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
|
|
||||||
}
|
|
||||||
loggerConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
|
||||||
loggerConfig.Encoding = cfg.LogFormat
|
|
||||||
logger, err := loggerConfig.Build()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
zap.ReplaceGlobals(logger)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
+36
-26
@@ -10,20 +10,16 @@ import (
|
|||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
"go.uber.org/zap/zapcore"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DefaultDirectory = "."
|
DefaultTLS = false
|
||||||
DefaultModify = false
|
DefaultCert = "cert.pem"
|
||||||
DefaultDebug = false
|
DefaultKey = "key.pem"
|
||||||
DefaultNoSniff = false
|
DefaultAddress = "0.0.0.0"
|
||||||
DefaultTLS = false
|
DefaultPort = 6065
|
||||||
DefaultCert = "cert.pem"
|
DefaultPrefix = "/"
|
||||||
DefaultKey = "key.pem"
|
|
||||||
DefaultAddress = "0.0.0.0"
|
|
||||||
DefaultPort = 6065
|
|
||||||
DefaultPrefix = "/"
|
|
||||||
DefaultLogFormat = "console"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@@ -36,7 +32,7 @@ type Config struct {
|
|||||||
Key string
|
Key string
|
||||||
Prefix string
|
Prefix string
|
||||||
NoSniff bool
|
NoSniff bool
|
||||||
LogFormat string `mapstructure:"log_format"`
|
Log Log
|
||||||
CORS CORS
|
CORS CORS
|
||||||
Users []User
|
Users []User
|
||||||
}
|
}
|
||||||
@@ -50,11 +46,6 @@ func ParseConfig(filename string, flags *pflag.FlagSet) (*Config, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = v.BindPFlag("LogFormat", flags.Lookup("log_format"))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configuration file settings
|
// Configuration file settings
|
||||||
@@ -74,19 +65,21 @@ func ParseConfig(filename string, flags *pflag.FlagSet) (*Config, error) {
|
|||||||
// empty or false.
|
// empty or false.
|
||||||
|
|
||||||
// Defaults shared with flags
|
// Defaults shared with flags
|
||||||
v.SetDefault("Directory", DefaultDirectory)
|
|
||||||
v.SetDefault("Modify", DefaultModify)
|
|
||||||
v.SetDefault("Debug", DefaultDebug)
|
|
||||||
v.SetDefault("NoSniff", DefaultNoSniff)
|
|
||||||
v.SetDefault("TLS", DefaultTLS)
|
v.SetDefault("TLS", DefaultTLS)
|
||||||
v.SetDefault("Cert", DefaultCert)
|
v.SetDefault("Cert", DefaultCert)
|
||||||
v.SetDefault("Key", DefaultKey)
|
v.SetDefault("Key", DefaultKey)
|
||||||
v.SetDefault("Address", DefaultAddress)
|
v.SetDefault("Address", DefaultAddress)
|
||||||
v.SetDefault("Port", DefaultPort)
|
v.SetDefault("Port", DefaultPort)
|
||||||
v.SetDefault("Prefix", DefaultPrefix)
|
v.SetDefault("Prefix", DefaultPrefix)
|
||||||
v.SetDefault("Log_Format", DefaultLogFormat)
|
|
||||||
|
|
||||||
// Other defaults
|
// Other defaults
|
||||||
|
v.SetDefault("Directory", ".")
|
||||||
|
v.SetDefault("Modify", false)
|
||||||
|
v.SetDefault("Debug", false)
|
||||||
|
v.SetDefault("NoSniff", false)
|
||||||
|
v.SetDefault("Log.Format", "console")
|
||||||
|
v.SetDefault("Log.Outputs", []string{"stderr"})
|
||||||
|
v.SetDefault("Log.Colors", true)
|
||||||
v.SetDefault("CORS.Allowed_Headers", []string{"*"})
|
v.SetDefault("CORS.Allowed_Headers", []string{"*"})
|
||||||
v.SetDefault("CORS.Allowed_Hosts", []string{"*"})
|
v.SetDefault("CORS.Allowed_Hosts", []string{"*"})
|
||||||
v.SetDefault("CORS.Allowed_Methods", []string{"*"})
|
v.SetDefault("CORS.Allowed_Methods", []string{"*"})
|
||||||
@@ -135,10 +128,6 @@ func ParseConfig(filename string, flags *pflag.FlagSet) (*Config, error) {
|
|||||||
func (c *Config) Validate() error {
|
func (c *Config) Validate() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if len(c.Users) == 0 {
|
|
||||||
zap.L().Warn("unprotected config: no users have been set, so no authentication will be used")
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Directory, err = filepath.Abs(c.Directory)
|
c.Directory, err = filepath.Abs(c.Directory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("invalid config: %w", err)
|
return fmt.Errorf("invalid config: %w", err)
|
||||||
@@ -179,6 +168,27 @@ func (c *Config) Validate() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cfg *Config) GetLogger() (*zap.Logger, error) {
|
||||||
|
loggerConfig := zap.NewProductionConfig()
|
||||||
|
loggerConfig.DisableCaller = true
|
||||||
|
if cfg.Debug {
|
||||||
|
loggerConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
|
||||||
|
}
|
||||||
|
if cfg.Log.Colors && cfg.Log.Format != "json" {
|
||||||
|
loggerConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||||
|
}
|
||||||
|
loggerConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||||
|
loggerConfig.Encoding = cfg.Log.Format
|
||||||
|
loggerConfig.OutputPaths = cfg.Log.Outputs
|
||||||
|
return loggerConfig.Build()
|
||||||
|
}
|
||||||
|
|
||||||
|
type Log struct {
|
||||||
|
Format string
|
||||||
|
Colors bool
|
||||||
|
Outputs []string
|
||||||
|
}
|
||||||
|
|
||||||
type CORS struct {
|
type CORS struct {
|
||||||
Enabled bool
|
Enabled bool
|
||||||
Credentials bool
|
Credentials bool
|
||||||
|
|||||||
+3
-1
@@ -32,7 +32,9 @@ func TestConfigDefaults(t *testing.T) {
|
|||||||
require.EqualValues(t, DefaultAddress, cfg.Address)
|
require.EqualValues(t, DefaultAddress, cfg.Address)
|
||||||
require.EqualValues(t, DefaultPort, cfg.Port)
|
require.EqualValues(t, DefaultPort, cfg.Port)
|
||||||
require.EqualValues(t, DefaultPrefix, cfg.Prefix)
|
require.EqualValues(t, DefaultPrefix, cfg.Prefix)
|
||||||
require.EqualValues(t, DefaultLogFormat, cfg.LogFormat)
|
require.EqualValues(t, "console", cfg.Log.Format)
|
||||||
|
require.EqualValues(t, true, cfg.Log.Colors)
|
||||||
|
require.EqualValues(t, []string{"stderr"}, cfg.Log.Outputs)
|
||||||
|
|
||||||
dir, err := os.Getwd()
|
dir, err := os.Getwd()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|||||||
@@ -61,6 +61,10 @@ func NewHandler(c *Config) (http.Handler, error) {
|
|||||||
}).Handler(h), nil
|
}).Handler(h), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(c.Users) == 0 {
|
||||||
|
zap.L().Warn("unprotected config: no users have been set, so no authentication will be used")
|
||||||
|
}
|
||||||
|
|
||||||
return h, nil
|
return h, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user