summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go15
1 files changed, 9 insertions, 6 deletions
diff --git a/main.go b/main.go
index 82f4e49..e4020c3 100644
--- a/main.go
+++ b/main.go
@@ -14,6 +14,7 @@ import (
"bufio"
"errors"
"io/fs"
+ "strings"
"github.com/google/uuid"
"golang.org/x/crypto/bcrypt"
@@ -25,6 +26,7 @@ var tmplFS embed.FS
//go:embed favicon.ico
var favicon []byte
+const PROXYPREFIXKEY string = "BOX_PROXY_PREFIX"
type BoxHandler struct {
filesPath string
@@ -43,23 +45,24 @@ func serve(w http.ResponseWriter, token []byte, views ...string) {
ProxyPrefix template.URL
}{
Token: token != nil,
- ProxyPrefix: template.URL(os.Getenv("BOX_PROXY_PREFIX")),
+ ProxyPrefix: template.URL(os.Getenv(PROXYPREFIXKEY)),
}); err != nil {
log.Fatal(err)
}
}
func (handler BoxHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ reqPath := strings.TrimPrefix(r.URL.Path, os.Getenv(PROXYPREFIXKEY))
switch r.Method {
case http.MethodGet:
- if r.URL.Path == "/favicon.ico" {
+ if reqPath == "/favicon.ico" {
w.Write(favicon)
return
}
- if r.URL.Path == "/" {
+ if reqPath == "/" {
serve(w, handler.token, "templates/index.html")
} else {
- resourceId := path.Base(r.URL.Path)
+ resourceId := path.Base(reqPath)
resourcePath := filepath.Join(handler.filesPath, resourceId)
fi, err := os.Stat(resourcePath)
if errors.Is(err, fs.ErrNotExist) {
@@ -91,7 +94,7 @@ func (handler BoxHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
return
}
- resourceId := path.Base(r.URL.Path)
+ resourceId := path.Base(reqPath)
filename := filepath.Join(handler.filesPath, resourceId)
err := os.Remove(filename)
if err != nil {
@@ -103,7 +106,7 @@ func (handler BoxHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("Deleted %s", filename)
w.WriteHeader(http.StatusNoContent)
case http.MethodPost:
- if r.URL.Path != "/upload" {
+ if reqPath != "/upload" {
w.WriteHeader(http.StatusBadRequest)
return
}