Skip to content

Commit

Permalink
Wrapper for non-gzipped handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
SpamNeko committed May 9, 2017
1 parent b48e19b commit 4fe20bb
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,52 @@ import (
"github.com/ewhal/nyaa/service/captcha"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"fmt"
//"strings"
)

type wrappedResponseWriter struct {

Rw http.ResponseWriter
ignore bool

}

func (wrw *wrappedResponseWriter) WriteHeader(status int) {
if status==404 {
wrw.ignore=true
fmt.Println("Status = 404")
} else {
wrw.Rw.WriteHeader(status)
fmt.Println("Normal header")
}
}

func (wrw *wrappedResponseWriter) Write(p []byte) (int, error) {
if wrw.ignore {
//fmt.Println("Write returning nothing")
return 0, nil
}
//fmt.Println("Write returning normal")
return wrw.Rw.Write(p)
}

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

func wrapHandler(handler http.HandlerFunc) http.HandlerFunc {

return func (w http.ResponseWriter, r *http.Request) {
wrw := wrappedResponseWriter{w, false}
handler(&wrw, r)
if wrw.ignore==true {
fmt.Println("Going to notfound handler")
NotFoundHandler(wrw.Rw, r)
}
}
}

var Router *mux.Router

func init() {
Expand All @@ -27,7 +71,9 @@ func init() {
gzipAPIUpdateHandler := handlers.CompressHandler(http.HandlerFunc(ApiUpdateHandler))
gzipFaqHandler := handlers.CompressHandler(http.HandlerFunc(FaqHandler))
gzipRssHandler := handlers.CompressHandler(http.HandlerFunc(RssHandler))
gzipViewHandler := handlers.CompressHandler(http.HandlerFunc(ViewHandler))

gzipViewHandler := handlers.CompressHandler(http.HandlerFunc(wrapHandler(ViewHandler)))

gzipUploadHandler := handlers.CompressHandler(http.HandlerFunc(UploadHandler))
gzipUserRegisterFormHandler := handlers.CompressHandler(http.HandlerFunc(UserRegisterFormHandler))
gzipUserLoginFormHandler := handlers.CompressHandler(http.HandlerFunc(UserLoginFormHandler))
Expand Down

0 comments on commit 4fe20bb

Please sign in to comment.