diff options
author | xengineering <me@xengineering.eu> | 2023-02-08 22:27:15 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-02-08 22:27:49 +0100 |
commit | f18293689c48edbdcac7f636f7edd1e1407fee35 (patch) | |
tree | 1baaacf2c15a552f01ee0a8b025e81d8457c4b9c /handler.go | |
parent | f9a5140071703faf0c7515a05f52e69fdc1f11ba (diff) | |
download | ceres-f18293689c48edbdcac7f636f7edd1e1407fee35.tar ceres-f18293689c48edbdcac7f636f7edd1e1407fee35.tar.zst ceres-f18293689c48edbdcac7f636f7edd1e1407fee35.zip |
Test mux concept with index handler
Diffstat (limited to 'handler.go')
-rw-r--r-- | handler.go | 59 |
1 files changed, 28 insertions, 31 deletions
@@ -26,43 +26,40 @@ func static(filename string, staticRoot string) func(http.ResponseWriter, *http. } } -func index(db *Database, templateRoot string) func(http.ResponseWriter, *http.Request) { +func indexGet(w http.ResponseWriter, r *http.Request, db *Database, templateRoot string) { + + // get data from database + cmd := "SELECT id,title FROM recipes ORDER BY title;" + log.Printf("Query: %s", cmd) + rows, err := db.Backend.Query(cmd) + if err != nil { + Err(w, 1) + return + } + defer rows.Close() - return func(w http.ResponseWriter, r *http.Request) { + // prepare data store + type Element struct { + Id string + Title string + } + elements := make([]Element, 0) - // get data from database - cmd := "SELECT id,title FROM recipes ORDER BY title;" - log.Printf("Query: %s", cmd) - rows, err := db.Backend.Query(cmd) + // scan database rows to data store + for rows.Next() { + var element Element + err := rows.Scan(&element.Id, &element.Title) if err != nil { - Err(w, 1) + Err(w, 2) return + } else { + elements = append(elements, element) } - defer rows.Close() - - // prepare data store - type Element struct { - Id string - Title string - } - elements := make([]Element, 0) - - // scan database rows to data store - for rows.Next() { - var element Element - err := rows.Scan(&element.Id, &element.Title) - if err != nil { - Err(w, 2) - return - } else { - elements = append(elements, element) - } - } - - // render and return template - path := filepath.Join(templateRoot, "index.html") - ServeTemplate(w, "index", path, elements) } + + // render and return template + path := filepath.Join(templateRoot, "index.html") + ServeTemplate(w, "index", path, elements) } func recipe(db *Database, templateRoot string) func(http.ResponseWriter, *http.Request) { |