Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
78 lines (61 sloc) 1.54 KB
package site
import "net/http"
//gistsnip:start:db
type DB interface {
Comments() Comments
}
type Comments interface {
Add(user, comment string) error
List() ([]Comment, error)
}
type Server struct {
db DB
}
//gistsnip:end:db
func NewServer(db DB) *Server {
return &Server{
db: db,
}
}
func (server *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
server.HandleList(w, r)
case "/comment":
server.HandleAddComment(w, r)
default:
ShowErrorPage(w, http.StatusNotFound, "Page not found", nil)
}
}
//gistsnip:start:db
func (server *Server) HandleList(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
ShowErrorPage(w, http.StatusMethodNotAllowed, "Invalid method", nil)
return
}
comments, err := server.db.Comments().List()
if err != nil {
ShowErrorPage(w, http.StatusInternalServerError, "Unable to access DB", err)
return
}
ShowCommentsPage(w, comments)
}
//gistsnip:end:db
func (server *Server) HandleAddComment(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 := server.db.Comments().Add(user, comment)
if err != nil {
ShowErrorPage(w, http.StatusInternalServerError, "Unable to add data", err)
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
You can’t perform that action at this time.