github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/server/templates.go (about)

     1  package server
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net/http"
     7  	"text/template"
     8  )
     9  
    10  func getTemplate(dir http.FileSystem, path string) (*template.Template, error) {
    11  	f, err := dir.Open(path)
    12  	if err != nil {
    13  		return nil, fmt.Errorf("could not find file %s: %q", path, err)
    14  	}
    15  
    16  	b, err := io.ReadAll(f)
    17  	if err != nil {
    18  		return nil, fmt.Errorf("could not read file %s: %q", path, err)
    19  	}
    20  
    21  	tmpl, err := template.New(path).Parse(string(b))
    22  	if err != nil {
    23  		return nil, fmt.Errorf("could not parse %s template: %q", path, err)
    24  	}
    25  	return tmpl, nil
    26  }
    27  
    28  func mustExecute(t *template.Template, w io.Writer, v interface{}) {
    29  	if err := t.Execute(w, v); err != nil {
    30  		panic(err)
    31  	}
    32  }