mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-19 16:21:43 +01:00
* Add comprehensive post-request rules support for response phase * Enable response body, status, and header manipulation via set commands * Refactor command handlers to support both request and response phases * Implement response modifier system for post-request template execution * Support response-based rule matching with status and header checks * Add comprehensive benchmarks for matcher performance * Refactor authentication and proxying commands for unified error handling * Support negated conditions with ! * Enhance error handling, error formatting and validation * Routes: add `rule_file` field with rule preset support * Environment variable substitution: now supports variables without `GODOXY_` prefix * new conditions: * `on resp_header <key> [<value>]` * `on status <status>` * new commands: * `require_auth` * `set resp_header <key> <template>` * `set resp_body <template>` * `set status <code>` * `log <level> <path> <template>` * `notify <level> <provider> <title_template> <body_template>`
127 lines
3.0 KiB
Go
127 lines
3.0 KiB
Go
package accesslog
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/yusing/godoxy/internal/serialization"
|
|
gperr "github.com/yusing/goutils/errs"
|
|
)
|
|
|
|
type (
|
|
ConfigBase struct {
|
|
B int `json:"buffer_size"` // Deprecated: buffer size is adjusted dynamically
|
|
Path string `json:"path"`
|
|
Stdout bool `json:"stdout"`
|
|
Retention *Retention `json:"retention" aliases:"keep"`
|
|
RotateInterval time.Duration `json:"rotate_interval,omitempty" swaggertype:"primitive,integer"`
|
|
}
|
|
ACLLoggerConfig struct {
|
|
ConfigBase
|
|
LogAllowed bool `json:"log_allowed"`
|
|
}
|
|
RequestLoggerConfig struct {
|
|
ConfigBase
|
|
Format Format `json:"format" validate:"oneof=common combined json"`
|
|
Filters Filters `json:"filters"`
|
|
Fields Fields `json:"fields"`
|
|
} // @name RequestLoggerConfig
|
|
Config struct {
|
|
ConfigBase
|
|
acl *ACLLoggerConfig
|
|
req *RequestLoggerConfig
|
|
}
|
|
AnyConfig interface {
|
|
ToConfig() *Config
|
|
IO() (WriterWithName, error)
|
|
}
|
|
|
|
Format string
|
|
Filters struct {
|
|
StatusCodes LogFilter[*StatusCodeRange] `json:"status_codes"`
|
|
Method LogFilter[HTTPMethod] `json:"method"`
|
|
Host LogFilter[Host] `json:"host"`
|
|
Headers LogFilter[*HTTPHeader] `json:"headers"` // header exists or header == value
|
|
CIDR LogFilter[*CIDR] `json:"cidr"`
|
|
}
|
|
Fields struct {
|
|
Headers FieldConfig `json:"headers" aliases:"header"`
|
|
Query FieldConfig `json:"query" aliases:"queries"`
|
|
Cookies FieldConfig `json:"cookies" aliases:"cookie"`
|
|
}
|
|
)
|
|
|
|
var (
|
|
FormatCommon Format = "common"
|
|
FormatCombined Format = "combined"
|
|
FormatJSON Format = "json"
|
|
|
|
ReqLoggerFormats = []Format{FormatCommon, FormatCombined, FormatJSON}
|
|
)
|
|
|
|
func (cfg *ConfigBase) Validate() gperr.Error {
|
|
if cfg.Path == "" && !cfg.Stdout {
|
|
return gperr.New("path or stdout is required")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// IO returns a writer for the config.
|
|
// If only stdout is enabled, it returns nil, nil.
|
|
func (cfg *ConfigBase) IO() (WriterWithName, error) {
|
|
if cfg.Path != "" {
|
|
io, err := NewFileIO(cfg.Path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return io, nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (cfg *ACLLoggerConfig) ToConfig() *Config {
|
|
return &Config{
|
|
ConfigBase: cfg.ConfigBase,
|
|
acl: cfg,
|
|
}
|
|
}
|
|
|
|
func (cfg *RequestLoggerConfig) ToConfig() *Config {
|
|
return &Config{
|
|
ConfigBase: cfg.ConfigBase,
|
|
req: cfg,
|
|
}
|
|
}
|
|
|
|
func DefaultRequestLoggerConfig() *RequestLoggerConfig {
|
|
return &RequestLoggerConfig{
|
|
ConfigBase: ConfigBase{
|
|
Retention: &Retention{Days: 30},
|
|
},
|
|
Format: FormatCombined,
|
|
Fields: Fields{
|
|
Headers: FieldConfig{
|
|
Default: FieldModeDrop,
|
|
},
|
|
Query: FieldConfig{
|
|
Default: FieldModeKeep,
|
|
},
|
|
Cookies: FieldConfig{
|
|
Default: FieldModeDrop,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func DefaultACLLoggerConfig() *ACLLoggerConfig {
|
|
return &ACLLoggerConfig{
|
|
ConfigBase: ConfigBase{
|
|
Retention: &Retention{Days: 30},
|
|
},
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
serialization.RegisterDefaultValueFactory(DefaultRequestLoggerConfig)
|
|
serialization.RegisterDefaultValueFactory(DefaultACLLoggerConfig)
|
|
}
|