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-09 20:54:50 +02:00
gzipAPIUpdateHandler := handlers . CompressHandler ( http . HandlerFunc ( ApiUpdateHandler ) )
2017-05-08 00:56:45 +02:00
gzipFaqHandler := handlers . CompressHandler ( http . HandlerFunc ( FaqHandler ) )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
gzipRSSHandler := handlers . CompressHandler ( http . HandlerFunc ( RSSHandler ) )
2017-05-08 00:56:45 +02:00
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-09 03:36:48 +02:00
gzipUserLogoutHandler := handlers . CompressHandler ( http . HandlerFunc ( UserLogoutHandler ) )
2017-05-08 13:37:56 +02:00
gzipUserProfileHandler := handlers . CompressHandler ( http . HandlerFunc ( UserProfileHandler ) )
2017-05-10 03:15:29 +02:00
gzipUserFollowHandler := handlers . CompressHandler ( http . HandlerFunc ( UserFollowHandler ) )
2017-05-09 17:47:06 +02:00
gzipUserProfileFormHandler := handlers . CompressHandler ( http . HandlerFunc ( UserProfileFormHandler ) )
2017-05-08 00:56:45 +02:00
2017-05-10 05:24:18 +02:00
gzipIndexModPanel := handlers . CompressHandler ( http . HandlerFunc ( IndexModPanel ) )
gzipTorrentsListPanel := handlers . CompressHandler ( http . HandlerFunc ( TorrentsListPanel ) )
gzipUsersListPanel := handlers . CompressHandler ( http . HandlerFunc ( UsersListPanel ) )
gzipCommentsListPanel := handlers . CompressHandler ( http . HandlerFunc ( CommentsListPanel ) )
gzipTorrentEditModPanel := handlers . CompressHandler ( http . HandlerFunc ( TorrentEditModPanel ) )
gzipTorrentPostEditModPanel := handlers . CompressHandler ( http . HandlerFunc ( TorrentPostEditModPanel ) )
gzipCommentDeleteModPanel := handlers . CompressHandler ( http . HandlerFunc ( CommentDeleteModPanel ) )
gzipTorrentDeleteModPanel := handlers . CompressHandler ( http . HandlerFunc ( TorrentDeleteModPanel ) )
2017-05-10 06:20:45 +02:00
gzipGetTorrentReportHandler := handlers . CompressHandler ( http . HandlerFunc ( GetTorrentReportHandler ) )
gzipTorrentReportCreateHandler := handlers . CompressHandler ( http . HandlerFunc ( CreateTorrentReportHandler ) )
gzipTorrentReportDeleteHandler := handlers . CompressHandler ( http . HandlerFunc ( DeleteTorrentReportHandler ) )
gzipTorrentDeleteHandler := handlers . CompressHandler ( http . HandlerFunc ( DeleteTorrentHandler ) )
2017-05-10 05:24:18 +02:00
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" )
2017-05-09 18:54:12 +02:00
Router . Handle ( "/api" , gzipAPIHandler ) . Methods ( "GET" )
2017-05-09 17:09:45 +02:00
Router . Handle ( "/api/{page:[0-9]*}" , gzipAPIHandler ) . Methods ( "GET" )
2017-05-08 00:56:45 +02:00
Router . Handle ( "/api/view/{id}" , gzipAPIViewHandler ) . Methods ( "GET" )
2017-05-09 18:54:12 +02:00
Router . Handle ( "/api/upload" , gzipAPIUploadHandler ) . Methods ( "POST" )
2017-05-09 20:54:50 +02:00
Router . Handle ( "/api/update" , gzipAPIUpdateHandler ) . Methods ( "PUT" )
2017-05-08 00:56:45 +02:00
Router . Handle ( "/faq" , gzipFaqHandler ) . Name ( "faq" )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
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-09 03:36:48 +02:00
Router . Handle ( "/user/logout" , gzipUserLogoutHandler ) . Name ( "user_logout" )
2017-05-08 13:37:56 +02:00
Router . Handle ( "/user/{id}/{username}" , gzipUserProfileHandler ) . Name ( "user_profile" ) . Methods ( "GET" )
2017-05-10 03:15:29 +02:00
Router . Handle ( "/user/{id}/{username}/follow" , gzipUserFollowHandler ) . Name ( "user_follow" ) . Methods ( "GET" )
2017-05-09 17:47:06 +02:00
Router . Handle ( "/user/{id}/{username}" , gzipUserProfileFormHandler ) . Name ( "user_profile" ) . Methods ( "POST" )
2017-05-10 05:24:18 +02:00
Router . Handle ( "/mod/" , gzipIndexModPanel ) . Name ( "mod_index" )
Router . Handle ( "/mod/torrents" , gzipTorrentsListPanel ) . Name ( "mod_tlist" )
Router . Handle ( "/mod/users" , gzipUsersListPanel ) . Name ( "mod_ulist" )
Router . Handle ( "/mod/comments" , gzipCommentsListPanel ) . Name ( "mod_clist" )
2017-05-10 08:20:17 +02:00
Router . Handle ( "/mod/comments" , gzipCommentsListPanel ) . Name ( "mod_cedit" ) // TODO
2017-05-10 05:24:18 +02:00
Router . Handle ( "/mod/torrent/" , gzipTorrentEditModPanel ) . Name ( "mod_tedit" )
Router . Handle ( "/mod/torrent/" , gzipTorrentPostEditModPanel ) . Name ( "mod_ptedit" )
2017-05-10 08:20:17 +02:00
Router . Handle ( "/mod/torrent/delete" , gzipTorrentDeleteModPanel ) . Name ( "mod_tdelete" )
Router . Handle ( "/mod/comment/delete" , gzipCommentDeleteModPanel ) . Name ( "mod_cdelete" )
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-10 06:20:45 +02:00
Router . Handle ( "/report/create" , gzipTorrentReportCreateHandler ) . Name ( "torrent_report_create" ) . Methods ( "POST" )
// TODO Allow only moderators to access /moderation/*
Router . Handle ( "/moderation/report/delete" , gzipTorrentReportDeleteHandler ) . Name ( "torrent_report_delete" ) . Methods ( "POST" )
Router . Handle ( "/moderation/torrent/delete" , gzipTorrentDeleteHandler ) . Name ( "torrent_delete" ) . Methods ( "POST" )
Router . Handle ( "/moderation/report" , gzipGetTorrentReportHandler ) . Name ( "torrent_report" ) . Methods ( "GET" )
2017-05-07 09:05:31 +02:00
Router . NotFoundHandler = http . HandlerFunc ( NotFoundHandler )
2017-05-06 00:37:59 +02:00
}