From 402dbabdbd44a5e2f425bf5adbbc81493d38b9b0 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Mon, 15 Apr 2013 00:59:19 +0200 Subject: [PATCH] Use gorilla/mux patterns --- admin.go | 13 +++++-------- reader.go | 28 ++++++++++------------------ trantor.go | 36 +++++++++++++++++------------------- 3 files changed, 32 insertions(+), 45 deletions(-) diff --git a/admin.go b/admin.go index 831d713..cbcde5f 100644 --- a/admin.go +++ b/admin.go @@ -1,6 +1,7 @@ package main import ( + "github.com/gorilla/mux" "labix.org/v2/mgo/bson" "log" "net/http" @@ -47,7 +48,7 @@ func deleteHandler(w http.ResponseWriter, r *http.Request) { var titles []string var isNew bool - ids := strings.Split(r.URL.Path[len("/delete/"):], "/") + ids := strings.Split(mux.Vars(r)["ids"], "/") for _, idStr := range ids { if idStr == "" { continue @@ -85,7 +86,7 @@ func editHandler(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) return } - id := bson.ObjectIdHex(r.URL.Path[len("/edit/"):]) + id := bson.ObjectIdHex(mux.Vars(r)["id"]) books, _, err := db.GetBooks(bson.M{"_id": id}) if err != nil { http.NotFound(w, r) @@ -109,17 +110,13 @@ func cleanEmptyStr(s []string) []string { } func saveHandler(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - http.NotFound(w, r) - return - } sess := GetSession(r) if sess.User == "" { http.NotFound(w, r) return } - idStr := r.URL.Path[len("/save/"):] + idStr := mux.Vars(r)["id"] id := bson.ObjectIdHex(idStr) title := r.FormValue("title") publisher := r.FormValue("publisher") @@ -217,7 +214,7 @@ func storeHandler(w http.ResponseWriter, r *http.Request) { } var titles []string - ids := strings.Split(r.URL.Path[len("/store/"):], "/") + ids := strings.Split(mux.Vars(r)["ids"], "/") for _, idStr := range ids { if idStr == "" { continue diff --git a/reader.go b/reader.go index 31ca9c5..f88004f 100644 --- a/reader.go +++ b/reader.go @@ -2,10 +2,10 @@ package main import ( "git.gitorious.org/go-pkg/epubgo.git" + "github.com/gorilla/mux" "io" "labix.org/v2/mgo/bson" "net/http" - "regexp" "strconv" "strings" ) @@ -29,18 +29,6 @@ type readData struct { Back string } -func parseUrl(url string) (string, string, string) { - exp, _ := regexp.Compile("^(\\/[^\\/]*\\/)([^\\/]*)\\/?(.*)?$") - res := exp.FindStringSubmatch(url) - base := res[1] - id := res[2] - file := "" - if len(res) == 4 { - file = res[3] - } - return base, id, file -} - func cleanHtml(html string) string { str := strings.Split(html, "