Use gorilla/mux patterns

This commit is contained in:
Las Zenow 2013-04-15 00:59:19 +02:00
parent 3a99f86f32
commit 402dbabdbd
3 changed files with 32 additions and 45 deletions

View file

@ -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

View file

@ -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, "<body")
if len(str) < 2 {
@ -140,7 +128,9 @@ func listChapters(nav *epubgo.NavigationIterator, depth int) []chapter {
}
func readHandler(w http.ResponseWriter, r *http.Request) {
base, id, file := parseUrl(r.URL.Path)
vars := mux.Vars(r)
id := vars["id"]
file := vars["file"]
books, _, err := db.GetBooks(bson.M{"_id": bson.ObjectIdHex(id)})
if err != nil || len(books) == 0 {
http.NotFound(w, r)
@ -171,19 +161,21 @@ func readHandler(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
http.Redirect(w, r, base+id+"/"+it.Url(), http.StatusTemporaryRedirect)
http.Redirect(w, r, "/read/"+id+"/"+it.Url(), http.StatusTemporaryRedirect)
return
}
data.S = GetStatus(w, r)
data.Next, data.Prev = getNextPrev(e, file, id, base)
data.Chapters = getChapters(e, file, id, base)
data.Next, data.Prev = getNextPrev(e, file, id, "/read/")
data.Chapters = getChapters(e, file, id, "/read/")
data.Content = genLink(id, "/content/", file)
loadTemplate(w, "read", data)
}
func contentHandler(w http.ResponseWriter, r *http.Request) {
_, id, file := parseUrl(r.URL.Path)
vars := mux.Vars(r)
id := vars["id"]
file := vars["file"]
if file == "" {
http.NotFound(w, r)
return

View file

@ -30,20 +30,18 @@ func logoutHandler(w http.ResponseWriter, r *http.Request) {
}
func loginHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
user := r.FormValue("user")
pass := r.FormValue("pass")
sess := GetSession(r)
if db.UserValid(user, pass) {
log.Println("User", user, "log in")
sess.LogIn(user)
sess.Notify("Successful login!", "Welcome "+user, "success")
} else {
log.Println("User", user, "bad user or password")
sess.Notify("Invalid login!", "user or password invalid", "error")
}
sess.Save(w, r)
user := r.FormValue("user")
pass := r.FormValue("pass")
sess := GetSession(r)
if db.UserValid(user, pass) {
log.Println("User", user, "log in")
sess.LogIn(user)
sess.Notify("Successful login!", "Welcome "+user, "success")
} else {
log.Println("User", user, "bad user or password")
sess.Notify("Invalid login!", "user or password invalid", "error")
}
sess.Save(w, r)
http.Redirect(w, r, r.Referer(), http.StatusFound)
}
@ -55,7 +53,7 @@ type bookData struct {
func bookHandler(w http.ResponseWriter, r *http.Request) {
var data bookData
data.S = GetStatus(w, r)
id := bson.ObjectIdHex(r.URL.Path[len("/book/"):])
id := bson.ObjectIdHex(mux.Vars(r)["id"])
books, _, err := db.GetBooks(bson.M{"_id": id})
if err != nil || len(books) == 0 {
http.NotFound(w, r)
@ -67,7 +65,7 @@ func bookHandler(w http.ResponseWriter, r *http.Request) {
}
func downloadHandler(w http.ResponseWriter, r *http.Request) {
id := bson.ObjectIdHex(r.URL.Path[len("/books/"):])
id := bson.ObjectIdHex(mux.Vars(r)["id"])
books, _, err := db.GetBooks(bson.M{"_id": id})
if err != nil || len(books) == 0 {
http.NotFound(w, r)
@ -136,16 +134,16 @@ func main() {
r.HandleFunc("/book/{id:[0-9a-fA-F]+}", bookHandler)
r.HandleFunc("/search/{query}", searchHandler)
r.HandleFunc("/upload/", uploadHandler)
r.HandleFunc("/login/", loginHandler)
r.HandleFunc("/login/", loginHandler).Methods("POST")
r.HandleFunc("/logout/", logoutHandler)
r.HandleFunc("/new/", newHandler)
r.HandleFunc("/store/{ids:([0-9a-fA-F]+/)+}", storeHandler)
r.HandleFunc("/delete/{ids:([0-9a-fA-F]+/)+}", deleteHandler)
r.HandleFunc("/read/{id:[0-9a-fA-F]+}", readHandler)
r.HandleFunc("/read/{id:[0-9a-fA-F]+}/{path}", readHandler)
r.HandleFunc("/content/{id:[0-9a-fA-F]+}/{path}", contentHandler)
r.HandleFunc("/read/{id:[0-9a-fA-F]+}/{file:.*}", readHandler)
r.HandleFunc("/content/{id:[0-9a-fA-F]+}/{file:.*}", contentHandler)
r.HandleFunc("/edit/{id:[0-9a-fA-F]+}", editHandler)
r.HandleFunc("/save/{id:[0-9a-fA-F]+}", saveHandler)
r.HandleFunc("/save/{id:[0-9a-fA-F]+}", saveHandler).Methods("POST")
r.HandleFunc("/about/", aboutHandler)
r.HandleFunc("/books/{id:[0-9a-fA-F]+}", downloadHandler)
r.HandleFunc("/settings/", settingsHandler)