2017-05-05 16:39:15 +02:00
package router
import (
"net/http"
2017-05-07 10:25:09 +02:00
"github.com/ewhal/nyaa/service/captcha"
2017-05-08 00:56:45 +02:00
"github.com/gorilla/handlers"
2017-05-07 10:25:09 +02:00
"github.com/gorilla/mux"
2017-05-05 16:39:15 +02:00
)
var Router * mux . Router
2017-05-06 00:37:59 +02:00
func init ( ) {
2017-05-08 00:56:45 +02:00
// Static file handlers
2017-05-07 00:26:02 +02:00
cssHandler := http . FileServer ( http . Dir ( "./public/css/" ) )
jsHandler := http . FileServer ( http . Dir ( "./public/js/" ) )
imgHandler := http . FileServer ( http . Dir ( "./public/img/" ) )
2017-05-05 16:39:15 +02:00
2017-05-08 00:56:45 +02:00
// Enable GZIP compression for all handlers except imgHandler and captcha
gzipCSSHandler := handlers . CompressHandler ( cssHandler )
gzipJSHandler := handlers . CompressHandler ( jsHandler )
gzipHomeHandler := handlers . CompressHandler ( http . HandlerFunc ( HomeHandler ) )
gzipSearchHandler := handlers . CompressHandler ( http . HandlerFunc ( SearchHandler ) )
gzipAPIHandler := handlers . CompressHandler ( http . HandlerFunc ( ApiHandler ) )
gzipAPIViewHandler := handlers . CompressHandler ( http . HandlerFunc ( ApiViewHandler ) )
2017-05-08 11:18:49 +02:00
gzipAPIUploadHandler := handlers . CompressHandler ( http . HandlerFunc ( ApiUploadHandler ) )
2017-05-08 00:56:45 +02:00
gzipFaqHandler := handlers . CompressHandler ( http . HandlerFunc ( FaqHandler ) )
gzipRssHandler := handlers . CompressHandler ( http . HandlerFunc ( RssHandler ) )
gzipViewHandler := handlers . CompressHandler ( http . HandlerFunc ( ViewHandler ) )
gzipUploadHandler := handlers . CompressHandler ( http . HandlerFunc ( UploadHandler ) )
gzipUserRegisterFormHandler := handlers . CompressHandler ( http . HandlerFunc ( UserRegisterFormHandler ) )
gzipUserLoginFormHandler := handlers . CompressHandler ( http . HandlerFunc ( UserLoginFormHandler ) )
gzipUserVerifyEmailHandler := handlers . CompressHandler ( http . HandlerFunc ( UserVerifyEmailHandler ) )
gzipUserRegisterPostHandler := handlers . CompressHandler ( http . HandlerFunc ( UserRegisterPostHandler ) )
2017-05-08 13:29:17 +02:00
gzipUserLoginPostHandler := handlers . CompressHandler ( http . HandlerFunc ( UserLoginPostHandler ) )
2017-05-08 00:56:45 +02:00
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" )
2017-05-08 11:18:49 +02:00
Router . Handle ( "/api/upload" , gzipAPIUploadHandler ) . Methods ( "POST" )
2017-05-08 00:56:45 +02:00
Router . Handle ( "/api/view/{id}" , gzipAPIViewHandler ) . Methods ( "GET" )
Router . Handle ( "/faq" , gzipFaqHandler ) . Name ( "faq" )
Router . Handle ( "/feed" , gzipRssHandler ) . Name ( "feed" )
2017-05-08 05:34:12 +02:00
Router . Handle ( "/view/{id}" , gzipViewHandler ) . Methods ( "GET" ) . Name ( "view_torrent" )
Router . HandleFunc ( "/view/{id}" , PostCommentHandler ) . Methods ( "POST" ) . Name ( "post_comment" )
2017-05-08 00:56:45 +02:00
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" )
2017-05-08 13:29:17 +02:00
Router . Handle ( "/user/login" , gzipUserLoginPostHandler ) . Name ( "user_login" ) . Methods ( "POST" )
2017-05-07 10:25:09 +02:00
Router . PathPrefix ( "/captcha" ) . Methods ( "GET" ) . HandlerFunc ( captcha . ServeFiles )
2017-05-07 08:59:45 +02:00
2017-05-07 09:05:31 +02:00
Router . NotFoundHandler = http . HandlerFunc ( NotFoundHandler )
2017-05-06 00:37:59 +02:00
}