feat: add option to disable plugin

This commit is contained in:
Oleh Astappiev 2024-10-16 16:32:09 +02:00
parent 3e5c982284
commit 9d645b5c7e
No known key found for this signature in database
GPG key ID: 37D7AC22589E9632
2 changed files with 45 additions and 36 deletions

View file

@ -7,6 +7,7 @@ import: github.com/astappiev/traefik-umami-feeder
summary: 'Traefik plugin that sends HTTP requests to Umami as pageview events.'
testData:
disabled: false
umamiHost: ""
umamiToken: ""
umamiUsername: ""

View file

@ -11,6 +11,7 @@ import (
// Config the plugin configuration.
type Config struct {
Disabled bool `json:"disabled"`
UmamiHost string `json:"umamiHost"`
// it is optional, but either UmamiToken or Websites should be set
UmamiToken string `json:"umamiToken"`
@ -28,6 +29,7 @@ type Config struct {
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
Disabled: false,
UmamiHost: "",
UmamiToken: "",
UmamiUsername: "",
@ -44,6 +46,7 @@ type UmamiFeeder struct {
next http.Handler
name string
debug bool
disabled bool
logHandler *log.Logger
UmamiHost string
@ -60,6 +63,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
next: next,
name: name,
debug: config.Debug,
disabled: config.Disabled,
logHandler: log.New(os.Stdout, "", 0),
UmamiHost: config.UmamiHost,
@ -69,6 +73,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
CreateNewWebsites: config.CreateNewWebsites,
}
if !h.disabled {
if config.UmamiUsername != "" && config.UmamiPassword != "" {
token, err := getToken(h.UmamiHost, config.UmamiUsername, config.UmamiPassword)
if err != nil {
@ -107,16 +112,19 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
}
h.log("websites fetched")
}
}
return h, nil
}
func (h *UmamiFeeder) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if !h.disabled {
if h.shouldBeTracked(req) {
go h.trackRequest(req)
} else {
h.trace("Tracking skipped %v", req.URL)
}
}
h.next.ServeHTTP(rw, req)
}