2017-05-05 16:39:15 +02:00
package router
import (
"net/http"
2017-05-07 10:25:09 +02:00
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/service/captcha"
2017-05-18 03:09:23 +02:00
"github.com/gorilla/handlers"
2017-05-07 10:25:09 +02:00
"github.com/gorilla/mux"
2017-06-05 03:33:48 +02:00
"github.com/justinas/nosurf"
2017-05-05 16:39:15 +02:00
)
2017-05-25 21:54:58 +02:00
// Router variable for exporting the route configuration
2017-05-05 16:39:15 +02:00
var Router * mux . Router
2017-06-05 03:33:48 +02:00
// CSRFRouter : CSRF protection for Router variable for exporting the route configuration
var CSRFRouter * nosurf . CSRFHandler
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-11 05:06:12 +02:00
// TODO Use config from cli
// TODO Make sure the directory exists
2017-05-24 09:11:13 +02:00
dumpsHandler := http . FileServer ( http . Dir ( DatabaseDumpPath ) )
2017-05-11 05:06:12 +02:00
// TODO Use config from cli
// TODO Make sure the directory exists
2017-05-11 18:30:38 +02:00
gpgKeyHandler := http . FileServer ( http . Dir ( GPGPublicKeyPath ) )
2017-06-14 05:53:54 +02:00
gzipHomeHandler := http . HandlerFunc ( SearchHandler )
2017-05-25 21:54:58 +02:00
gzipAPIHandler := http . HandlerFunc ( APIHandler )
gzipAPIViewHandler := http . HandlerFunc ( APIViewHandler )
2017-05-11 22:21:06 +02:00
gzipViewHandler := http . HandlerFunc ( ViewHandler )
gzipUserProfileHandler := http . HandlerFunc ( UserProfileHandler )
2017-05-25 21:54:58 +02:00
gzipUserAPIKeyResetHandler := http . HandlerFunc ( UserAPIKeyResetHandler )
2017-05-11 22:21:06 +02:00
gzipUserDetailsHandler := http . HandlerFunc ( UserDetailsHandler )
2017-05-28 01:29:46 +02:00
downloadTorrentHandler := http . HandlerFunc ( DownloadTorrent )
2017-05-11 22:21:06 +02:00
gzipUserProfileFormHandler := http . HandlerFunc ( UserProfileFormHandler )
2017-05-21 00:02:57 +02:00
gzipUserNotificationsHandler := http . HandlerFunc ( UserNotificationsHandler )
2017-05-11 05:06:12 +02:00
gzipDumpsHandler := handlers . CompressHandler ( dumpsHandler )
gzipGpgKeyHandler := handlers . CompressHandler ( gpgKeyHandler )
gzipDatabaseDumpHandler := handlers . CompressHandler ( http . HandlerFunc ( DatabaseDumpHandler ) )
2017-05-10 05:24:18 +02:00
2017-05-08 00:56:45 +02:00
Router = mux . NewRouter ( )
2017-05-11 22:21:06 +02:00
http . Handle ( "/css/" , http . StripPrefix ( "/css/" , cssHandler ) )
http . Handle ( "/js/" , http . StripPrefix ( "/js/" , jsHandler ) )
http . Handle ( "/img/" , http . StripPrefix ( "/img/" , imgHandler ) )
2017-05-11 05:06:12 +02:00
http . Handle ( "/dbdumps/" , http . StripPrefix ( "/dbdumps/" , wrapHandler ( gzipDumpsHandler ) ) )
http . Handle ( "/gpg/" , http . StripPrefix ( "/gpg/" , wrapHandler ( gzipGpgKeyHandler ) ) )
2017-05-30 00:18:43 +02:00
// We don't need CSRF here
2017-05-11 05:06:12 +02:00
Router . Handle ( "/" , gzipHomeHandler ) . Name ( "home" )
2017-05-10 00:17:25 +02:00
Router . Handle ( "/page/{page:[0-9]+}" , wrapHandler ( gzipHomeHandler ) ) . Name ( "home_page" )
2017-05-11 22:21:06 +02:00
Router . HandleFunc ( "/search" , SearchHandler ) . Name ( "search" )
Router . HandleFunc ( "/search/{page}" , SearchHandler ) . Name ( "search_page" )
2017-05-30 00:18:43 +02:00
Router . HandleFunc ( "/verify/email/{token}" , UserVerifyEmailHandler ) . Name ( "user_verify" ) . Methods ( "GET" )
2017-05-11 22:21:06 +02:00
Router . HandleFunc ( "/faq" , FaqHandler ) . Name ( "faq" )
2017-06-15 04:44:46 +02:00
Router . HandleFunc ( "/activities" , ActivityListHandler ) . Name ( "activity_list" )
2017-05-11 22:21:06 +02:00
Router . HandleFunc ( "/feed" , RSSHandler ) . Name ( "feed" )
2017-06-15 15:45:00 +02:00
Router . HandleFunc ( "/feed/{page:[0-9]+}" , RSSHandler ) . Name ( "feed_page" )
2017-06-25 02:45:56 +02:00
Router . HandleFunc ( "/feed/magnet" , RSSMagnetHandler ) . Name ( "feed_magnet" )
Router . HandleFunc ( "/feed/magnet/{page:[0-9]+}" , RSSMagnetHandler ) . Name ( "feed_magnet_page" )
2017-06-15 15:43:18 +02:00
Router . HandleFunc ( "/feed/torznab" , RSSTorznabHandler ) . Name ( "feed_torznab" )
2017-06-17 15:58:45 +02:00
Router . HandleFunc ( "/feed/torznab/api" , RSSTorznabHandler ) . Name ( "feed_torznab" )
2017-06-15 21:54:06 +02:00
Router . HandleFunc ( "/feed/torznab/{page:[0-9]+}" , RSSTorznabHandler ) . Name ( "feed_torznab_page" )
2017-06-15 15:43:18 +02:00
Router . HandleFunc ( "/feed/eztv" , RSSEztvHandler ) . Name ( "feed_eztv" )
2017-06-15 21:54:06 +02:00
Router . HandleFunc ( "/feed/eztv/{page:[0-9]+}" , RSSEztvHandler ) . Name ( "feed_eztv_page" )
2017-05-10 05:24:18 +02:00
2017-05-28 01:29:46 +02:00
// !!! This line need to have the same download location as the one define in config.TorrentStorageLink !!!
Router . Handle ( "/download/{hash}" , wrapHandler ( downloadTorrentHandler ) ) . Name ( "torrent_download" )
2017-05-30 00:18:43 +02:00
// For now, no CSRF protection here, as API is not usable for uploads
Router . HandleFunc ( "/upload" , UploadHandler ) . Name ( "upload" )
2017-05-30 00:22:23 +02:00
Router . HandleFunc ( "/user/login" , UserLoginPostHandler ) . Name ( "user_login" ) . Methods ( "POST" )
2017-05-30 00:18:43 +02:00
torrentViewRoutes := Router . PathPrefix ( "/view" ) . Subrouter ( )
torrentViewRoutes . Handle ( "/{id}" , wrapHandler ( gzipViewHandler ) ) . Methods ( "GET" ) . Name ( "view_torrent" )
torrentViewRoutes . HandleFunc ( "/{id}" , ViewHeadHandler ) . Methods ( "HEAD" )
torrentViewRoutes . HandleFunc ( "/{id}" , PostCommentHandler ) . Methods ( "POST" ) . Name ( "post_comment" )
torrentRoutes := Router . PathPrefix ( "/torrent" ) . Subrouter ( )
torrentRoutes . HandleFunc ( "/" , TorrentEditUserPanel ) . Methods ( "GET" ) . Name ( "user_torrent_edit" )
torrentRoutes . HandleFunc ( "/" , TorrentPostEditUserPanel ) . Methods ( "POST" ) . Name ( "user_torrent_edit" )
torrentRoutes . HandleFunc ( "/delete" , TorrentDeleteUserPanel ) . Methods ( "GET" ) . Name ( "user_torrent_delete" )
userRoutes := Router . PathPrefix ( "/user" ) . Subrouter ( )
userRoutes . HandleFunc ( "/register" , UserRegisterFormHandler ) . Name ( "user_register" ) . Methods ( "GET" )
userRoutes . HandleFunc ( "/login" , UserLoginFormHandler ) . Name ( "user_login" ) . Methods ( "GET" )
userRoutes . HandleFunc ( "/register" , UserRegisterPostHandler ) . Name ( "user_register" ) . Methods ( "POST" )
2017-06-05 03:33:16 +02:00
userRoutes . HandleFunc ( "/logout" , UserLogoutHandler ) . Name ( "user_logout" ) . Methods ( "POST" )
2017-05-30 00:18:43 +02:00
userRoutes . Handle ( "/{id}/{username}" , wrapHandler ( gzipUserProfileHandler ) ) . Name ( "user_profile" ) . Methods ( "GET" )
userRoutes . HandleFunc ( "/{id}/{username}/follow" , UserFollowHandler ) . Name ( "user_follow" ) . Methods ( "GET" )
userRoutes . Handle ( "/{id}/{username}/edit" , wrapHandler ( gzipUserDetailsHandler ) ) . Name ( "user_profile_details" ) . Methods ( "GET" )
userRoutes . Handle ( "/{id}/{username}/edit" , wrapHandler ( gzipUserProfileFormHandler ) ) . Name ( "user_profile_edit" ) . Methods ( "POST" )
userRoutes . Handle ( "/{id}/{username}/apireset" , wrapHandler ( gzipUserAPIKeyResetHandler ) ) . Name ( "user_profile_apireset" ) . Methods ( "GET" )
userRoutes . Handle ( "/notifications" , wrapHandler ( gzipUserNotificationsHandler ) ) . Name ( "user_notifications" )
userRoutes . HandleFunc ( "/{id}/{username}/feed" , RSSHandler ) . Name ( "feed_user" )
userRoutes . HandleFunc ( "/{id}/{username}/feed/{page}" , RSSHandler ) . Name ( "feed_user_page" )
// We don't need CSRF here
api := Router . PathPrefix ( "/api" ) . Subrouter ( )
api . Handle ( "" , wrapHandler ( gzipAPIHandler ) ) . Methods ( "GET" )
api . Handle ( "/" , wrapHandler ( gzipAPIHandler ) ) . Methods ( "GET" )
api . Handle ( "/{page:[0-9]*}" , wrapHandler ( gzipAPIHandler ) ) . Methods ( "GET" )
api . Handle ( "/view/{id}" , wrapHandler ( gzipAPIViewHandler ) ) . Methods ( "GET" )
api . HandleFunc ( "/view/{id}" , APIViewHeadHandler ) . Methods ( "HEAD" )
api . HandleFunc ( "/upload" , APIUploadHandler ) . Methods ( "POST" )
2017-06-13 08:01:57 +02:00
api . HandleFunc ( "/login" , APILoginHandler ) . Methods ( "POST" )
api . HandleFunc ( "/token/check" , APICheckTokenHandler ) . Methods ( "GET" )
api . HandleFunc ( "/token/refresh" , APIRefreshTokenHandler ) . Methods ( "GET" )
2017-05-30 00:18:43 +02:00
api . HandleFunc ( "/search" , APISearchHandler )
api . HandleFunc ( "/search/{page}" , APISearchHandler )
api . HandleFunc ( "/update" , APIUpdateHandler ) . Methods ( "PUT" )
2017-05-25 21:54:58 +02:00
// INFO Everything under /mod should be wrapped by wrapModHandler. This make
2017-05-20 01:10:16 +02:00
// sure the page is only accessible by moderators
2017-05-30 00:18:43 +02:00
// We don't need CSRF here
2017-05-20 01:10:16 +02:00
// TODO Find a native mux way to add a 'prehook' for route /mod
2017-05-25 21:54:58 +02:00
Router . HandleFunc ( "/mod" , wrapModHandler ( IndexModPanel ) ) . Name ( "mod_index" )
Router . HandleFunc ( "/mod/torrents" , wrapModHandler ( TorrentsListPanel ) ) . Name ( "mod_tlist" ) . Methods ( "GET" )
Router . HandleFunc ( "/mod/torrents/{page:[0-9]+}" , wrapModHandler ( TorrentsListPanel ) ) . Name ( "mod_tlist_page" ) . Methods ( "GET" )
Router . HandleFunc ( "/mod/torrents" , wrapModHandler ( TorrentsPostListPanel ) ) . Methods ( "POST" )
Router . HandleFunc ( "/mod/torrents/{page:[0-9]+}" , wrapModHandler ( TorrentsPostListPanel ) ) . Methods ( "POST" )
Router . HandleFunc ( "/mod/torrents/deleted" , wrapModHandler ( DeletedTorrentsModPanel ) ) . Name ( "mod_tlist_deleted" ) . Methods ( "GET" )
Router . HandleFunc ( "/mod/torrents/deleted/{page:[0-9]+}" , wrapModHandler ( DeletedTorrentsModPanel ) ) . Name ( "mod_tlist_deleted_page" ) . Methods ( "GET" )
Router . HandleFunc ( "/mod/torrents/deleted" , wrapModHandler ( DeletedTorrentsPostPanel ) ) . Name ( "mod_tlist_deleted" ) . Methods ( "POST" )
Router . HandleFunc ( "/mod/torrents/deleted/{page:[0-9]+}" , wrapModHandler ( DeletedTorrentsPostPanel ) ) . Name ( "mod_tlist_deleted_page" ) . Methods ( "POST" )
Router . HandleFunc ( "/mod/reports" , wrapModHandler ( TorrentReportListPanel ) ) . Name ( "mod_trlist" )
Router . HandleFunc ( "/mod/reports/{page}" , wrapModHandler ( TorrentReportListPanel ) ) . Name ( "mod_trlist_page" )
Router . HandleFunc ( "/mod/users" , wrapModHandler ( UsersListPanel ) ) . Name ( "mod_ulist" )
Router . HandleFunc ( "/mod/users/{page}" , wrapModHandler ( UsersListPanel ) ) . Name ( "mod_ulist_page" )
Router . HandleFunc ( "/mod/comments" , wrapModHandler ( CommentsListPanel ) ) . Name ( "mod_clist" )
Router . HandleFunc ( "/mod/comments/{page}" , wrapModHandler ( CommentsListPanel ) ) . Name ( "mod_clist_page" )
Router . HandleFunc ( "/mod/comment" , wrapModHandler ( CommentsListPanel ) ) . Name ( "mod_cedit" ) // TODO
Router . HandleFunc ( "/mod/torrent/" , wrapModHandler ( TorrentEditModPanel ) ) . Name ( "mod_tedit" ) . Methods ( "GET" )
Router . HandleFunc ( "/mod/torrent/" , wrapModHandler ( TorrentPostEditModPanel ) ) . Name ( "mod_ptedit" ) . Methods ( "POST" )
Router . HandleFunc ( "/mod/torrent/delete" , wrapModHandler ( TorrentDeleteModPanel ) ) . Name ( "mod_tdelete" )
Router . HandleFunc ( "/mod/torrent/block" , wrapModHandler ( TorrentBlockModPanel ) ) . Name ( "mod_tblock" )
Router . HandleFunc ( "/mod/report/delete" , wrapModHandler ( TorrentReportDeleteModPanel ) ) . Name ( "mod_trdelete" )
Router . HandleFunc ( "/mod/comment/delete" , wrapModHandler ( CommentDeleteModPanel ) ) . Name ( "mod_cdelete" )
Router . HandleFunc ( "/mod/reassign" , wrapModHandler ( TorrentReassignModPanel ) ) . Name ( "mod_treassign" ) . Methods ( "GET" )
Router . HandleFunc ( "/mod/reassign" , wrapModHandler ( TorrentPostReassignModPanel ) ) . Name ( "mod_treassign" ) . Methods ( "POST" )
2017-05-30 00:18:43 +02:00
apiMod := Router . PathPrefix ( "/mod/api" ) . Subrouter ( )
apiMod . HandleFunc ( "/torrents" , wrapModHandler ( APIMassMod ) ) . Name ( "mod_tapi" ) . Methods ( "POST" )
2017-05-10 05:24:18 +02:00
2017-05-10 10:17:21 +02:00
//reporting a torrent
2017-05-22 09:10:08 +02:00
Router . HandleFunc ( "/report/{id}" , ReportTorrentHandler ) . Methods ( "POST" ) . Name ( "torrent_report" )
2017-05-10 05:24:18 +02:00
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-11 05:06:12 +02:00
Router . Handle ( "/dumps" , gzipDatabaseDumpHandler ) . Name ( "dump" ) . Methods ( "GET" )
2017-05-27 19:08:47 +02:00
Router . HandleFunc ( "/settings" , SeePublicSettingsHandler ) . Methods ( "GET" ) . Name ( "see_languages" )
Router . HandleFunc ( "/settings" , ChangePublicSettingsHandler ) . Methods ( "POST" ) . Name ( "see_languages" )
2017-05-13 00:17:34 +02:00
2017-05-07 09:05:31 +02:00
Router . NotFoundHandler = http . HandlerFunc ( NotFoundHandler )
2017-06-05 03:33:48 +02:00
CSRFRouter = nosurf . New ( Router )
2017-06-13 08:01:57 +02:00
CSRFRouter . ExemptRegexp ( "/api(?:/.+)*" )
2017-06-05 03:33:48 +02:00
CSRFRouter . ExemptPath ( "/mod" )
CSRFRouter . ExemptPath ( "/upload" )
CSRFRouter . ExemptPath ( "/user/login" )
2017-05-06 00:37:59 +02:00
}