Skip to content

Instantly share code, notes, and snippets.

@eltonjncorreia
Last active May 26, 2024 04:35
Show Gist options
  • Save eltonjncorreia/12507d98598042051684e2ff1909c97d to your computer and use it in GitHub Desktop.
Save eltonjncorreia/12507d98598042051684e2ff1909c97d to your computer and use it in GitHub Desktop.
// RenderOnePage renderiza paginas html
// Uso: RenderOnePage(w, r, &config.Application{}, "index.html", TemplateData{})
func RenderOnePage(w http.ResponseWriter, r *http.Request, app *config.Application, html string, data TemplateData) {
ts, err := template.ParseFiles(html)
if err != nil {
app.Logger.Error(err.Error())
ServerError(w, r, app, err)
return
}
err = ts.Execute(w, data)
if err != nil {
app.Logger.Error(err.Error())
ServerError(w, r, app, err)
return
}
}
// Application é usado para injetar dependências
type Application struct {
Logger *slog.Logger
}
// ServerError levanta uma exceção Internal Server Error
func ServerError(w http.ResponseWriter, r *http.Request, app *config.Application, err error) {
app.Logger.Error(err.Error(), "Path:", r.URL.Path)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment