package panelui
import (
"fmt"
"html/template"
"net/http"
"os"
"strings"
"github.com/GeertJohan/go.rice"
)
func templateGet(w http.ResponseWriter, r *http.Request, name string, box *rice.Box) (*template.Template, error) {
tmplRaw, err := box.Bytes(name)
if err != nil {
w.WriteHeader(500)
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(w, "Unexpected error processing template")
return nil, err
}
tmpl := template.Must(template.New(name).Parse(string(tmplRaw)))
return tmpl, nil
}
func templateExecute(w http.ResponseWriter, r *http.Request, name string, box *rice.Box, data map[string]interface{}) error {
tmpl, err := templateGet(w, r, name, box)
if err != nil {
return err
}
// TODO: run template _before_ writing it (to a buffer) so that appropriate error can be checked
w.WriteHeader(200)
if strings.HasSuffix(name, ".html") {
w.Header().Set("Content-type", "text/html")
}
if pref, has := os.LookupEnv("PANEL_PREFIX"); has {
data["BaseURL"] = pref
} else {
data["BaseURL"] = "/"
}
tmpl.Execute(w, data)
return nil
}