diff --git a/admin.go b/admin.go index b06c88d..67e87a3 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 == "" { - http.NotFound(w, r) + notFound(w) 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 == "" { - http.NotFound(w, r) + notFound(w) return } @@ -80,13 +80,13 @@ func deleteHandler(w http.ResponseWriter, r *http.Request, sess *Session) { func editHandler(w http.ResponseWriter, r *http.Request, sess *Session) { if sess.User == "" { - http.NotFound(w, r) + notFound(w) return } id := bson.ObjectIdHex(mux.Vars(r)["id"]) books, _, err := db.GetBooks(bson.M{"_id": id}) if err != nil { - http.NotFound(w, r) + notFound(w) return } @@ -108,7 +108,7 @@ func cleanEmptyStr(s []string) []string { func saveHandler(w http.ResponseWriter, r *http.Request, sess *Session) { if sess.User == "" { - http.NotFound(w, r) + notFound(w) return } @@ -131,7 +131,7 @@ func saveHandler(w http.ResponseWriter, r *http.Request, sess *Session) { book["keywords"] = keywords(book) err := db.UpdateBook(id, book) if err != nil { - http.NotFound(w, r) + notFound(w) return } @@ -160,7 +160,7 @@ type newData struct { func newHandler(w http.ResponseWriter, r *http.Request, sess *Session) { if sess.User == "" { - http.NotFound(w, r) + notFound(w) return } @@ -203,7 +203,7 @@ func newHandler(w http.ResponseWriter, r *http.Request, sess *Session) { func storeHandler(w http.ResponseWriter, r *http.Request, sess *Session) { if sess.User == "" { - http.NotFound(w, r) + notFound(w) return } diff --git a/cover.go b/cover.go index 876f7a8..ff9f220 100644 --- a/cover.go +++ b/cover.go @@ -26,7 +26,7 @@ func coverHandler(w http.ResponseWriter, r *http.Request) { id := bson.ObjectIdHex(vars["id"]) books, _, err := db.GetBooks(bson.M{"_id": id}) if err != nil || len(books) == 0 { - http.NotFound(w, r) + notFound(w) return } book := books[0] @@ -34,7 +34,7 @@ func coverHandler(w http.ResponseWriter, r *http.Request) { if !book.Active { sess := GetSession(r) if sess.User == "" { - http.NotFound(w, r) + notFound(w) return } } @@ -48,7 +48,7 @@ func coverHandler(w http.ResponseWriter, r *http.Request) { } if err != nil { log.Println("Error while opening image:", err) - http.NotFound(w, r) + notFound(w) return } defer f.Close() diff --git a/reader.go b/reader.go index ed035e5..d030c3f 100644 --- a/reader.go +++ b/reader.go @@ -131,14 +131,14 @@ func readStartHandler(w http.ResponseWriter, r *http.Request, sess *Session) { id := mux.Vars(r)["id"] e, _ := openReadEpub(w, r, sess) if e == nil { - http.NotFound(w, r) + notFound(w) return } defer e.Close() it, err := e.Spine() if err != nil { - http.NotFound(w, r) + notFound(w) return } http.Redirect(w, r, "/read/"+id+"/"+it.Url(), http.StatusTemporaryRedirect) @@ -149,7 +149,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 { - http.NotFound(w, r) + notFound(w) return } defer e.Close() @@ -195,32 +195,32 @@ func contentHandler(w http.ResponseWriter, r *http.Request, sess *Session) { id := vars["id"] file := vars["file"] if file == "" { - http.NotFound(w, r) + notFound(w) return } books, _, err := db.GetBooks(bson.M{"_id": bson.ObjectIdHex(id)}) if err != nil || len(books) == 0 { - http.NotFound(w, r) + notFound(w) return } book := books[0] if !book.Active { if sess.User == "" { - http.NotFound(w, r) + notFound(w) return } } e, err := OpenBook(book.File) if err != nil { - http.NotFound(w, r) + notFound(w) return } defer e.Close() html, err := e.OpenFile(file) if err != nil { - http.NotFound(w, r) + notFound(w) return } defer html.Close() diff --git a/template.go b/template.go index d35184f..a5ca948 100644 --- a/template.go +++ b/template.go @@ -25,6 +25,7 @@ func GetStatus(w http.ResponseWriter, r *http.Request) Status { var templates = template.Must(template.ParseFiles(TEMPLATE_PATH+"header.html", TEMPLATE_PATH+"footer.html", + TEMPLATE_PATH+"404.html", TEMPLATE_PATH+"index.html", TEMPLATE_PATH+"about.html", TEMPLATE_PATH+"book.html", diff --git a/templates/404.html b/templates/404.html new file mode 100644 index 0000000..c5555cd --- /dev/null +++ b/templates/404.html @@ -0,0 +1,12 @@ +{{template "header.html" .}} + +
+
+

Page not found

+

+ The requested page don't exist. +

+
+
+ +{{template "footer.html"}} diff --git a/trantor.go b/trantor.go index 7e917cf..d106a20 100644 --- a/trantor.go +++ b/trantor.go @@ -53,7 +53,7 @@ func bookHandler(w http.ResponseWriter, r *http.Request, sess *Session) { 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) + notFound(w) return } db.IncVisit(id) @@ -65,7 +65,7 @@ func downloadHandler(w http.ResponseWriter, r *http.Request, sess *Session) { 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) + notFound(w) return } book := books[0] @@ -73,7 +73,7 @@ func downloadHandler(w http.ResponseWriter, r *http.Request, sess *Session) { if !book.Active { sess := GetSession(r) if sess.User == "" { - http.NotFound(w, r) + notFound(w) return } } @@ -81,7 +81,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 { - http.NotFound(w, r) + notFound(w) return } defer f.Close() @@ -115,6 +115,11 @@ func indexHandler(w http.ResponseWriter, r *http.Request, sess *Session) { loadTemplate(w, "index", data) } +func notFound(w http.ResponseWriter) { + w.WriteHeader(http.StatusNotFound) + loadTemplate(w, "404", nil) +} + func main() { db = initDB() defer db.Close() @@ -128,6 +133,10 @@ func main() { func setUpRouter() { r := mux.NewRouter() + var notFoundHandler http.HandlerFunc + notFoundHandler = GatherStats(func(w http.ResponseWriter, r *http.Request, sess *Session) { notFound(w) }) + r.NotFoundHandler = notFoundHandler + r.HandleFunc("/", GatherStats(indexHandler)) r.HandleFunc("/book/{id:[0-9a-fA-F]+}", GatherStats(bookHandler)) r.HandleFunc("/search/", GatherStats(searchHandler))