Save startup time to avoid double-sending static content

This commit is contained in:
Eli Ribble 2026-02-17 22:05:47 +00:00
parent 8bf0e24bcd
commit 58da733531
No known key found for this signature in database
4 changed files with 12 additions and 5 deletions

View file

@ -8,16 +8,15 @@ import (
"os"
"path/filepath"
"strings"
"time"
"github.com/Gleipnir-Technology/nidus-sync/config"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log"
)
// FileServer conveniently sets up a http.FileServer handler to serve
// fileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
func FileServer(r chi.Router, path string, root http.FileSystem, embeddedFS embed.FS, embeddedPath string) {
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.")
}
@ -68,7 +67,7 @@ func FileServer(r chi.Router, path string, root http.FileSystem, embeddedFS embe
crw := &customResponseWriter{ResponseWriter: w}
// Serve the file
http.ServeContent(crw, r, requestedPath, time.Time{}, fileToServe)
http.ServeContent(crw, r, requestedPath, startedTime, fileToServe)
// Close the file
fileToServe.Close()

View file

@ -2,8 +2,15 @@ package html
import (
"net/http"
"time"
)
func RenderOrError(w http.ResponseWriter, template_name string, content interface{}) {
templates.renderOrError(w, template_name, content)
}
var startedTime time.Time
func SetStartedTime() {
startedTime = time.Now()
}

View file

@ -16,5 +16,5 @@ func AddStaticRoute(r chi.Router, path string) {
if localFS == "" {
localFS = http.Dir("./html/static")
}
FileServer(r, "/static", localFS, EmbeddedStaticFS, "static")
fileServer(r, "/static", localFS, EmbeddedStaticFS, "static")
}