Skip to content

Commit 6697774

Browse files
committed
plausbile: and check if plausible should be used or not
1 parent fb58ca9 commit 6697774

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

cmd/browser/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func main() {
6666
googleSecret = fs.String("google.secret", "", "Google OAuth2 secret.")
6767
googleRedirect = fs.String("google.redirect", "", "Google OAuth2 redirect URL.")
6868
healthzToken = fs.String("health.token", "", "Token for the health check endpoint")
69+
usePlausible = fs.Bool("plausible", false, "Use plausible.io for tracking statistics")
6970
devMode = fs.Bool("dev", false, "Run in development mode.")
7071
_ = fs.String("config", "", "Config file (optional)")
7172
)
@@ -119,6 +120,7 @@ func main() {
119120
http.WithDatabase(db),
120121
http.WithStationService(stationService),
121122
http.WithDevMode(*devMode),
123+
http.WithPlausible(*usePlausible),
122124
)
123125

124126
// Initialize authentication handler.

internal/http/handler.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ var (
2121

2222
// Handler serves various HTTP endpoints.
2323
type Handler struct {
24-
mux *http.ServeMux
25-
devMode bool
24+
mux *http.ServeMux
25+
devMode bool
26+
plausible bool
2627

2728
db browser.Database
2829
stationService browser.StationService
@@ -96,6 +97,12 @@ func WithDevMode(mode bool) Option {
9697
}
9798
}
9899

100+
func WithPlausible(use bool) Option {
101+
return func(h *Handler) {
102+
h.plausible = use
103+
}
104+
}
105+
99106
func (h *Handler) handleVersion(w http.ResponseWriter, r *http.Request) {
100107
w.Header().Set("Content-Type", "text/plain")
101108
w.Write([]byte(browser.Version))

internal/http/templates/base.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<script src="/assets/third_party/jquery/jquery-ui.min.js"></script>
2727
<script src="/assets/third_party/bootstrap/js/bootstrap.min.js"></script>
2828
<script src="/assets/third_party/cookies-eu-banner/js/cookies-eu-banner.js"></script>
29-
<script defer data-domain="browser.lter.eurac.edu" src="https://plausible.io/js/plausible.js"></script>
29+
{{ if .Plausible }}<script defer data-domain="browser.lter.eurac.edu" src="https://plausible.io/js/plausible.js"></script>{{ end }}
3030
<link rel="icon" type="image/png" sizes="32x32" href="/assets/favicon-32x32.png">
3131
<link rel="icon" type="image/png" sizes="16x16" href="/assets/favicon-16x16.png">
3232
<link rel="apple-touch-icon" href="/assets/favicon-196x196.png">

internal/http/ui.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (h *Handler) handleIndex() http.HandlerFunc {
6565
Token string
6666
StartDate string
6767
EndDate string
68+
Plausible bool
6869
}{
6970
data,
7071
browser.GroupsByRole(user.Role),
@@ -75,6 +76,7 @@ func (h *Handler) handleIndex() http.HandlerFunc {
7576
middleware.XSRFTokenPlaceholder,
7677
time.Now().AddDate(0, -6, 0).Format("2006-01-02"),
7778
time.Now().Format("2006-01-02"),
79+
h.plausible,
7880
})
7981
if err != nil {
8082
Error(w, err, http.StatusInternalServerError)
@@ -112,18 +114,20 @@ func (h *Handler) handleHello() http.HandlerFunc {
112114
}
113115

114116
err = tmpl.Execute(w, struct {
115-
Data browser.Stations
116-
User *browser.User
117-
Language string
118-
Path string
119-
Token string
120-
Content template.HTML
117+
Data browser.Stations
118+
User *browser.User
119+
Language string
120+
Path string
121+
Token string
122+
Plausible bool
123+
Content template.HTML
121124
}{
122125
data,
123126
user,
124127
lang,
125128
name,
126129
middleware.XSRFTokenPlaceholder,
130+
h.plausible,
127131
template.HTML(license),
128132
})
129133
if err != nil {
@@ -175,16 +179,18 @@ func (h *Handler) handleStaticPage() http.HandlerFunc {
175179
}
176180

177181
err = tmpl.Execute(w, struct {
178-
Data browser.Stations
179-
User *browser.User
180-
Language string
181-
Path string
182-
Content template.HTML
182+
Data browser.Stations
183+
User *browser.User
184+
Language string
185+
Path string
186+
Plausible bool
187+
Content template.HTML
183188
}{
184189
data,
185190
user,
186191
lang,
187192
name,
193+
h.plausible,
188194
template.HTML(p),
189195
})
190196
if err != nil {

0 commit comments

Comments
 (0)