feat: embed templates and static assets with go:embed for distroless containers
Replace all runtime template.ParseFiles() calls with template.ParseFS() using an embedded filesystem, and serve static assets from an embedded FS via http.FileServerFS(). This eliminates the need for COPY steps in the Dockerfile and ensures the binary works with readOnlyRootFilesystem: true. - Add internal/templates/embed.go exposing templates.FS - Add static/embed.go exposing static.FS - Update all handlers to use template.ParseFS(templates.FS, ...) - Update static file server to use http.FileServerFS(static.FS) - Remove COPY static/ and COPY internal/templates/ from Dockerfile - Remove TestMain working directory hack (no longer needed) Closes leeworks-agents/gitea-mobile#231 Closes leeworks-agents/gitea-mobile#220 Closes leeworks-agents/gitea-mobile#221 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -7,9 +7,6 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -18,26 +15,8 @@ import (
|
||||
"gitea.leeworks.dev/0xwheatyz/gitea-mobile/internal/middleware"
|
||||
)
|
||||
|
||||
// TestMain changes the working directory to the project root so that
|
||||
// template files can be found by handlers that use relative paths.
|
||||
func TestMain(m *testing.M) {
|
||||
// Walk up from this source file to find the project root (where go.mod lives).
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
dir := filepath.Dir(filename)
|
||||
for {
|
||||
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
|
||||
break
|
||||
}
|
||||
parent := filepath.Dir(dir)
|
||||
if parent == dir {
|
||||
// Reached filesystem root without finding go.mod; run tests from cwd.
|
||||
break
|
||||
}
|
||||
dir = parent
|
||||
}
|
||||
_ = os.Chdir(dir)
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
// Note: TestMain is no longer needed because templates and static assets
|
||||
// are embedded at compile time via go:embed. Tests work from any directory.
|
||||
|
||||
// mockGiteaAPI starts an httptest server that simulates the Gitea API
|
||||
// endpoints needed by the handlers. Returns the server and a cleanup func.
|
||||
|
||||
Reference in New Issue
Block a user