feat: add option to disable plugin
This commit is contained in:
parent
3e5c982284
commit
9d645b5c7e
2 changed files with 45 additions and 36 deletions
|
|
@ -7,6 +7,7 @@ import: github.com/astappiev/traefik-umami-feeder
|
||||||
summary: 'Traefik plugin that sends HTTP requests to Umami as pageview events.'
|
summary: 'Traefik plugin that sends HTTP requests to Umami as pageview events.'
|
||||||
|
|
||||||
testData:
|
testData:
|
||||||
|
disabled: false
|
||||||
umamiHost: ""
|
umamiHost: ""
|
||||||
umamiToken: ""
|
umamiToken: ""
|
||||||
umamiUsername: ""
|
umamiUsername: ""
|
||||||
|
|
|
||||||
80
umami.go
80
umami.go
|
|
@ -11,6 +11,7 @@ import (
|
||||||
|
|
||||||
// Config the plugin configuration.
|
// Config the plugin configuration.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
Disabled bool `json:"disabled"`
|
||||||
UmamiHost string `json:"umamiHost"`
|
UmamiHost string `json:"umamiHost"`
|
||||||
// it is optional, but either UmamiToken or Websites should be set
|
// it is optional, but either UmamiToken or Websites should be set
|
||||||
UmamiToken string `json:"umamiToken"`
|
UmamiToken string `json:"umamiToken"`
|
||||||
|
|
@ -28,6 +29,7 @@ type Config struct {
|
||||||
// CreateConfig creates the default plugin configuration.
|
// CreateConfig creates the default plugin configuration.
|
||||||
func CreateConfig() *Config {
|
func CreateConfig() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
|
Disabled: false,
|
||||||
UmamiHost: "",
|
UmamiHost: "",
|
||||||
UmamiToken: "",
|
UmamiToken: "",
|
||||||
UmamiUsername: "",
|
UmamiUsername: "",
|
||||||
|
|
@ -44,6 +46,7 @@ type UmamiFeeder struct {
|
||||||
next http.Handler
|
next http.Handler
|
||||||
name string
|
name string
|
||||||
debug bool
|
debug bool
|
||||||
|
disabled bool
|
||||||
logHandler *log.Logger
|
logHandler *log.Logger
|
||||||
|
|
||||||
UmamiHost string
|
UmamiHost string
|
||||||
|
|
@ -60,6 +63,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
|
||||||
next: next,
|
next: next,
|
||||||
name: name,
|
name: name,
|
||||||
debug: config.Debug,
|
debug: config.Debug,
|
||||||
|
disabled: config.Disabled,
|
||||||
logHandler: log.New(os.Stdout, "", 0),
|
logHandler: log.New(os.Stdout, "", 0),
|
||||||
|
|
||||||
UmamiHost: config.UmamiHost,
|
UmamiHost: config.UmamiHost,
|
||||||
|
|
@ -69,53 +73,57 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
|
||||||
CreateNewWebsites: config.CreateNewWebsites,
|
CreateNewWebsites: config.CreateNewWebsites,
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.UmamiUsername != "" && config.UmamiPassword != "" {
|
if !h.disabled {
|
||||||
token, err := getToken(h.UmamiHost, config.UmamiUsername, config.UmamiPassword)
|
if config.UmamiUsername != "" && config.UmamiPassword != "" {
|
||||||
if err != nil {
|
token, err := getToken(h.UmamiHost, config.UmamiUsername, config.UmamiPassword)
|
||||||
return nil, fmt.Errorf("failed to get token: %w", err)
|
if err != nil {
|
||||||
}
|
return nil, fmt.Errorf("failed to get token: %w", err)
|
||||||
if token == "" {
|
}
|
||||||
return nil, fmt.Errorf("retrieved token is empty")
|
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, website := range *websites {
|
if h.UmamiHost == "" {
|
||||||
if _, ok := h.Websites[website.Domain]; ok {
|
return nil, fmt.Errorf("`umamiHost` is not set")
|
||||||
continue
|
}
|
||||||
|
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
|
for _, website := range *websites {
|
||||||
h.trace("fetched websiteId for: %s", website.Domain)
|
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
|
return h, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *UmamiFeeder) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
func (h *UmamiFeeder) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
if h.shouldBeTracked(req) {
|
if !h.disabled {
|
||||||
go h.trackRequest(req)
|
if h.shouldBeTracked(req) {
|
||||||
} else {
|
go h.trackRequest(req)
|
||||||
h.trace("Tracking skipped %v", req.URL)
|
} else {
|
||||||
|
h.trace("Tracking skipped %v", req.URL)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
h.next.ServeHTTP(rw, req)
|
h.next.ServeHTTP(rw, req)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue