Skip to content

Commit

Permalink
feat: add option to disable plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
astappiev committed Oct 16, 2024
1 parent 3e5c982 commit 9d645b5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 34 deletions.
1 change: 1 addition & 0 deletions .traefik.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""
Expand Down
76 changes: 42 additions & 34 deletions umami.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -28,6 +29,7 @@ type Config struct {
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
Disabled: false,
UmamiHost: "",
UmamiToken: "",
UmamiUsername: "",
Expand All @@ -44,6 +46,7 @@ type UmamiFeeder struct {
next http.Handler
name string
debug bool
disabled bool
logHandler *log.Logger

UmamiHost string
Expand All @@ -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,
Expand All @@ -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")
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
}
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.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")
}

for _, website := range *websites {
if _, ok := h.Websites[website.Domain]; ok {
continue
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)
Expand Down

0 comments on commit 9d645b5

Please sign in to comment.