Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
64 lines (51 sloc) 1.35 KB
package main
import (
"log"
"net/http"
)
type Comment struct {
User string
Text string
}
func main() {
//gistsnip:start:main
commentsRepo, err := NewComments("user=dbdemo password=dbdemo dbname=dbdemo sslmode=disable")
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
ShowErrorPage(w, http.StatusMethodNotAllowed, "Invalid method", nil)
return
}
comments, err := commentsRepo.List()
if err != nil {
ShowErrorPage(w, http.StatusInternalServerError, "Unable to access DB", err)
return
}
ShowCommentsPage(w, comments)
})
//gistsnip:end:main
http.HandleFunc("/comment", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
ShowErrorPage(w, http.StatusMethodNotAllowed, "Invalid method", nil)
return
}
if err := r.ParseForm(); err != nil {
ShowErrorPage(w, http.StatusBadRequest, "Unable to parse data", err)
return
}
user := r.Form.Get("user")
comment := r.Form.Get("comment")
err := commentsRepo.Add(user, comment)
if err != nil {
ShowErrorPage(w, http.StatusInternalServerError, "Unable to add data", err)
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
})
log.Println("Started listening on :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
You can’t perform that action at this time.