feat: embed UI assets into binary for easier distributing
This commit is contained in:
parent
0b3f167fce
commit
ecb147533a
@ -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)
|
||||
|
34
handlers.go
34
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" {
|
||||
|
Loading…
Reference in New Issue
Block a user