From ecb147533a1e146953ce93e210503099458e5ab6 Mon Sep 17 00:00:00 2001 From: jabuxas Date: Fri, 18 Oct 2024 11:18:33 -0300 Subject: [PATCH] feat: embed UI assets into binary for easier distributing --- file_display.go | 12 +++++++++++- handlers.go | 34 +++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/file_display.go b/file_display.go index b449fef..535f09e 100644 --- a/file_display.go +++ b/file_display.go @@ -1,6 +1,7 @@ package main import ( + "embed" "html/template" "log/slog" "net/http" @@ -32,8 +33,17 @@ var extensions = map[string]string{ ".rst": "text", ".el": "text", ".fish": "text", } +//go:embed templates/files.html +var filesTemplate embed.FS + func DisplayFile(app *Application, file string, w http.ResponseWriter) { - tmpl := template.Must(template.ParseFiles("templates/files.html")) + var tmpl *template.Template + + if _, err := os.Stat("./templates/dirlist.html"); err == nil { + tmpl = template.Must(template.ParseFiles("templates/files.html")) + } else { + tmpl = template.Must(template.ParseFS(filesTemplate, "templates/files.html")) + } fileStat, _ := os.Stat("." + file) fileContent, _ := os.ReadFile("." + file) diff --git a/handlers.go b/handlers.go index ef6e53e..5d24997 100644 --- a/handlers.go +++ b/handlers.go @@ -1,9 +1,11 @@ package main import ( + "embed" "fmt" "html/template" "io" + "io/fs" "log/slog" "net/http" "os" @@ -27,6 +29,12 @@ type Application struct { lastUploadedFile string } +//go:embed static/** +var static embed.FS + +//go:embed templates/dirlist.html +var treeTemplate embed.FS + func (app *Application) fileListingHandler(w http.ResponseWriter, r *http.Request) { dir := app.filesDir + r.URL.Path @@ -56,7 +64,13 @@ func (app *Application) fileListingHandler(w http.ResponseWriter, r *http.Reques }) } - tmpl := template.Must(template.ParseFiles("templates/dirlist.html")) + var tmpl *template.Template + + if _, err := os.Stat("./templates/dirlist.html"); err == nil { + tmpl = template.Must(template.ParseFiles("templates/dirlist.html")) + } else { + tmpl = template.Must(template.ParseFS(treeTemplate, "templates/dirlist.html")) + } templateData := TemplateData{ Files: fileInfos, URL: app.url, @@ -81,6 +95,12 @@ func (app *Application) fileHandler(w http.ResponseWriter, r *http.Request) { } func (app *Application) indexHandler(w http.ResponseWriter, r *http.Request) { + if _, err := os.Stat(app.filesDir); err != nil { + if err := os.Mkdir(app.filesDir, 0750); err != nil { + http.Error(w, "Error creating storage directory", http.StatusInternalServerError) + } + } + if r.Method == http.MethodPost { app.uploadHandler(w, r) return @@ -99,7 +119,12 @@ func (app *Application) indexHandler(w http.ResponseWriter, r *http.Request) { return } - http.StripPrefix("/", http.FileServer(http.Dir("./static"))).ServeHTTP(w, r) + if _, err := os.Stat("./static"); err == nil { + http.StripPrefix("/", http.FileServer(http.Dir("./static"))).ServeHTTP(w, r) + } else { + fs, _ := fs.Sub(static, "static") + http.StripPrefix("/", http.FileServer(http.FS(fs))).ServeHTTP(w, r) + } } func (app *Application) lastUploadedHandler(w http.ResponseWriter, r *http.Request) { @@ -111,11 +136,6 @@ func (app *Application) lastUploadedHandler(w http.ResponseWriter, r *http.Reque } func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { - if _, err := os.Stat(app.filesDir); err != nil { - if err := os.Mkdir(app.filesDir, 0750); err != nil { - http.Error(w, "Error creating storage directory", http.StatusInternalServerError) - } - } if contentType := r.Header.Get("Content-Type"); contentType == "application/x-www-form-urlencoded" { app.formHandler(w, r) } else if strings.Split(contentType, ";")[0] == "multipart/form-data" {