Last active
August 29, 2015 14:07
-
-
Save hilem/1e4c7fcf53fbc74a1cb5 to your computer and use it in GitHub Desktop.
Playing with Pat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
r "github.com/dancannon/gorethink" | |
//$ "github.com/gorilla/mux" | |
"github.com/bmizerany/pat" | |
"log" | |
"net/http" | |
"time" | |
) | |
var ( | |
// router *mux.Router | |
router *pat.PatternServeMux | |
session *r.Session | |
) | |
func init() { | |
var err error | |
session, err = r.Connect(r.ConnectOpts{ | |
Address: "localhost:28015", | |
Database: "todo", | |
}) | |
if err != nil { | |
log.Fatalln(err.Error()) | |
} | |
} | |
func NewServer(addr string) *http.Server { | |
// Setup router | |
router = initRouting() | |
// Create and start server | |
return &http.Server{ | |
Addr: addr, | |
Handler: router, | |
} | |
} | |
func StartServer(server *http.Server) { | |
err := server.ListenAndServe() | |
if err != nil { | |
log.Fatalln("Error: %v", err) | |
} | |
} | |
// func initRouting() *mux.Router { | |
func initRouting() *pat.PatternServeMux { | |
m := pat.New() | |
//$ r := mux.NewRouter() | |
m.Get("/", http.HandlerFunc(indexHandler)) // works | |
m.Get("/active", http.HandlerFunc(activeIndexHandler)) // works | |
m.Get("/completed", http.HandlerFunc(completedIndexHandler)) // works | |
m.Post("/new", http.HandlerFunc(newHandler)) // works | |
m.Get("/toggle/:id", http.HandlerFunc(toggleHandler)) // need to get static to work to test following | |
m.Get("/delete/:id", http.HandlerFunc(deleteHandler)) | |
m.Get("/clear", http.HandlerFunc(clearHandler)) | |
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) | |
// r.HandleFunc("/", indexHandler) | |
// r.HandleFunc("/active", activeIndexHandler) | |
// r.HandleFunc("/completed", completedIndexHandler) | |
// r.HandleFunc("/new", newHandler) | |
// r.HandleFunc("/toggle/{id}", toggleHandler) | |
// r.HandleFunc("/delete/{id}", deleteHandler) | |
// r.HandleFunc("/clear", clearHandler) | |
// Add handler for static files | |
// r.PathPrefix("/").Handler(http.FileServer(http.Dir("static"))) | |
// return r | |
return m | |
} | |
// Handlers | |
func indexHandler(w http.ResponseWriter, req *http.Request) { | |
items := []TodoItem{} | |
// Fetch all the items from the database | |
res, err := r.Table("items").OrderBy(r.Asc("Created")).Run(session) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
err = res.All(&items) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
renderTemplate(w, "index", items) | |
} | |
func activeIndexHandler(w http.ResponseWriter, req *http.Request) { | |
items := []TodoItem{} | |
// Fetch all the items from the database | |
query := r.Table("items").Filter(r.Row.Field("Status").Eq("active")) | |
query = query.OrderBy(r.Asc("Created")) | |
res, err := query.Run(session) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
err = res.All(&items) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
renderTemplate(w, "index", items) | |
} | |
func completedIndexHandler(w http.ResponseWriter, req *http.Request) { | |
items := []TodoItem{} | |
// Fetch all the items from the database | |
query := r.Table("items").Filter(r.Row.Field("Status").Eq("complete")) | |
query = query.OrderBy(r.Asc("Created")) | |
res, err := query.Run(session) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
err = res.All(&items) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
renderTemplate(w, "index", items) | |
} | |
func newHandler(w http.ResponseWriter, req *http.Request) { | |
// Create the item | |
item := NewTodoItem(req.PostFormValue("text")) | |
item.Created = time.Now() | |
// Insert the new item into the database | |
_, err := r.Table("items").Insert(item).RunWrite(session) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
http.Redirect(w, req, "/", http.StatusFound) | |
} | |
func toggleHandler(w http.ResponseWriter, req *http.Request) { | |
// vars := mux.Vars(req) | |
// id := vars["id"] | |
id := req.URL.Query().Get(":id") | |
if id == "" { | |
http.NotFound(w, req) | |
return | |
} | |
// Check that the item exists | |
res, err := r.Table("items").Get(id).Run(session) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
if res.IsNil() { | |
http.NotFound(w, req) | |
return | |
} | |
// Toggle the item | |
_, err = r.Table("items").Get(id).Update(map[string]interface{}{"Status": r.Branch( | |
r.Row.Field("Status").Eq("active"), | |
"complete", | |
"active", | |
)}).RunWrite(session) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
http.Redirect(w, req, "/", http.StatusFound) | |
} | |
func deleteHandler(w http.ResponseWriter, req *http.Request) { | |
// vars := mux.Vars(req) | |
// id := vars["id"] | |
id := req.URL.Query().Get(":id") | |
if id == "" { | |
http.NotFound(w, req) | |
return | |
} | |
// Check that the item exists | |
res, err := r.Table("items").Get(id).Run(session) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
if res.IsNil() { | |
http.NotFound(w, req) | |
return | |
} | |
// Delete the item | |
_, err = r.Table("items").Get(id).Delete().RunWrite(session) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
http.Redirect(w, req, "/", http.StatusFound) | |
} | |
func clearHandler(w http.ResponseWriter, req *http.Request) { | |
// Delete all completed items | |
_, err := r.Table("items").Filter( | |
r.Row.Field("Status").Eq("complete"), | |
).Delete().RunWrite(session) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
http.Redirect(w, req, "/", http.StatusFound) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment