Fix embedded static files on production builds

This commit is contained in:
Eli Ribble 2026-03-21 05:38:42 +00:00
parent 9cbce4ff14
commit a2c3f52ab4
No known key found for this signature in database
12 changed files with 209 additions and 3083 deletions

View file

@ -21,15 +21,20 @@ pkgs.buildGoModule rec {
preBuild = ''
# Compile SCSS
SASS_SRC_DIR="./scss"
CSS_OUTPUT_DIR="./static/gen/css/"
CSS_OUTPUT_DIR="./static/gen/css"
mkdir -p "$CSS_OUTPUT_DIR"
echo "Compiling $SASS_SRC_DIR/style.scss to $CSS_OUTPUT_DIR/bootstrap.css..."
sass --style=compressed --trace "$SASS_SRC_DIR/style.scss":"$CSS_OUTPUT_DIR/bootstrap.css"
echo "Compiling $SASS_SRC_DIR/style.scss to $CSS_OUTPUT_DIR/style.css..."
sass --style=compressed --trace "$SASS_SRC_DIR/style.scss":"$CSS_OUTPUT_DIR/style.css"
# Generate hash and rename style
STYLE_HASH=$(sha256sum "$CSS_OUTPUT_DIR/style.css" | cut -c1-12)
mv "$CSS_OUTPUT_DIR/style.css" "$CSS_OUTPUT_DIR/style.$STYLE_HASH.css"
echo "Generated CSS style with hash: $STYLE_HASH"
# Bundle TypeScript
JS_OUTPUT_DIR="./static/gen/js/"
JS_OUTPUT_DIR="./static/gen/js"
mkdir -p "$JS_OUTPUT_DIR"
echo "Bundling TypeScript..."
@ -38,15 +43,16 @@ esbuild ts/main.ts --bundle --minify --outfile="$JS_OUTPUT_DIR/bundle.js"
# Generate hash and rename bundle
BUNDLE_HASH=$(sha256sum "$JS_OUTPUT_DIR/bundle.js" | cut -c1-12)
mv "$JS_OUTPUT_DIR/bundle.js" "$JS_OUTPUT_DIR/bundle.$BUNDLE_HASH.js"
echo "Generated JS bundle with hash: $BUNDLE_HASH"
# Generate gen.go with bundle path
cat > gen.go <<EOF
package main
cat > static/gen.go <<EOF
package static
// Generated by Nix build - do not edit manually
const JsBundlePath = "/static/js/bundel.$BUNDLE_HASH.js"
const BundlePathCSS = "/static/gen/css/style.$STYLE_HASH.css"
const BundlePathJS = "/static/gen/js/bundle.$BUNDLE_HASH.js"
EOF
echo "Generated JS bundle with hash: $BUNDLE_HASH"
echo "Generated static/gen.go"
'';
}

View file

@ -1,147 +0,0 @@
package html
import (
"embed"
"fmt"
"io/fs"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/Gleipnir-Technology/nidus-sync/config"
"github.com/Gleipnir-Technology/nidus-sync/static"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log"
)
// fileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
var startedTime time.Time = time.Now()
var localFS http.Dir
func AddStaticRoute(r chi.Router, path string) {
if localFS == "" {
localFS = http.Dir("./static")
}
fileServer(r, "/static", localFS, static.EmbeddedStaticFS, "static")
}
func fileServer(r chi.Router, path string, root http.FileSystem, embeddedFS embed.FS, embeddedPath string) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit any URL parameters.")
}
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
// Determine the actual file path
requestedPath := strings.TrimPrefix(r.URL.Path, pathPrefix)
var err error
var fileToServe http.File
found := false
// For dev, try the current filesystem
if !config.IsProductionEnvironment() {
// Try to open from local filesystem for development
fileToServe, err = root.Open(requestedPath)
if err != nil {
log.Warn().Str("path", requestedPath).Msg("Failed to read static file for dev")
found = false
} else {
found = true
}
}
// For production use the embedded filesystem
if !found {
embeddedFilePath := filepath.Join(embeddedPath, requestedPath)
embeddedFile, err := embeddedFS.Open(embeddedFilePath)
if err != nil {
http.NotFound(w, r)
return
}
// Wrap the embedded file to implement http.File interface
fileToServe = &embeddedFileWrapper{embeddedFile}
}
// Create a custom ResponseWriter that allows us to modify headers
crw := &customResponseWriter{ResponseWriter: w}
// Add caching headers
if config.IsProductionEnvironment() {
ext := filepath.Ext(requestedPath)
switch ext {
case ".css", ".js", ".jpg", ".jpeg", ".png", ".gif", ".svg", ".woff", ".woff2", ".ttf":
// Cache for 1 week (604800 seconds)
crw.Header().Set("Cache-Control", "public, max-age=604800, stale-while-revalidate=86400")
default:
// Other files, 1 hour
crw.Header().Set("Cache-Control", "public, max-age=3600")
}
}
// Serve the file
http.ServeContent(crw, r, requestedPath, startedTime, fileToServe)
// Close the file
fileToServe.Close()
})
}
type embeddedFileWrapper struct {
file fs.File
}
func (e *embeddedFileWrapper) Close() error {
return e.file.Close()
}
func (e *embeddedFileWrapper) Read(p []byte) (n int, err error) {
return e.file.Read(p)
}
type Seeker interface {
Seek(offset int64, whence int) (int64, error)
}
func (e *embeddedFileWrapper) Seek(offset int64, whence int) (int64, error) {
if seeker, ok := e.file.(Seeker); ok {
return seeker.Seek(offset, whence)
}
return 0, fmt.Errorf("Seek not supported")
}
func (e *embeddedFileWrapper) Readdir(count int) ([]os.FileInfo, error) {
// This is a bit tricky with embedded files
if dirFile, ok := e.file.(fs.ReadDirFile); ok {
entries, err := dirFile.ReadDir(count)
if err != nil {
return nil, err
}
fileInfos := make([]os.FileInfo, len(entries))
for i, entry := range entries {
fileInfos[i], err = entry.Info()
if err != nil {
return nil, err
}
}
return fileInfos, nil
}
return nil, fmt.Errorf("Readdir not supported")
}
func (e *embeddedFileWrapper) Stat() (os.FileInfo, error) {
return e.file.Stat()
}

View file

@ -6,33 +6,6 @@ import (
"github.com/rs/zerolog/log"
)
// Custom ResponseWriter to track Content-Type
type customResponseWriter struct {
http.ResponseWriter
contentType string
wroteHeader bool
}
func (crw *customResponseWriter) WriteHeader(code int) {
crw.wroteHeader = true
crw.ResponseWriter.WriteHeader(code)
}
func (crw *customResponseWriter) Header() http.Header {
return crw.ResponseWriter.Header()
}
func (crw *customResponseWriter) Write(b []byte) (int, error) {
if !crw.wroteHeader {
if crw.contentType == "" {
crw.contentType = http.DetectContentType(b)
crw.ResponseWriter.Header().Set("Content-Type", crw.contentType)
}
crw.WriteHeader(http.StatusOK)
}
return crw.ResponseWriter.Write(b)
}
// Respond with an error that is visible to the user
func RespondError(w http.ResponseWriter, m string, e error, s int) {
log.Warn().Int("status", s).Err(e).Str("user message", m).Msg("Responding with an error")

View file

@ -92,7 +92,7 @@
{{ template "content" . }}
</div>
<div id="flogo"></div>
<script src="/static/vendor/js/bootstrap.bundle.min.js"></script>
<script src="/static/vendor/bootstrap-5.3.8/bootstrap.bundle.min.js"></script>
<script>
document.getElementById("sidebarToggle").addEventListener("click", () => {
const sidebar = document.getElementById("sidebar");

View file

@ -2,6 +2,7 @@ package rmo
import (
"github.com/Gleipnir-Technology/nidus-sync/html"
"github.com/Gleipnir-Technology/nidus-sync/static"
"github.com/go-chi/chi/v5"
)
@ -45,6 +46,6 @@ func Router() chi.Router {
r.Get("/status", getStatus)
r.Get("/status/{report_id}", getStatusByID)
r.Get("/terms-of-service", getTerms)
html.AddStaticRoute(r, "/static")
static.AddStaticRoute(r, "/static")
return r
}

View file

@ -50,7 +50,7 @@ i.bi svg {
width: 18px;
}
@import "./vendor/bootstrap-5.3.8/scss/bootstrap";
$bootstrap-icons-font-dir: "/static/vendor/bootstrap-icons-1.13.1/fonts/";
$bootstrap-icons-font-dir: "/static/vendor/bootstrap-icons-1.13.1/fonts";
@import "./vendor/bootstrap-icons-1.13.1/bootstrap-icons";
@import "./sidebar.scss";

View file

@ -1,7 +1,15 @@
#!/run/current-system/sw/bin/bash
# with MITM
# export $(cat /var/run/secrets/nidus-dev-sync-env | xargs) && MITM_PROXY=http://127.0.0.1:8080 ./nidus-sync 2>&1 | tee nidus-sync.log
#
# original recipe
#export $(cat /var/run/secrets/nidus-dev-sync-env | xargs) && ./nidus-sync 2>&1 | tee nidus-sync.log
# force production environment
export $(cat /var/run/secrets/nidus-dev-sync-env | xargs) && ./nidus-sync -prod 2>&1 | tee nidus-sync.log
# export $(cat /var/run/secrets/nidus-dev-sync-env | xargs) && ./nidus-sync -prod 2>&1 | tee nidus-sync.log
#
# force production environment, but with debug logging
export $(cat /var/run/secrets/nidus-dev-sync-env | xargs) && VERBOSE=1 ./nidus-sync -prod 2>&1 | tee nidus-sync.log
#
# Use nix build output, force production environment
#export $(cat /var/run/secrets/nidus-dev-sync-env | xargs) && ./result/bin/nidus-sync -prod 2>&1 | tee nidus-sync.log

View file

@ -2,7 +2,183 @@ package static
import (
"embed"
"fmt"
"io/fs"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/Gleipnir-Technology/nidus-sync/config"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log"
)
//go:embed css gen file ico js vendor
var EmbeddedStaticFS embed.FS
//go:embed css gen file ico img js vendor
var embeddedStaticFS embed.FS
// fileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
var startedTime time.Time = time.Now()
var localFS http.Dir
func AddStaticRoute(r chi.Router, path string) {
if localFS == "" {
localFS = http.Dir("./static")
// Useful for debugging embedded file issues
if config.IsProductionEnvironment() {
fs.WalkDir(embeddedStaticFS, ".", func(path string, d fs.DirEntry, err error) error {
log.Debug().Str("path", path).Send()
return nil
})
}
}
fileServer(r, "/static", localFS, embeddedStaticFS)
}
func fileServer(r chi.Router, path string, root http.FileSystem, embeddedFS embed.FS) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit any URL parameters.")
}
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
// Determine the actual file path
requestedPath := strings.TrimPrefix(r.URL.Path, pathPrefix+"/")
var err error
var fileToServe http.File
found := false
// For dev, try the current filesystem
if !config.IsProductionEnvironment() {
// Try to open from local filesystem for development
fileToServe, err = root.Open(requestedPath)
if err != nil {
log.Warn().Str("path", requestedPath).Msg("Failed to read static file for dev")
found = false
} else {
found = true
}
}
// For production use the embedded filesystem
if !found {
// Requested paths start with
embeddedFile, err := embeddedFS.Open(requestedPath)
if err != nil {
log.Debug().Err(err).Str("requested path", requestedPath).Msg("Failed to find resource")
http.NotFound(w, r)
return
}
// Wrap the embedded file to implement http.File interface
fileToServe = &embeddedFileWrapper{embeddedFile}
}
// Create a custom ResponseWriter that allows us to modify headers
crw := &customResponseWriter{ResponseWriter: w}
// Add caching headers
if config.IsProductionEnvironment() {
ext := filepath.Ext(requestedPath)
switch ext {
case ".css", ".js", ".jpg", ".jpeg", ".png", ".gif", ".svg", ".woff", ".woff2", ".ttf":
// Cache for 1 week (604800 seconds)
crw.Header().Set("Cache-Control", "public, max-age=604800, stale-while-revalidate=86400")
default:
// Other files, 1 hour
crw.Header().Set("Cache-Control", "public, max-age=3600")
}
}
// Serve the file
http.ServeContent(crw, r, requestedPath, startedTime, fileToServe)
// Close the file
fileToServe.Close()
})
}
type embeddedFileWrapper struct {
file fs.File
}
func (e *embeddedFileWrapper) Close() error {
return e.file.Close()
}
func (e *embeddedFileWrapper) Read(p []byte) (n int, err error) {
return e.file.Read(p)
}
type Seeker interface {
Seek(offset int64, whence int) (int64, error)
}
func (e *embeddedFileWrapper) Seek(offset int64, whence int) (int64, error) {
if seeker, ok := e.file.(Seeker); ok {
return seeker.Seek(offset, whence)
}
return 0, fmt.Errorf("Seek not supported")
}
func (e *embeddedFileWrapper) Readdir(count int) ([]os.FileInfo, error) {
// This is a bit tricky with embedded files
if dirFile, ok := e.file.(fs.ReadDirFile); ok {
entries, err := dirFile.ReadDir(count)
if err != nil {
return nil, err
}
fileInfos := make([]os.FileInfo, len(entries))
for i, entry := range entries {
fileInfos[i], err = entry.Info()
if err != nil {
return nil, err
}
}
return fileInfos, nil
}
return nil, fmt.Errorf("Readdir not supported")
}
func (e *embeddedFileWrapper) Stat() (os.FileInfo, error) {
return e.file.Stat()
}
// Custom ResponseWriter to track Content-Type
type customResponseWriter struct {
http.ResponseWriter
contentType string
wroteHeader bool
}
func (crw *customResponseWriter) WriteHeader(code int) {
crw.wroteHeader = true
crw.ResponseWriter.WriteHeader(code)
}
func (crw *customResponseWriter) Header() http.Header {
return crw.ResponseWriter.Header()
}
func (crw *customResponseWriter) Write(b []byte) (int, error) {
if !crw.wroteHeader {
if crw.contentType == "" {
crw.contentType = http.DetectContentType(b)
crw.ResponseWriter.Header().Set("Content-Type", crw.contentType)
}
crw.WriteHeader(http.StatusOK)
}
return crw.ResponseWriter.Write(b)
}

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load diff

View file

@ -3,7 +3,7 @@ package sync
import (
"github.com/Gleipnir-Technology/nidus-sync/api"
"github.com/Gleipnir-Technology/nidus-sync/auth"
"github.com/Gleipnir-Technology/nidus-sync/html"
"github.com/Gleipnir-Technology/nidus-sync/static"
"github.com/go-chi/chi/v5"
)
@ -92,6 +92,6 @@ func Router() chi.Router {
r.Method("GET", "/text/{destination}", authenticatedHandler(getTextMessages))
r.Method("GET", "/tile/gps", auth.NewEnsureAuth(getTileGPS))
html.AddStaticRoute(r, "/static")
static.AddStaticRoute(r, "/static")
return r
}