Skip to content

Commit 1720426

Browse files
authored
Add token entry/storage functionality to test console (#7)
1 parent 0e48c95 commit 1720426

File tree

3 files changed

+269
-114
lines changed

3 files changed

+269
-114
lines changed

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
MODULE_NAME := $(shell go list -m)
1+
MODULE_NAME := scrape
2+
ifneq ($(shell command -v go >/dev/null 2>&1 && echo yes),)
3+
MODULE_NAME := $(shell go list -m)
4+
endif
25
DOCKER_IMAGE_NAME := ${shell basename ${MODULE_NAME}}-bookworm-slim
36
CWD := $(shell pwd)
47
BUILD_DIR := build

internal/server/routes.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ var home embed.FS
107107

108108
func (h scrapeServer) mustHomeTemplate() *template.Template {
109109
tmpl := template.New("home")
110-
var keyF = func() string { return "" }
111-
if len(h.SigningKey) > 0 {
112-
keyF = func() string {
110+
var authTokenF = func() string { return "" }
111+
var authEnabledF = func() bool { return len(h.SigningKey) > 0 }
112+
if authEnabledF() {
113+
authTokenF = func() string {
113114
c, err := auth.NewClaims(
114115
auth.WithSubject("home"),
115116
auth.ExpiresIn(60*time.Minute),
@@ -126,7 +127,11 @@ func (h scrapeServer) mustHomeTemplate() *template.Template {
126127
return s
127128
}
128129
}
129-
tmpl = tmpl.Funcs(template.FuncMap{"AuthToken": keyF})
130+
funcMap := template.FuncMap{
131+
"AuthToken": authTokenF,
132+
"AuthEnabled": authEnabledF,
133+
}
134+
tmpl = tmpl.Funcs(funcMap)
130135
homeSource, _ := home.ReadFile("templates/index.html")
131136
tmpl = template.Must(tmpl.Parse(string(homeSource)))
132137
return tmpl

0 commit comments

Comments
 (0)