Skip to content

Commit

Permalink
Merge pull request NyaaPantsu#236 from SpamNeko/404wrapper
Browse files Browse the repository at this point in the history
Get custom 404 page working for all handlers
  • Loading branch information
PantsuDev committed May 10, 2017
2 parents 19abfdb + e312581 commit b88f5b9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
18 changes: 9 additions & 9 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ func init() {
Router = mux.NewRouter()

// Routes
http.Handle("/css/", http.StripPrefix("/css/", gzipCSSHandler))
http.Handle("/js/", http.StripPrefix("/js/", gzipJSHandler))
http.Handle("/img/", http.StripPrefix("/img/", imgHandler))
http.Handle("/css/", http.StripPrefix("/css/", wrapHandler(gzipCSSHandler)))
http.Handle("/js/", http.StripPrefix("/js/", wrapHandler(gzipJSHandler)))
http.Handle("/img/", http.StripPrefix("/img/", wrapHandler(imgHandler)))
Router.Handle("/", gzipHomeHandler).Name("home")
Router.Handle("/page/{page:[0-9]+}", gzipHomeHandler).Name("home_page")
Router.Handle("/page/{page:[0-9]+}", wrapHandler(gzipHomeHandler)).Name("home_page")
Router.Handle("/search", gzipSearchHandler).Name("search")
Router.Handle("/search/{page}", gzipSearchHandler).Name("search_page")
Router.Handle("/api", gzipAPIHandler).Methods("GET")
Router.Handle("/api/{page:[0-9]*}", gzipAPIHandler).Methods("GET")
Router.Handle("/api/view/{id}", gzipAPIViewHandler).Methods("GET")
Router.Handle("/api/{page:[0-9]*}", wrapHandler(gzipAPIHandler)).Methods("GET")
Router.Handle("/api/view/{id}", wrapHandler(gzipAPIViewHandler)).Methods("GET")
Router.Handle("/api/upload", gzipAPIUploadHandler).Methods("POST")
Router.Handle("/api/update", gzipAPIUpdateHandler).Methods("PUT")
Router.Handle("/faq", gzipFaqHandler).Name("faq")
Router.Handle("/feed", gzipRSSHandler).Name("feed")
Router.Handle("/view/{id}", gzipViewHandler).Methods("GET").Name("view_torrent")
Router.Handle("/view/{id}", wrapHandler(gzipViewHandler)).Methods("GET").Name("view_torrent")
Router.HandleFunc("/view/{id}", PostCommentHandler).Methods("POST").Name("post_comment")
Router.Handle("/upload", gzipUploadHandler).Name("upload")
Router.Handle("/user/register", gzipUserRegisterFormHandler).Name("user_register").Methods("GET")
Expand All @@ -79,9 +79,9 @@ func init() {
Router.Handle("/user/register", gzipUserRegisterPostHandler).Name("user_register").Methods("POST")
Router.Handle("/user/login", gzipUserLoginPostHandler).Name("user_login").Methods("POST")
Router.Handle("/user/logout", gzipUserLogoutHandler).Name("user_logout")
Router.Handle("/user/{id}/{username}", gzipUserProfileHandler).Name("user_profile").Methods("GET")
Router.Handle("/user/{id}/{username}", wrapHandler(gzipUserProfileHandler)).Name("user_profile").Methods("GET")
Router.Handle("/user/{id}/{username}/follow", gzipUserFollowHandler).Name("user_follow").Methods("GET")
Router.Handle("/user/{id}/{username}", gzipUserProfileFormHandler).Name("user_profile").Methods("POST")
Router.Handle("/user/{id}/{username}", wrapHandler(gzipUserProfileFormHandler)).Name("user_profile").Methods("POST")

Router.Handle("/mod/", gzipIndexModPanel).Name("mod_index")
Router.Handle("/mod/torrents", gzipTorrentsListPanel).Name("mod_tlist")
Expand Down
49 changes: 49 additions & 0 deletions router/wrapHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package router

import (
"net/http"
)

type wrappedResponseWriter struct {
Rw http.ResponseWriter
Ignore bool
}

func (wrw *wrappedResponseWriter) WriteHeader(status int) {
if status==404 {
wrw.Ignore=true
} else {
wrw.Rw.WriteHeader(status)
}
}

func (wrw *wrappedResponseWriter) Write(p []byte) (int, error) {
if wrw.Ignore {
return 0, nil
}
return wrw.Rw.Write(p)
}

func (wrw *wrappedResponseWriter) Header() http.Header {
return wrw.Rw.Header()
}


type wrappedHandler struct {
h http.Handler
}

func (wh *wrappedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
wrw := wrappedResponseWriter{w, false}
wh.h.ServeHTTP(&wrw, r)
if wrw.Ignore==true {
wrw.Rw.Header().Del("Content-Encoding")
wrw.Rw.Header().Del("Vary")
wrw.Rw.Header().Set("Content-Type", "text/html; charset=utf-8")
NotFoundHandler(wrw.Rw, r)
}
}

func wrapHandler(handler http.Handler) http.Handler {
return &wrappedHandler{handler}
}

0 comments on commit b88f5b9

Please sign in to comment.