Skip to content

Commit 53963d6

Browse files
committed
feat(server): add optional analytics tracker tag
1 parent 5eeab83 commit 53963d6

File tree

5 files changed

+18
-3
lines changed

5 files changed

+18
-3
lines changed

internal/config/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ type Cfg struct {
2929
ConnectionURI string `env:"CONNECTION_URI" json:"-"`
3030

3131
// Web
32-
Headless bool `env:"HEADLESS" envDefault:"false" json:"headless"` // Enable website
32+
Headless bool `env:"HEADLESS" envDefault:"false" json:"headless"` // Enable website
33+
Analytics string `env:"ANALYTICS" envDefault:"" json:"analytics"` // <script> tag for analytics (leave blank to disable)
3334

3435
// Document
3536
IDLength int `env:"ID_LENGTH" envDefault:"8" json:"id_length"`

internal/server/fetch.go

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strings"
2727

2828
"github.com/go-chi/chi/v5"
29+
"github.com/lukewhrit/spacebin/internal/config"
2930
"github.com/lukewhrit/spacebin/internal/database"
3031
"github.com/lukewhrit/spacebin/internal/util"
3132
"golang.org/x/exp/slices"
@@ -81,6 +82,7 @@ func (s *Server) StaticDocument(w http.ResponseWriter, r *http.Request) {
8182
"Lines": util.CountLines(document.Content),
8283
"Content": document.Content,
8384
"Extension": extension,
85+
"Analytics": template.HTML(config.Config.Analytics),
8486
}
8587

8688
if err := t.Execute(w, data); err != nil {

internal/server/server.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io/fs"
2222
"net/http"
2323
"strings"
24+
"text/template"
2425

2526
"github.com/go-chi/chi/v5"
2627
"github.com/go-chi/chi/v5/middleware"
@@ -143,14 +144,21 @@ func (s *Server) MountStatic() {
143144
})
144145

145146
s.Router.Get("/", func(w http.ResponseWriter, r *http.Request) {
146-
file, err := resources.ReadFile("web/index.html")
147+
t, err := template.ParseFS(resources, "web/index.html")
147148

148149
if err != nil {
149150
util.WriteError(w, http.StatusInternalServerError, err)
150151
return
151152
}
152153

153-
w.Write(file)
154+
err = t.Execute(w, map[string]interface{}{
155+
"Analytics": config.Config.Analytics,
156+
})
157+
158+
if err != nil {
159+
util.WriteError(w, http.StatusInternalServerError, err)
160+
return
161+
}
154162
})
155163
}
156164

internal/server/web/document.html

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
<link rel="stylesheet" type="text/css" href="/static/normalize.css">
2222
<link rel="stylesheet" type="text/css" href="/static/monokai.min.css">
2323
<link rel="stylesheet" type="text/css" href="/static/global.css">
24+
25+
{{.Analytics}}
2426
</head>
2527

2628
<body>

internal/server/web/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
content="Spacebin is a highly-reliable pastebin server, built with Go, that's capable of serving notes, code, or any other documents." />
1818
<meta property="og:color" content="#e34b4a" />
1919

20+
{{.Analytics}}
21+
2022
<link rel="icon" type="image/x-icon" href="/static/favicon.ico">
2123
<link rel="stylesheet" type="text/css" href="/static/normalize.css">
2224
<link rel="stylesheet" type="text/css" href="/static/global.css">

0 commit comments

Comments
 (0)