Skip to content

Commit

Permalink
Merge pull request #210 from coroot/embedded_static_last_modified_header
Browse files Browse the repository at this point in the history
Set the `Last-Modified` header for embedded static assets
  • Loading branch information
apetruhin committed Apr 30, 2024
2 parents 31cd4f1 + ca8b83f commit 5fab278
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func main() {
if *developerMode {
r.PathPrefix("/static/").Handler(http.StripPrefix(*urlBasePath+"static/", http.FileServer(http.Dir("./static"))))
} else {
r.PathPrefix("/static/").Handler(http.StripPrefix(*urlBasePath, http.FileServer(http.FS(static))))
r.PathPrefix("/static/").Handler(http.StripPrefix(*urlBasePath, http.FileServer(&StaticFSWrapper{FileSystem: http.FS(static), modTime: time.Now()})))
}

indexHtml := readIndexHtml(*urlBasePath, version, instanceUuid, !*doNotCheckForUpdates, *developerMode)
Expand Down Expand Up @@ -331,3 +331,32 @@ func migrateClickhouse(database *db.DB, coll *collector.Collector) {
}(cfg)
}
}

type StaticFSWrapper struct {
http.FileSystem
modTime time.Time
}

func (f *StaticFSWrapper) Open(name string) (http.File, error) {
file, err := f.FileSystem.Open(name)
return &StaticFileWrapper{File: file, modTime: f.modTime}, err
}

type StaticFileWrapper struct {
http.File
modTime time.Time
}

func (f *StaticFileWrapper) Stat() (os.FileInfo, error) {
fileInfo, err := f.File.Stat()
return &StaticFileInfoWrapper{FileInfo: fileInfo, modTime: f.modTime}, err
}

type StaticFileInfoWrapper struct {
os.FileInfo
modTime time.Time
}

func (f *StaticFileInfoWrapper) ModTime() time.Time {
return f.modTime
}

0 comments on commit 5fab278

Please sign in to comment.