From 82d04ad7cd480a78c10ee800c3302fa0a6ace80b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Fri, 29 May 2026 15:50:09 +0200 Subject: [PATCH] Pull out index.html --- Dockerfile | 4 +- go.mod | 2 +- index.html.tmpl | 197 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 208 ++---------------------------------------------- 4 files changed, 208 insertions(+), 203 deletions(-) create mode 100644 index.html.tmpl diff --git a/Dockerfile b/Dockerfile index 0926c10..84ea3f1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ WORKDIR /build COPY go.mod ./ RUN go mod download -COPY main.go ./ +COPY index.html.tmpl main.go ./ RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o beam . @@ -16,7 +16,7 @@ RUN adduser -D -u 1000 beam USER beam WORKDIR /app -COPY --from=builder /build/beam . +COPY --from=builder /build/index.html.tmpl /build/beam . EXPOSE 8080 diff --git a/go.mod b/go.mod index bf28d04..1449b2e 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module beam -go 1.26 +go 1.24 diff --git a/index.html.tmpl b/index.html.tmpl new file mode 100644 index 0000000..39e3983 --- /dev/null +++ b/index.html.tmpl @@ -0,0 +1,197 @@ + + + + + Beam - File Upload Service + + + +

🚀 Beam - File Upload Service

+ +
+

Upload with curl

+ curl --upload-file myfile.txt {{ .Proto }}://{{ .Host }}/myfile.txt +

The server will respond with a download URL.

+
+ +
+

Download a file

+ curl {{ .Proto }}://{{ .Host }}/x123abcx/myfile.txt -o myfile.txt +

Or simply open the URL in your browser.

+
+ +
+

Web Upload

+
+

📁 Click to select files or drag and drop here

+ + +
+
+
+
+
+
+ + + + + diff --git a/main.go b/main.go index d07f11e..d195c46 100644 --- a/main.go +++ b/main.go @@ -11,13 +11,14 @@ import ( "path/filepath" "strings" "time" + "html/template" ) var uploadDir string const idLength = 8 const idCharset = "abcdefghijklmnopqrstuvwxyz0123456789" - +var indexHTML *template.Template func main() { uploadDir = os.Getenv("UPLOAD_DIR") if uploadDir == "" { @@ -32,7 +33,7 @@ func main() { if err := os.MkdirAll(uploadDir, 0755); err != nil { log.Fatalf("Failed to create upload directory: %v", err) } - + indexHTML = template.Must(template.ParseFiles("index.html.tmpl")) http.HandleFunc("/", handleRequest) log.Printf("Starting server on port %s, upload directory: %s", port, uploadDir) @@ -49,6 +50,9 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { logRequest(r, 200, time.Since(start)) return } + if r.Method == "GET" && r.URL.Path == "/healthz" { + return + } if r.Method == "POST" && r.URL.Path == "/upload" { handleMultipartUpload(w, r, start) @@ -327,204 +331,8 @@ func logRequest(r *http.Request, status int, duration time.Duration) { func serveFrontpage(w http.ResponseWriter, r *http.Request) { proto := getProtocol(r) host := getHost(r) - html := fmt.Sprintf(` - - - - - Beam - File Upload Service - - - -

🚀 Beam - File Upload Service

- -
-

Upload with curl (Recommended)

- curl --upload-file myfile.txt %s://%s/myfile.txt -

The server will respond with a download URL.

-
- -
-

Download a file

- curl %s://%s/xxxxxxxx/myfile.txt -o myfile.txt -

Or simply open the URL in your browser.

-
- -
-

Web Upload

-
-

📁 Click to select files or drag and drop here

- - -
-
-
-
-
-
- - - -`, proto, host, proto, host) + pageData := map[string]string {"Proto": proto, "Host":host} w.Header().Set("Content-Type", "text/html; charset=utf-8") - w.Write([]byte(html)) + indexHTML.Execute(w, pageData) }