Andrey Meshkov da0cb6fd0e Sync v2.9.0
2024-10-14 17:44:24 +03:00

43 lines
878 B
Go

package websvc
import (
"maps"
"net/http"
)
// StaticContent serves static content with the given content type. Elements
// must not be nil.
type StaticContent map[string]*StaticFile
// StaticFile is a single file in a [StaticFS].
type StaticFile struct {
// Headers contains headers of the HTTP response.
Headers http.Header
// Content is the file content.
Content []byte
}
// type check
var _ http.Handler = StaticContent(nil)
// ServeHTTP implements the [http.Handler] interface for StaticContent.
func (sc StaticContent) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p := r.URL.Path
f, ok := sc[p]
if !ok {
http.NotFound(w, r)
return
}
respHdr := w.Header()
maps.Copy(respHdr, f.Headers)
w.WriteHeader(http.StatusOK)
_, err := w.Write(f.Content)
if err != nil {
logErrorByType(err, "websvc: static content: writing %s: %s", p, err)
}
}