Merge pull request #170 from Leodmanx2/compression
Add compression wrapper to handlers
Cette révision appartient à :
révision
f8f77f58c7
1 fichiers modifiés avec 38 ajouts et 22 suppressions
|
@ -4,38 +4,54 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/ewhal/nyaa/service/captcha"
|
"github.com/ewhal/nyaa/service/captcha"
|
||||||
|
"github.com/gorilla/handlers"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Router *mux.Router
|
var Router *mux.Router
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Router = mux.NewRouter()
|
// Static file handlers
|
||||||
|
|
||||||
cssHandler := http.FileServer(http.Dir("./public/css/"))
|
cssHandler := http.FileServer(http.Dir("./public/css/"))
|
||||||
jsHandler := http.FileServer(http.Dir("./public/js/"))
|
jsHandler := http.FileServer(http.Dir("./public/js/"))
|
||||||
imgHandler := http.FileServer(http.Dir("./public/img/"))
|
imgHandler := http.FileServer(http.Dir("./public/img/"))
|
||||||
http.Handle("/css/", http.StripPrefix("/css/", cssHandler))
|
|
||||||
http.Handle("/js/", http.StripPrefix("/js/", jsHandler))
|
|
||||||
http.Handle("/img/", http.StripPrefix("/img/", imgHandler))
|
|
||||||
|
|
||||||
// Routes,
|
// Enable GZIP compression for all handlers except imgHandler and captcha
|
||||||
Router.HandleFunc("/", HomeHandler).Name("home")
|
gzipCSSHandler := handlers.CompressHandler(cssHandler)
|
||||||
Router.HandleFunc("/page/{page:[0-9]+}", HomeHandler).Name("home_page")
|
gzipJSHandler := handlers.CompressHandler(jsHandler)
|
||||||
Router.HandleFunc("/search", SearchHandler).Name("search")
|
gzipHomeHandler := handlers.CompressHandler(http.HandlerFunc(HomeHandler))
|
||||||
Router.HandleFunc("/search/{page}", SearchHandler).Name("search_page")
|
gzipSearchHandler := handlers.CompressHandler(http.HandlerFunc(SearchHandler))
|
||||||
Router.HandleFunc("/api/{page}", ApiHandler).Methods("GET")
|
gzipAPIHandler := handlers.CompressHandler(http.HandlerFunc(ApiHandler))
|
||||||
Router.HandleFunc("/api/view/{id}", ApiViewHandler).Methods("GET")
|
gzipAPIViewHandler := handlers.CompressHandler(http.HandlerFunc(ApiViewHandler))
|
||||||
Router.HandleFunc("/faq", FaqHandler).Name("faq")
|
gzipFaqHandler := handlers.CompressHandler(http.HandlerFunc(FaqHandler))
|
||||||
Router.HandleFunc("/feed", RssHandler).Name("feed")
|
gzipRssHandler := handlers.CompressHandler(http.HandlerFunc(RssHandler))
|
||||||
Router.HandleFunc("/view/{id}", ViewHandler).Name("view_torrent")
|
gzipViewHandler := handlers.CompressHandler(http.HandlerFunc(ViewHandler))
|
||||||
Router.HandleFunc("/upload", UploadHandler).Name("upload")
|
gzipUploadHandler := handlers.CompressHandler(http.HandlerFunc(UploadHandler))
|
||||||
Router.HandleFunc("/user/register", UserRegisterFormHandler).Name("user_register").Methods("GET")
|
gzipUserRegisterFormHandler := handlers.CompressHandler(http.HandlerFunc(UserRegisterFormHandler))
|
||||||
Router.HandleFunc("/user/login", UserLoginFormHandler).Name("user_login").Methods("GET")
|
gzipUserLoginFormHandler := handlers.CompressHandler(http.HandlerFunc(UserLoginFormHandler))
|
||||||
Router.HandleFunc("/verify/email/{token}", UserVerifyEmailHandler).Name("user_verify").Methods("GET")
|
gzipUserVerifyEmailHandler := handlers.CompressHandler(http.HandlerFunc(UserVerifyEmailHandler))
|
||||||
Router.HandleFunc("/user/register", UserRegisterPostHandler).Name("user_register_post").Methods("POST")
|
gzipUserRegisterPostHandler := handlers.CompressHandler(http.HandlerFunc(UserRegisterPostHandler))
|
||||||
Router.HandleFunc("/user/login", UserLoginPostHandler).Name("user_login_post").Methods("POST")
|
|
||||||
Router.HandleFunc("/user/{id}", UserProfileHandler).Name("user_profile")
|
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))
|
||||||
|
Router.Handle("/", gzipHomeHandler).Name("home")
|
||||||
|
Router.Handle("/page/{page:[0-9]+}", gzipHomeHandler).Name("home_page")
|
||||||
|
Router.Handle("/search", gzipSearchHandler).Name("search")
|
||||||
|
Router.Handle("/search/{page}", gzipSearchHandler).Name("search_page")
|
||||||
|
Router.Handle("/api/{page}", gzipAPIHandler).Methods("GET")
|
||||||
|
Router.Handle("/api/view/{id}", gzipAPIViewHandler).Methods("GET")
|
||||||
|
Router.Handle("/faq", gzipFaqHandler).Name("faq")
|
||||||
|
Router.Handle("/feed", gzipRssHandler).Name("feed")
|
||||||
|
Router.Handle("/view/{id}", gzipViewHandler).Name("view_torrent")
|
||||||
|
Router.Handle("/upload", gzipUploadHandler).Name("upload")
|
||||||
|
Router.Handle("/user/register", gzipUserRegisterFormHandler).Name("user_register").Methods("GET")
|
||||||
|
Router.Handle("/user/login", gzipUserLoginFormHandler).Name("user_login").Methods("GET")
|
||||||
|
Router.Handle("/verify/email/{token}", gzipUserVerifyEmailHandler).Name("user_verify").Methods("GET")
|
||||||
|
Router.Handle("/user/register", gzipUserRegisterPostHandler).Name("user_register").Methods("POST")
|
||||||
Router.PathPrefix("/captcha").Methods("GET").HandlerFunc(captcha.ServeFiles)
|
Router.PathPrefix("/captcha").Methods("GET").HandlerFunc(captcha.ServeFiles)
|
||||||
|
|
||||||
Router.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
|
Router.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
|
||||||
|
|
Référencer dans un nouveau ticket