Files
oslstats/internal/handlers/static.go
2026-03-05 18:32:55 +11:00

30 lines
899 B
Go

package handlers
import (
"net/http"
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/oslstats/internal/respond"
"git.haelnorr.com/h/oslstats/internal/throw"
)
// StaticFS handles requests for static files, without allowing access to the
// directory viewer and returning 404 if an exact file is not found
func StaticFS(staticFS *http.FileSystem, server *hws.Server) http.Handler {
// Create the file server once, not on every request
fs, err := hws.SafeFileServer(staticFS)
if err != nil {
// If we can't create the file server, return a handler that always errors
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
throw.InternalServiceError(server, w, r, "An error occurred trying to load the file system", err)
})
}
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
respond.ContentType(w, r)
fs.ServeHTTP(w, r)
},
)
}