diff options
| author | Julian Hurst <ark@mansus.space> | 2025-04-10 02:03:33 +0200 |
|---|---|---|
| committer | Julian Hurst <ark@mansus.space> | 2025-04-10 02:03:33 +0200 |
| commit | caab30c96ba0ac436e0a6b8211ea32c2e396a8cf (patch) | |
| tree | 5e0883b2f39620a3bbc29e3e3b7a460259eb46bd | |
| parent | 3548e8f91c86170a29c2e6f20ede914c31358539 (diff) | |
| download | box-caab30c96ba0ac436e0a6b8211ea32c2e396a8cf.tar.gz | |
Add file listing support
| -rw-r--r-- | main.go | 14 | ||||
| -rw-r--r-- | templates/index.html | 22 |
2 files changed, 35 insertions, 1 deletions
@@ -23,6 +23,9 @@ import ( //go:embed templates var tmplFS embed.FS +//go:embed static +var staticFS embed.FS + //go:embed favicon.ico var favicon []byte @@ -61,6 +64,17 @@ func (handler BoxHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } if reqPath == "/" { serve(w, handler.token, "templates/index.html") + } else if strings.HasPrefix(reqPath, "/static") { + http.ServeFileFS(w, r, staticFS, reqPath) + } else if strings.HasPrefix(reqPath, "/getindex") { + token := r.Header.Get("X-Token") + if bcrypt.CompareHashAndPassword(handler.token, []byte(token)) != nil { + log.Println("unauthorized") + w.WriteHeader(http.StatusUnauthorized) + fmt.Fprint(w, "<span>Unauthorized: invalid token</span>") + return + } + http.ServeFile(w, r, filepath.Join(handler.filesPath)) } else { resourceId := path.Base(reqPath) resourcePath := filepath.Join(handler.filesPath, resourceId) diff --git a/templates/index.html b/templates/index.html index c06b6ec..7c77e8e 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,6 +3,7 @@ <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Box</title> + <script src="{{.ProxyPrefix}}/static/htmx.min.js"></script> <script> function upload() { let resourceId = ""; @@ -29,7 +30,7 @@ document.getElementById("progress").innerHTML = "Error: unknown"; } } else { - document.getElementById("progress").innerHTML = "Done: <a href=\"{{.ProxyPrefix}}/" + resourceId + "\">" + resourceId + "</a>"; + document.getElementById("progress").innerHTML = "Done: <a href=\"{{.ProxyPrefix}}/" + resourceId + "\">" + resourceId + "</a>"; } } if (xhr.readyState === xhr.HEADERS_RECEIVED) { @@ -48,9 +49,28 @@ } xhr.send(f); } + + function gettoken() { + token = document.getElementById("token"); + if (token == null) { + token = ""; + } else { + token = token.value; + } + return token; + } </script> + <meta name="htmx-config" content='{ + "responseHandling":[ + {"code":"200", "swap": true}, + {"code":"401", "swap": true} + ] + }' + /> </head> <body> + <button hx-get="/getindex" hx-headers='js:{"X-Token": gettoken()}' hx-target="#index">list</button> + <div id="index"></div> <pre> _ | |__ _____ __ |
