Pass the session to the 404 page

This commit is contained in:
Las Zenow 2013-06-01 20:51:21 +02:00
parent 3da8cae762
commit e72fd6e4d4
5 changed files with 33 additions and 30 deletions

View file

@ -15,7 +15,7 @@ type settingsData struct {
func settingsHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
if sess.User == "" {
notFound(w)
notFound(w, r)
return
}
if r.Method == "POST" {
@ -40,7 +40,7 @@ func settingsHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
func deleteHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
if sess.User == "" {
notFound(w)
notFound(w, r)
return
}
@ -81,13 +81,13 @@ func deleteHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
func editHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
idStr := mux.Vars(r)["id"]
if sess.User == "" || !bson.IsObjectIdHex(idStr) {
notFound(w)
notFound(w, r)
return
}
id := bson.ObjectIdHex(idStr)
books, _, err := db.GetBooks(bson.M{"_id": id})
if err != nil {
notFound(w)
notFound(w, r)
return
}
@ -110,7 +110,7 @@ func cleanEmptyStr(s []string) []string {
func saveHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
idStr := mux.Vars(r)["id"]
if sess.User == "" || !bson.IsObjectIdHex(idStr) {
notFound(w)
notFound(w, r)
return
}
@ -132,7 +132,7 @@ func saveHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
book["keywords"] = keywords(book)
err := db.UpdateBook(id, book)
if err != nil {
notFound(w)
notFound(w, r)
return
}
@ -161,7 +161,7 @@ type newData struct {
func newHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
if sess.User == "" {
notFound(w)
notFound(w, r)
return
}
@ -204,7 +204,7 @@ func newHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
func storeHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
if sess.User == "" {
notFound(w)
notFound(w, r)
return
}

View file

@ -24,13 +24,13 @@ import (
func coverHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
if !bson.IsObjectIdHex(vars["id"]) {
notFound(w)
notFound(w, r)
return
}
id := bson.ObjectIdHex(vars["id"])
books, _, err := db.GetBooks(bson.M{"_id": id})
if err != nil || len(books) == 0 {
notFound(w)
notFound(w, r)
return
}
book := books[0]
@ -38,7 +38,7 @@ func coverHandler(w http.ResponseWriter, r *http.Request) {
if !book.Active {
sess := GetSession(r)
if sess.User == "" {
notFound(w)
notFound(w, r)
return
}
}
@ -52,7 +52,7 @@ func coverHandler(w http.ResponseWriter, r *http.Request) {
}
if err != nil {
log.Println("Error while opening image:", err)
notFound(w)
notFound(w, r)
return
}
defer f.Close()

View file

@ -133,14 +133,14 @@ func readStartHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
id := mux.Vars(r)["id"]
e, _ := openReadEpub(w, r, sess)
if e == nil {
notFound(w)
notFound(w, r)
return
}
defer e.Close()
it, err := e.Spine()
if err != nil {
notFound(w)
notFound(w, r)
return
}
http.Redirect(w, r, "/read/"+id+"/"+it.Url(), http.StatusTemporaryRedirect)
@ -151,7 +151,7 @@ func readHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
file := mux.Vars(r)["file"]
e, book := openReadEpub(w, r, sess)
if e == nil {
notFound(w)
notFound(w, r)
return
}
defer e.Close()
@ -200,32 +200,32 @@ func contentHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
id := vars["id"]
file := vars["file"]
if file == "" || !bson.IsObjectIdHex(id) {
notFound(w)
notFound(w, r)
return
}
books, _, err := db.GetBooks(bson.M{"_id": bson.ObjectIdHex(id)})
if err != nil || len(books) == 0 {
notFound(w)
notFound(w, r)
return
}
book := books[0]
if !book.Active {
if sess.User == "" {
notFound(w)
notFound(w, r)
return
}
}
e, err := OpenBook(book.File)
if err != nil {
notFound(w)
notFound(w, r)
return
}
defer e.Close()
html, err := e.OpenFile(file)
if err != nil {
notFound(w)
notFound(w, r)
return
}
defer html.Close()

View file

@ -1,4 +1,4 @@
{{template "header.html" .}}
{{template "header.html" .S}}
<div class="row">
<div class="span10 offset1">

View file

@ -59,7 +59,7 @@ type bookData struct {
func bookHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
idStr := mux.Vars(r)["id"]
if !bson.IsObjectIdHex(idStr) {
notFound(w)
notFound(w, r)
return
}
@ -68,7 +68,7 @@ func bookHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
id := bson.ObjectIdHex(idStr)
books, _, err := db.GetBooks(bson.M{"_id": id})
if err != nil || len(books) == 0 {
notFound(w)
notFound(w, r)
return
}
data.Book = books[0]
@ -79,14 +79,14 @@ func bookHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
func downloadHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
idStr := mux.Vars(r)["id"]
if !bson.IsObjectIdHex(idStr) {
notFound(w)
notFound(w, r)
return
}
id := bson.ObjectIdHex(idStr)
books, _, err := db.GetBooks(bson.M{"_id": id})
if err != nil || len(books) == 0 {
notFound(w)
notFound(w, r)
return
}
book := books[0]
@ -94,7 +94,7 @@ func downloadHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
if !book.Active {
sess := GetSession(r)
if sess.User == "" {
notFound(w)
notFound(w, r)
return
}
}
@ -102,7 +102,7 @@ func downloadHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
fs := db.GetFS(FS_BOOKS)
f, err := fs.OpenId(book.File)
if err != nil {
notFound(w)
notFound(w, r)
return
}
defer f.Close()
@ -135,9 +135,12 @@ func indexHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
loadTemplate(w, "index", data)
}
func notFound(w http.ResponseWriter) {
func notFound(w http.ResponseWriter, r *http.Request) {
var data statusData
data.S = GetStatus(w, r)
w.WriteHeader(http.StatusNotFound)
loadTemplate(w, "404", nil)
loadTemplate(w, "404", data)
}
func main() {
@ -154,7 +157,7 @@ func main() {
func setUpRouter() {
r := mux.NewRouter()
var notFoundHandler http.HandlerFunc
notFoundHandler = GatherStats(func(w http.ResponseWriter, r *http.Request, sess *Session) { notFound(w) })
notFoundHandler = GatherStats(func(w http.ResponseWriter, r *http.Request, sess *Session) { notFound(w, r) })
r.NotFoundHandler = notFoundHandler
r.HandleFunc("/", GatherStats(indexHandler))