Skip to content

Commit

Permalink
feat(server): add optional analytics tracker tag
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewhrit committed Jul 13, 2024
1 parent 5eeab83 commit 53963d6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
3 changes: 2 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type Cfg struct {
ConnectionURI string `env:"CONNECTION_URI" json:"-"`

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

// Document
IDLength int `env:"ID_LENGTH" envDefault:"8" json:"id_length"`
Expand Down
2 changes: 2 additions & 0 deletions internal/server/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"

"github.com/go-chi/chi/v5"
"github.com/lukewhrit/spacebin/internal/config"
"github.com/lukewhrit/spacebin/internal/database"
"github.com/lukewhrit/spacebin/internal/util"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -81,6 +82,7 @@ func (s *Server) StaticDocument(w http.ResponseWriter, r *http.Request) {
"Lines": util.CountLines(document.Content),
"Content": document.Content,
"Extension": extension,
"Analytics": template.HTML(config.Config.Analytics),
}

if err := t.Execute(w, data); err != nil {
Expand Down
12 changes: 10 additions & 2 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/fs"
"net/http"
"strings"
"text/template"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
Expand Down Expand Up @@ -143,14 +144,21 @@ func (s *Server) MountStatic() {
})

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

if err != nil {
util.WriteError(w, http.StatusInternalServerError, err)
return
}

w.Write(file)
err = t.Execute(w, map[string]interface{}{
"Analytics": config.Config.Analytics,
})

if err != nil {
util.WriteError(w, http.StatusInternalServerError, err)
return
}
})
}

Expand Down
2 changes: 2 additions & 0 deletions internal/server/web/document.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<link rel="stylesheet" type="text/css" href="/static/normalize.css">
<link rel="stylesheet" type="text/css" href="/static/monokai.min.css">
<link rel="stylesheet" type="text/css" href="/static/global.css">

{{.Analytics}}
</head>

<body>
Expand Down
2 changes: 2 additions & 0 deletions internal/server/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
content="Spacebin is a highly-reliable pastebin server, built with Go, that's capable of serving notes, code, or any other documents." />
<meta property="og:color" content="#e34b4a" />

{{.Analytics}}

<link rel="icon" type="image/x-icon" href="/static/favicon.ico">
<link rel="stylesheet" type="text/css" href="/static/normalize.css">
<link rel="stylesheet" type="text/css" href="/static/global.css">
Expand Down

0 comments on commit 53963d6

Please sign in to comment.