Skip to content
Snippets Groups Projects
Select Git revision
  • 711caeabaf8cdf9f1cd369d54c0df198a654fb28
  • master default protected
2 results

main.go

Blame
  • 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))
    }