Save startup time to avoid double-sending static content
This commit is contained in:
parent
8bf0e24bcd
commit
58da733531
4 changed files with 12 additions and 5 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
|
|
|
|||
1
main.go
1
main.go
|
|
@ -143,6 +143,7 @@ func main() {
|
|||
log.Error().Err(err).Msg("Failed to start openAI client")
|
||||
os.Exit(8)
|
||||
}
|
||||
http.SetStartedTime()
|
||||
server := &http.Server{
|
||||
Addr: config.Bind,
|
||||
Handler: r,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue