ncDocConverter/internal/frontend/handler.go

32 lines
613 B
Go
Raw Normal View History

2022-09-22 13:22:58 +00:00
package frontend
import (
"bytes"
"fmt"
"net/http"
)
func (app *Frontend) home(w http.ResponseWriter, r *http.Request) {
app.render(w, http.StatusOK, "main.tmpl.html", app.newTemplateData(r))
}
func (app *Frontend) render(w http.ResponseWriter, status int, page string, data *templateData) {
ts, ok := app.templateCache[page]
if !ok {
err := fmt.Errorf("the template %s does not exist", page)
app.serverError(w, err)
return
}
buf := new(bytes.Buffer)
err := ts.ExecuteTemplate(buf, "base", data)
if err != nil {
app.serverError(w, err)
return
}
w.WriteHeader(status)
buf.WriteTo(w)
}