Use clousures for the http handlers
This commit is contained in:
parent
f1eab1bebc
commit
3db6231762
3 changed files with 54 additions and 48 deletions
|
@ -31,7 +31,8 @@ type searchData struct {
|
||||||
Prev string
|
Prev string
|
||||||
}
|
}
|
||||||
|
|
||||||
func searchHandler(coll *mgo.Collection, w http.ResponseWriter, r *http.Request) {
|
func searchHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
err := r.ParseForm()
|
err := r.ParseForm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
@ -66,3 +67,4 @@ func searchHandler(coll *mgo.Collection, w http.ResponseWriter, r *http.Request)
|
||||||
}
|
}
|
||||||
loadTemplate(w, "search", data)
|
loadTemplate(w, "search", data)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
10
trantor.go
10
trantor.go
|
@ -16,11 +16,13 @@ func aboutHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
loadTemplate(w, "about", nil)
|
loadTemplate(w, "about", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func bookHandler(coll *mgo.Collection, w http.ResponseWriter, r *http.Request) {
|
func bookHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var book Book
|
var book Book
|
||||||
coll.Find(bson.M{"title": r.URL.Path[len("/book/"):]}).One(&book)
|
coll.Find(bson.M{"title": r.URL.Path[len("/book/"):]}).One(&book)
|
||||||
loadTemplate(w, "book", book)
|
loadTemplate(w, "book", book)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func fileHandler(path string) {
|
func fileHandler(path string) {
|
||||||
|
@ -37,9 +39,9 @@ func main() {
|
||||||
coll := session.DB(DB_NAME).C(BOOKS_COLL)
|
coll := session.DB(DB_NAME).C(BOOKS_COLL)
|
||||||
num, _ := coll.Count()
|
num, _ := coll.Count()
|
||||||
|
|
||||||
http.HandleFunc("/book/", func(w http.ResponseWriter, r *http.Request) { bookHandler(coll, w, r) })
|
http.HandleFunc("/book/", bookHandler(coll))
|
||||||
http.HandleFunc("/search/", func(w http.ResponseWriter, r *http.Request) { searchHandler(coll, w, r) })
|
http.HandleFunc("/search/", searchHandler(coll))
|
||||||
http.HandleFunc("/upload/", func(w http.ResponseWriter, r *http.Request) { uploadHandler(coll, w, r) })
|
http.HandleFunc("/upload/", uploadHandler(coll))
|
||||||
http.HandleFunc("/about/", aboutHandler)
|
http.HandleFunc("/about/", aboutHandler)
|
||||||
fileHandler("/img/")
|
fileHandler("/img/")
|
||||||
fileHandler("/cover/")
|
fileHandler("/cover/")
|
||||||
|
|
|
@ -42,7 +42,8 @@ func storeFile(r *http.Request) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func uploadHandler(coll *mgo.Collection, w http.ResponseWriter, r *http.Request) {
|
func uploadHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
status := ""
|
status := ""
|
||||||
if r.Method == "POST" {
|
if r.Method == "POST" {
|
||||||
err := storeFile(r)
|
err := storeFile(r)
|
||||||
|
@ -55,3 +56,4 @@ func uploadHandler(coll *mgo.Collection, w http.ResponseWriter, r *http.Request)
|
||||||
|
|
||||||
loadTemplate(w, "upload", status)
|
loadTemplate(w, "upload", status)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
Reference in a new issue