Adding a Handler
Handlers are responsible for accepting incoming requests, validating requests, calling the relevant backend functions, and then constructing a response back to the client. Handlers should be called with relevant server objects they need, for example the database connection, storage bucket, etc.
Location
Place your handlers close to the pages that they handle. For example
root
|--internal
| |--ui
| |--pages // frontend pages
| |--section // website section
| |--handlers.go // section handlers
| |--section_page.go // section page
| |--index.templ // the app's main template page
| |--handlers.go // root handlers
Database
Handlers are responsible for wrapping operations in database transactions and handling errors. For example in our mytodo app the main index page will get the available todo lists as below.
func Index(db db.DB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var l []list.List
var err error
err = db.Transaction(r.Context(), func(tx *sql.Tx) error {
l, err = list.GetLists(r.Context(), tx)
return err
})
if err != nil {
http.Error(w, "error getting lists", http.StatusInternalServerError)
return
}
templ.Handler(ui.Index(l)).ServeHTTP(w, r)
})
}