Example #1
0
func (s *Server) renderTemplate(w http.ResponseWriter, page string) {
	html, err := statics.Asset("statics/" + page)
	if err != nil {
		logging.GetLogger().Error("Unable to find the asset " + page)
		w.WriteHeader(http.StatusNotFound)
		return
	}

	t := template.New(page)
	t, err = t.Parse(string(html))
	if err != nil {
		panic(err)
	}

	w.Header().Set("Content-Type", "text/html; charset=UTF-8")
	w.WriteHeader(http.StatusOK)
	if err = t.Execute(w, s.getTemplateData()); err != nil {
		logging.GetLogger().Errorf("Unable to render template: %s", err.Error())
	}
}
Example #2
0
func serveStatics(w http.ResponseWriter, r *http.Request) {
	upath := r.URL.Path
	if strings.HasPrefix(upath, "/") {
		upath = strings.TrimPrefix(upath, "/")
	}

	content, err := statics.Asset(upath)
	if err != nil {
		logging.GetLogger().Errorf("Unable to find the asset: %s", upath)
		w.WriteHeader(http.StatusNotFound)
		return
	}

	ext := filepath.Ext(upath)
	ct := mime.TypeByExtension(ext)

	w.Header().Set("Content-Type", ct+"; charset=UTF-8")
	w.WriteHeader(http.StatusOK)
	w.Write(content)
}