Select Git revision
static.go 788 B
package httputil
import (
"bytes"
"io/ioutil"
"net/http"
"os"
"time"
)
// StaticContent is an http.Handler that serves in-memory data as if
// it were a static file.
type StaticContent struct {
modtime time.Time
name string
data []byte
}
// LoadStaticContent creates a StaticContent by loading data from a file.
func LoadStaticContent(path string) (*StaticContent, error) {
stat, err := os.Stat(path)
if err != nil {
return nil, err
}
data, err := ioutil.ReadFile(path) // #nosec
if err != nil {
return nil, err
}
return &StaticContent{
name: path,
modtime: stat.ModTime(),
data: data,
}, nil
}
func (c *StaticContent) ServeHTTP(w http.ResponseWriter, req *http.Request) {
http.ServeContent(w, req, c.name, c.modtime, bytes.NewReader(c.data))
}