refactor: embed web files

Signed-off-by: skidoodle <contact@albert.lol>
This commit is contained in:
2026-01-18 20:53:56 +01:00
parent 954aec6d8e
commit 73ee7a9a14
14 changed files with 47 additions and 26 deletions
-5
View File
@@ -26,7 +26,6 @@ archives:
{{- else }}{{ .Arch }}{{ end }}
formats: ["tar.gz"]
files:
- web/**/*
- README.md
dockers:
@@ -37,8 +36,6 @@ dockers:
goos: linux
goarch: amd64
dockerfile: Dockerfile.release
extra_files:
- web
build_flag_templates:
- "--platform=linux/amd64"
- "--label=org.opencontainers.image.title={{ .ProjectName }}"
@@ -51,8 +48,6 @@ dockers:
goos: linux
goarch: arm64
dockerfile: Dockerfile.release
extra_files:
- web
build_flag_templates:
- "--platform=linux/arm64"
- "--label=org.opencontainers.image.title={{ .ProjectName }}"
-1
View File
@@ -31,7 +31,6 @@ RUN useradd -m -u 10001 -s /bin/bash appuser
WORKDIR /app
COPY --from=builder /app/safebin .
COPY --from=builder /app/web ./web
RUN mkdir -p /app/storage && chown 10001:10001 /app/storage
VOLUME ["/app/storage"]
-1
View File
@@ -9,7 +9,6 @@ RUN useradd -m -u 10001 -s /bin/bash appuser
WORKDIR /app
COPY safebin .
COPY web ./web
RUN mkdir -p /app/storage && chown 10001:10001 /app/storage
VOLUME ["/app/storage"]
+4 -2
View File
@@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"html/template"
"io/fs"
"log/slog"
"os"
"strconv"
@@ -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"))
}
+19 -3
View File
@@ -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,
})
+8 -6
View File
@@ -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,
}
+3 -1
View File
@@ -12,6 +12,7 @@ import (
"syscall"
"github.com/skidoodle/safebin/internal/app"
"github.com/skidoodle/safebin/web"
)
func main() {
@@ -46,7 +47,8 @@ func main() {
application := &app.App{
Conf: cfg,
Logger: logger,
Tmpl: app.ParseTemplates(),
Tmpl: app.ParseTemplates(web.Assets),
Assets: web.Assets,
DB: db,
}
View File
+6
View File
@@ -0,0 +1,6 @@
package web
import "embed"
//go:embed *.html *.css *.js *.ico
var Assets embed.FS
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

+4 -4
View File
@@ -1,12 +1,12 @@
{{define "base"}}
{{define "layout"}}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" />
<link rel="icon" type="image/vnd.microsoft.icon" href="/static/favicon.ico" />
<title>safebin</title>
<link rel="stylesheet" href="/static/css/style.css" />
<link rel="stylesheet" href="/static/style.css" />
</head>
<body>
<div class="container">
@@ -31,7 +31,7 @@
</section>
</div>
<input type="file" id="file-input" class="hidden" />
<script src="/static/js/app.js"></script>
<script src="/static/app.js"></script>
</body>
</html>
{{end}}