Skip to content
Snippets Groups Projects
Select Git revision
  • 277af91499e00810c12e7cfa7090cd301050f4ca
  • master default
  • registry-mirror
  • nginx-default-site
  • acmeserver2
  • clickhouse
  • improve-dns-toplevel-probes
  • tabacco-in-container
  • rsyslog-modern-json
  • improve-service-discovery
  • prometheus-external-healthchecks
  • env-vars-in-include-paths
  • dns-resolver
  • service-turndown
  • use_proxy_protocol
  • loki
  • docs_operating
  • net-overlay_firewall_containers
  • webdiff
19 results

reference.md

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