summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Hurst <ark@mansus.space>2025-04-10 02:03:33 +0200
committerJulian Hurst <ark@mansus.space>2025-04-10 02:03:33 +0200
commitcaab30c96ba0ac436e0a6b8211ea32c2e396a8cf (patch)
tree5e0883b2f39620a3bbc29e3e3b7a460259eb46bd
parent3548e8f91c86170a29c2e6f20ede914c31358539 (diff)
downloadbox-caab30c96ba0ac436e0a6b8211ea32c2e396a8cf.tar.gz
Add file listing support
-rw-r--r--main.go14
-rw-r--r--templates/index.html22
2 files changed, 35 insertions, 1 deletions
diff --git a/main.go b/main.go
index df415b8..a08467d 100644
--- a/main.go
+++ b/main.go
@@ -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>
_
| |__ _____ __