diff --git a/admin.go b/admin.go index a5558ef..139ccf9 100644 --- a/admin.go +++ b/admin.go @@ -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 } diff --git a/cover.go b/cover.go index dba08f3..626f65f 100644 --- a/cover.go +++ b/cover.go @@ -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() diff --git a/reader.go b/reader.go index da5e89c..8b11a2b 100644 --- a/reader.go +++ b/reader.go @@ -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() diff --git a/templates/404.html b/templates/404.html index c5555cd..45c4838 100644 --- a/templates/404.html +++ b/templates/404.html @@ -1,4 +1,4 @@ -{{template "header.html" .}} +{{template "header.html" .S}}
diff --git a/trantor.go b/trantor.go index 569720c..ad2621d 100644 --- a/trantor.go +++ b/trantor.go @@ -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))