Add a 404 page
This commit is contained in:
parent
137357cd12
commit
d0f6b83423
6 changed files with 45 additions and 23 deletions
16
admin.go
16
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
|
||||
}
|
||||
|
||||
|
|
6
cover.go
6
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()
|
||||
|
|
16
reader.go
16
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()
|
||||
|
|
|
@ -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",
|
||||
|
|
12
templates/404.html
Normal file
12
templates/404.html
Normal file
|
@ -0,0 +1,12 @@
|
|||
{{template "header.html" .}}
|
||||
|
||||
<div class="row">
|
||||
<div class="span10 offset1">
|
||||
<h4>Page not found</h4>
|
||||
<p>
|
||||
The requested page don't exist.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{template "footer.html"}}
|
17
trantor.go
17
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))
|
||||
|
|
Reference in a new issue