summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2023-08-14 21:24:54 -0700
committerMike Crute <mike@crute.us>2023-08-14 21:24:54 -0700
commit0b12194c52d0af5eb5339cb96905d86d175d6a12 (patch)
tree573ddd5c61899ae12d829465d4cf262843a24a03
parent746e5d91e7c23fe702040cfb5b10b60d7537dbfa (diff)
downloadwebsocket_proxy-0b12194c52d0af5eb5339cb96905d86d175d6a12.tar.bz2
websocket_proxy-0b12194c52d0af5eb5339cb96905d86d175d6a12.tar.xz
websocket_proxy-0b12194c52d0af5eb5339cb96905d86d175d6a12.zip
Support indexing clients
-rw-r--r--Makefile9
-rw-r--r--app/controllers/client_list.go39
-rw-r--r--cmd/web/server.go6
-rw-r--r--templates/client_index.tpl9
4 files changed, 59 insertions, 4 deletions
diff --git a/Makefile b/Makefile
index 161c8ab..2c12471 100644
--- a/Makefile
+++ b/Makefile
@@ -52,13 +52,16 @@ publish-prod:
52 docker tag $(IMAGE):stage $(IMAGE):latest 52 docker tag $(IMAGE):stage $(IMAGE):latest
53 docker push $(IMAGE):latest 53 docker push $(IMAGE):latest
54 54
55.PHONY: clean-server
56clean-server:
57 rm $(SERVER_BINARY) || true
58
55.PHONY: clean 59.PHONY: clean
56clean: 60clean: clean-server
57 rm -rf docker clients || true 61 rm -rf docker clients || true
58 rm $(SERVER_BINARY) || true
59 62
60.PHONY: run-web 63.PHONY: run-web
61run-web: clean $(SERVER_BINARY) 64run-web: clean-server $(SERVER_BINARY)
62 test -n "$(VAULT_ROLE_ID)" # Caller must export VAULT_ROLE_ID 65 test -n "$(VAULT_ROLE_ID)" # Caller must export VAULT_ROLE_ID
63 test -n "$(VAULT_SECRET_ID)" # Caller must also export VAULT_SECRET_ID 66 test -n "$(VAULT_SECRET_ID)" # Caller must also export VAULT_SECRET_ID
64 VAULT_ADDR="https://vault.sea4.crute.me:8200" \ 67 VAULT_ADDR="https://vault.sea4.crute.me:8200" \
diff --git a/app/controllers/client_list.go b/app/controllers/client_list.go
new file mode 100644
index 0000000..f6531da
--- /dev/null
+++ b/app/controllers/client_list.go
@@ -0,0 +1,39 @@
1package controllers
2
3import (
4 "io/fs"
5 "net/http"
6 "time"
7
8 "github.com/labstack/echo/v4"
9)
10
11type clientEntry struct {
12 Filename string
13 LastModified time.Time
14 ByteSize int64
15}
16
17func ListClients(clients fs.FS) echo.HandlerFunc {
18 return func(c echo.Context) error {
19 out := struct{ Models []clientEntry }{[]clientEntry{}}
20
21 fs.WalkDir(clients, "clients", func(path string, d fs.DirEntry, err error) error {
22 if path == "clients/keep" || d.IsDir() {
23 return nil
24 }
25 fi, err := d.Info()
26 if err != nil {
27 return err
28 }
29 out.Models = append(out.Models, clientEntry{
30 Filename: d.Name(),
31 LastModified: fi.ModTime(),
32 ByteSize: fi.Size(),
33 })
34 return nil
35 })
36
37 return c.Render(http.StatusOK, "client_index.tpl", out)
38 }
39}
diff --git a/cmd/web/server.go b/cmd/web/server.go
index 19c1bb7..b487abc 100644
--- a/cmd/web/server.go
+++ b/cmd/web/server.go
@@ -220,7 +220,11 @@ func webMain(cfg app.Config, embeddedTemplates, embeddedClients fs.FS, appVersio
220 220
221 csm := glmiddleware.CSRFProtect(ss) 221 csm := glmiddleware.CSRFProtect(ss)
222 222
223 glecho.StaticFS(s.GET, embeddedClients, "/clients", "./clients/") 223 lcc := controllers.ListClients(embeddedClients)
224 s.GET("/clients", lcc)
225 s.GET("/clients/", lcc)
226
227 glecho.StaticFS(s.GET, embeddedClients, "/clients/*", "./clients/")
224 228
225 s.NeverCacheStaticRoute("/js", "js") 229 s.NeverCacheStaticRoute("/js", "js")
226 230
diff --git a/templates/client_index.tpl b/templates/client_index.tpl
new file mode 100644
index 0000000..0223de9
--- /dev/null
+++ b/templates/client_index.tpl
@@ -0,0 +1,9 @@
1<html>
2<head><title>Index of /clients/</title></head>
3<body>
4<h1>Index of /clients/</h1><hr><pre>
5{{- range .Models }}
6<a href="{{ .Filename }}">{{ .Filename }}</a> {{ .LastModified }} {{ .ByteSize }}
7{{- end -}}
8</pre><hr></body>
9</html>