func renderHTML(w http.ResponseWriter, r *http.Request, task eremetic.Task, taskID string, conf *config.Config) { var templateFile string data := make(map[string]interface{}) funcMap := template.FuncMap{ "ToLower": strings.ToLower, "FormatTime": FormatTime, } if reflect.DeepEqual(task, (eremetic.Task{})) { notFound(w, r) return } templateFile = "task.html" data = makeMap(task) data["Version"] = version.Version source, _ := assets.Asset(fmt.Sprintf("templates/%s", templateFile)) tpl, err := template.New(templateFile).Funcs(funcMap).Parse(string(source)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) logrus.WithError(err).WithField("template", templateFile).Error("Unable to load template") return } err = tpl.Execute(w, data) if err != nil { logrus.WithError(err).WithField("template", templateFile).Error("Unable to execute template") } }
func notFound(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) if strings.Contains(r.Header.Get("Accept"), "text/html") { src, _ := assets.Asset("templates/error_404.html") tpl, err := template.New("404").Parse(string(src)) if err != nil { logrus.WithError(err).WithField("template", "error_404.html").Error("Unable to load template") } err = tpl.Execute(w, nil) if err != nil { logrus.WithError(err).WithField("template", "error_404.html").Error("Unable to execute template") } } w.Header().Set("Content-Type", "application/json; charset=UTF-8") json.NewEncoder(w).Encode(nil) }
// IndexHandler returns the index template, or no content. func (h Handler) IndexHandler(conf *config.Config) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if strings.Contains(r.Header.Get("Accept"), "text/html") { src, _ := assets.Asset("templates/index.html") tpl, err := template.New("index").Parse(string(src)) data := make(map[string]interface{}) data["Version"] = version.Version if err == nil { tpl.Execute(w, data) return } logrus.WithError(err).WithField("template", "index.html").Error("Unable to load template") } w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusNoContent) json.NewEncoder(w).Encode(nil) } }