feat: embed UI assets into binary for easier distributing

This commit is contained in:
jabuxas 2024-10-18 11:18:33 -03:00
parent 0b3f167fce
commit ecb147533a
2 changed files with 38 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"embed"
"html/template" "html/template"
"log/slog" "log/slog"
"net/http" "net/http"
@ -32,8 +33,17 @@ var extensions = map[string]string{
".rst": "text", ".el": "text", ".fish": "text", ".rst": "text", ".el": "text", ".fish": "text",
} }
//go:embed templates/files.html
var filesTemplate embed.FS
func DisplayFile(app *Application, file string, w http.ResponseWriter) { 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) fileStat, _ := os.Stat("." + file)
fileContent, _ := os.ReadFile("." + file) fileContent, _ := os.ReadFile("." + file)

View File

@ -1,9 +1,11 @@
package main package main
import ( import (
"embed"
"fmt" "fmt"
"html/template" "html/template"
"io" "io"
"io/fs"
"log/slog" "log/slog"
"net/http" "net/http"
"os" "os"
@ -27,6 +29,12 @@ type Application struct {
lastUploadedFile string 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) { func (app *Application) fileListingHandler(w http.ResponseWriter, r *http.Request) {
dir := app.filesDir + r.URL.Path 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{ templateData := TemplateData{
Files: fileInfos, Files: fileInfos,
URL: app.url, 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) { 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 { if r.Method == http.MethodPost {
app.uploadHandler(w, r) app.uploadHandler(w, r)
return return
@ -99,7 +119,12 @@ func (app *Application) indexHandler(w http.ResponseWriter, r *http.Request) {
return 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) { 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) { 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" { if contentType := r.Header.Get("Content-Type"); contentType == "application/x-www-form-urlencoded" {
app.formHandler(w, r) app.formHandler(w, r)
} else if strings.Split(contentType, ";")[0] == "multipart/form-data" { } else if strings.Split(contentType, ";")[0] == "multipart/form-data" {