From 9d645b5c7e7752ecb8de6da7e30262d090158525 Mon Sep 17 00:00:00 2001 From: Oleh Astappiev Date: Wed, 16 Oct 2024 16:32:09 +0200 Subject: [PATCH] feat: add option to disable plugin --- .traefik.yml | 1 + umami.go | 80 +++++++++++++++++++++++++++++----------------------- 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/.traefik.yml b/.traefik.yml index 0b724f1..80f5e05 100644 --- a/.traefik.yml +++ b/.traefik.yml @@ -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: "" diff --git a/umami.go b/umami.go index 695b876..1cdbe2e 100644 --- a/umami.go +++ b/umami.go @@ -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,53 +73,57 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h CreateNewWebsites: config.CreateNewWebsites, } - if config.UmamiUsername != "" && config.UmamiPassword != "" { - token, err := getToken(h.UmamiHost, config.UmamiUsername, config.UmamiPassword) - if err != nil { - return nil, fmt.Errorf("failed to get token: %w", err) - } - if token == "" { - return nil, fmt.Errorf("retrieved token is empty") - } - h.trace("token received %s", token) - h.UmamiToken = token - } - - if h.UmamiHost == "" { - return nil, fmt.Errorf("`umamiHost` is not set") - } - if h.UmamiToken == "" && len(h.Websites) == 0 { - return nil, fmt.Errorf("either `umamiToken` or `websites` should be set") - } - if h.UmamiToken == "" && h.CreateNewWebsites { - return nil, fmt.Errorf("`umamiToken` is required to create new websites") - } - - if h.UmamiToken != "" { - websites, err := fetchWebsites(h.UmamiHost, h.UmamiToken, h.UmamiTeamId) - if err != nil { - return nil, fmt.Errorf("failed to fetch websites: %w", err) + if !h.disabled { + if config.UmamiUsername != "" && config.UmamiPassword != "" { + token, err := getToken(h.UmamiHost, config.UmamiUsername, config.UmamiPassword) + if err != nil { + return nil, fmt.Errorf("failed to get token: %w", err) + } + if token == "" { + return nil, fmt.Errorf("retrieved token is empty") + } + h.trace("token received %s", token) + h.UmamiToken = token } - for _, website := range *websites { - if _, ok := h.Websites[website.Domain]; ok { - continue + if h.UmamiHost == "" { + return nil, fmt.Errorf("`umamiHost` is not set") + } + if h.UmamiToken == "" && len(h.Websites) == 0 { + return nil, fmt.Errorf("either `umamiToken` or `websites` should be set") + } + if h.UmamiToken == "" && h.CreateNewWebsites { + return nil, fmt.Errorf("`umamiToken` is required to create new websites") + } + + if h.UmamiToken != "" { + websites, err := fetchWebsites(h.UmamiHost, h.UmamiToken, h.UmamiTeamId) + if err != nil { + return nil, fmt.Errorf("failed to fetch websites: %w", err) } - h.Websites[website.Domain] = website.ID - h.trace("fetched websiteId for: %s", website.Domain) + for _, website := range *websites { + if _, ok := h.Websites[website.Domain]; ok { + continue + } + + h.Websites[website.Domain] = website.ID + h.trace("fetched websiteId for: %s", website.Domain) + } + h.log("websites fetched") } - h.log("websites fetched") } return h, nil } func (h *UmamiFeeder) ServeHTTP(rw http.ResponseWriter, req *http.Request) { - if h.shouldBeTracked(req) { - go h.trackRequest(req) - } else { - h.trace("Tracking skipped %v", req.URL) + if !h.disabled { + if h.shouldBeTracked(req) { + go h.trackRequest(req) + } else { + h.trace("Tracking skipped %v", req.URL) + } } h.next.ServeHTTP(rw, req)