mirror of
https://github.com/skidoodle/safebin.git
synced 2026-04-28 11:17:42 +02:00
refactor: embed web files
Signed-off-by: skidoodle <contact@albert.lol>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -36,7 +37,7 @@ const (
|
||||
|
||||
DBFileName = "safebin.db"
|
||||
DBBucketName = "files"
|
||||
TempDirName = "tmp"
|
||||
TempDirName = "tmp"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
@@ -50,6 +51,7 @@ type App struct {
|
||||
Tmpl *template.Template
|
||||
Logger *slog.Logger
|
||||
DB *bbolt.DB
|
||||
Assets fs.FS
|
||||
}
|
||||
|
||||
func LoadConfig() Config {
|
||||
@@ -93,6 +95,6 @@ func getEnvInt(key string, fallback int) int {
|
||||
return fallback
|
||||
}
|
||||
|
||||
func ParseTemplates() *template.Template {
|
||||
return template.Must(template.ParseGlob("./web/templates/*.html"))
|
||||
func ParseTemplates(fsys fs.FS) *template.Template {
|
||||
return template.Must(template.ParseFS(fsys, "*.html"))
|
||||
}
|
||||
|
||||
@@ -29,8 +29,8 @@ func TestCalculateRetention(t *testing.T) {
|
||||
{
|
||||
name: "Half size file (Somewhere in between)",
|
||||
fileSize: 50 * MegaByte,
|
||||
wantMin: 24 * time.Hour,
|
||||
wantMax: MaxRetention,
|
||||
wantMin: 24 * time.Hour,
|
||||
wantMax: MaxRetention,
|
||||
},
|
||||
{
|
||||
name: "Oversized file (Min retention)",
|
||||
|
||||
+19
-3
@@ -5,13 +5,29 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (app *App) Routes() *http.ServeMux {
|
||||
mux := http.NewServeMux()
|
||||
fileServer := http.FileServer(http.Dir("./web/static"))
|
||||
|
||||
mux.Handle("GET /static/", http.StripPrefix("/static/", fileServer))
|
||||
fileServer := http.FileServer(http.FS(app.Assets))
|
||||
|
||||
staticHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "" || strings.HasSuffix(r.URL.Path, "/") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasSuffix(r.URL.Path, ".html") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
fileServer.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
mux.Handle("GET /static/", http.StripPrefix("/static/", staticHandler))
|
||||
mux.HandleFunc("GET /{$}", app.HandleHome)
|
||||
mux.HandleFunc("POST /{$}", app.HandleUpload)
|
||||
mux.HandleFunc("POST /upload/chunk", app.HandleChunk)
|
||||
@@ -22,7 +38,7 @@ func (app *App) Routes() *http.ServeMux {
|
||||
}
|
||||
|
||||
func (app *App) HandleHome(writer http.ResponseWriter, request *http.Request) {
|
||||
err := app.Tmpl.ExecuteTemplate(writer, "base", map[string]any{
|
||||
err := app.Tmpl.ExecuteTemplate(writer, "layout", map[string]any{
|
||||
"MaxMB": app.Conf.MaxMB,
|
||||
"Host": request.Host,
|
||||
})
|
||||
|
||||
@@ -3,7 +3,6 @@ package app
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log/slog"
|
||||
"mime/multipart"
|
||||
@@ -19,12 +18,14 @@ func setupTestApp(t *testing.T) (*App, string) {
|
||||
storageDir := t.TempDir()
|
||||
os.MkdirAll(filepath.Join(storageDir, TempDirName), 0700)
|
||||
|
||||
tmplDir := filepath.Join(storageDir, "templates")
|
||||
os.MkdirAll(tmplDir, 0700)
|
||||
os.WriteFile(filepath.Join(tmplDir, "base.html"), []byte(`{{define "base"}}{{template "content" .}}{{end}}`), 0600)
|
||||
os.WriteFile(filepath.Join(tmplDir, "index.html"), []byte(`{{define "content"}}OK{{end}}`), 0600)
|
||||
webDir := filepath.Join(storageDir, "web")
|
||||
os.MkdirAll(webDir, 0700)
|
||||
|
||||
tmpl := template.Must(template.New("base").Parse(`{{define "base"}}OK{{end}}`))
|
||||
os.WriteFile(filepath.Join(webDir, "layout.html"), []byte(`{{define "layout"}}{{template "content" .}}{{end}}`), 0600)
|
||||
os.WriteFile(filepath.Join(webDir, "home.html"), []byte(`{{define "content"}}OK{{end}}`), 0600)
|
||||
|
||||
testFS := os.DirFS(webDir)
|
||||
tmpl := ParseTemplates(testFS)
|
||||
|
||||
db, err := InitDB(storageDir)
|
||||
if err != nil {
|
||||
@@ -39,6 +40,7 @@ func setupTestApp(t *testing.T) (*App, string) {
|
||||
},
|
||||
Logger: discardLogger(),
|
||||
Tmpl: tmpl,
|
||||
Assets: testFS,
|
||||
DB: db,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user