mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-25 17:42:44 +00:00
feat(config): add support for log filters
Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
@@ -326,6 +326,7 @@ func (sc StatusCodes) Valid() error {
|
|||||||
type fileConfig struct {
|
type fileConfig struct {
|
||||||
Bots []BotOrImport `json:"bots"`
|
Bots []BotOrImport `json:"bots"`
|
||||||
DNSBL bool `json:"dnsbl"`
|
DNSBL bool `json:"dnsbl"`
|
||||||
|
Logging *Logging `json:"logging"`
|
||||||
OpenGraph openGraphFileConfig `json:"openGraph,omitempty"`
|
OpenGraph openGraphFileConfig `json:"openGraph,omitempty"`
|
||||||
Impressum *Impressum `json:"impressum,omitempty"`
|
Impressum *Impressum `json:"impressum,omitempty"`
|
||||||
StatusCodes StatusCodes `json:"status_codes"`
|
StatusCodes StatusCodes `json:"status_codes"`
|
||||||
@@ -368,6 +369,12 @@ func (c *fileConfig) Valid() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.Logging != nil {
|
||||||
|
if err := c.Logging.Valid(); err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(errs) != 0 {
|
if len(errs) != 0 {
|
||||||
return fmt.Errorf("config is not valid:\n%w", errors.Join(errs...))
|
return fmt.Errorf("config is not valid:\n%w", errors.Join(errs...))
|
||||||
}
|
}
|
||||||
@@ -401,6 +408,7 @@ func Load(fin io.Reader, fname string) (*Config, error) {
|
|||||||
ConsiderHost: c.OpenGraph.ConsiderHost,
|
ConsiderHost: c.OpenGraph.ConsiderHost,
|
||||||
Override: c.OpenGraph.Override,
|
Override: c.OpenGraph.Override,
|
||||||
},
|
},
|
||||||
|
Logging: c.Logging,
|
||||||
StatusCodes: c.StatusCodes,
|
StatusCodes: c.StatusCodes,
|
||||||
Store: c.Store,
|
Store: c.Store,
|
||||||
}
|
}
|
||||||
@@ -441,6 +449,12 @@ func Load(fin io.Reader, fname string) (*Config, error) {
|
|||||||
result.Impressum = c.Impressum
|
result.Impressum = c.Impressum
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.Logging != nil {
|
||||||
|
if err := c.Logging.Valid(); err != nil {
|
||||||
|
validationErrs = append(validationErrs, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(c.Thresholds) == 0 {
|
if len(c.Thresholds) == 0 {
|
||||||
c.Thresholds = DefaultThresholds
|
c.Thresholds = DefaultThresholds
|
||||||
}
|
}
|
||||||
@@ -465,6 +479,7 @@ type Config struct {
|
|||||||
Bots []BotConfig
|
Bots []BotConfig
|
||||||
Thresholds []Threshold
|
Thresholds []Threshold
|
||||||
DNSBL bool
|
DNSBL bool
|
||||||
|
Logging *Logging
|
||||||
Impressum *Impressum
|
Impressum *Impressum
|
||||||
OpenGraph OpenGraph
|
OpenGraph OpenGraph
|
||||||
StatusCodes StatusCodes
|
StatusCodes StatusCodes
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Logging struct {
|
||||||
|
Filters []LogFilter `json:"filters,omitempty" yaml:"filters,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logging) Valid() error {
|
||||||
|
var errs []error
|
||||||
|
|
||||||
|
for _, lf := range l.Filters {
|
||||||
|
if err := lf.Valid(); err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(errs) != 0 {
|
||||||
|
return errors.Join(errs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type LogFilter struct {
|
||||||
|
Name string `json:"name" yaml:"name"`
|
||||||
|
Expression ExpressionOrList `json:"expression" yaml:"expression"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lf LogFilter) Valid() error {
|
||||||
|
var errs []error
|
||||||
|
|
||||||
|
if lf.Name == "" {
|
||||||
|
errs = append(errs, fmt.Errorf("%w: log filter has no name", ErrMissingValue))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := lf.Expression.Valid(); err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(errs) != 0 {
|
||||||
|
return fmt.Errorf("log filter %q is not valid: %w", lf.Name, errors.Join(errs...))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user