Skip to content

Commit

Permalink
feat: embed UI assets into binary for easier distributing
Browse files Browse the repository at this point in the history
  • Loading branch information
jabuxas committed Oct 18, 2024
1 parent 0b3f167 commit ecb1475
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
12 changes: 11 additions & 1 deletion file_display.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"embed"
"html/template"
"log/slog"
"net/http"
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 27 additions & 7 deletions handlers.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"embed"
"fmt"
"html/template"
"io"
"io/fs"
"log/slog"
"net/http"
"os"
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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" {
Expand Down

0 comments on commit ecb1475

Please sign in to comment.