From 68a980894254ee8cc528842eb41ec68ffa6c7579 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Thu, 2 Feb 2017 13:53:36 +0100 Subject: [PATCH] Wrap fasthttprouter inside own router --- app/router.go | 25 +++++++++++ app/start.go | 117 +++++++++++++++++++++++++------------------------- 2 files changed, 84 insertions(+), 58 deletions(-) create mode 100644 app/router.go diff --git a/app/router.go b/app/router.go new file mode 100644 index 0000000..809dd8b --- /dev/null +++ b/app/router.go @@ -0,0 +1,25 @@ +package app + +import ( + "github.com/buaazp/fasthttprouter" + "github.com/jmoiron/sqlx" + "github.com/valyala/fasthttp" + "zxq.co/ripple/rippleapi/common" +) + +type router struct { + r *fasthttprouter.Router +} + +func (r router) Method(path string, f func(md common.MethodData) common.CodeMessager, privilegesNeeded ...int) { + r.r.GET(path, Method(f, privilegesNeeded...)) +} +func (r router) POSTMethod(path string, f func(md common.MethodData) common.CodeMessager, privilegesNeeded ...int) { + r.r.POST(path, Method(f, privilegesNeeded...)) +} +func (r router) Peppy(path string, a func(c *fasthttp.RequestCtx, db *sqlx.DB)) { + r.r.GET(path, PeppyMethod(a)) +} +func (r router) GET(path string, handle fasthttp.RequestHandler) { + r.r.GET(path, handle) +} diff --git a/app/start.go b/app/start.go index 8818ddd..6068167 100644 --- a/app/start.go +++ b/app/start.go @@ -39,7 +39,8 @@ func Start(conf common.Conf, dbO *sqlx.DB) *fhr.Router { return snaker.CamelToSnake(s) }) - r := fhr.New() + rawRouter := fhr.New() + r := router{rawRouter} // TODO: add back gzip // TODO: add logging // TODO: add sentry panic recovering @@ -79,91 +80,91 @@ func Start(conf common.Conf, dbO *sqlx.DB) *fhr.Router { // peppyapi { - r.GET("/api/get_user", PeppyMethod(peppy.GetUser)) - r.GET("/api/get_match", PeppyMethod(peppy.GetMatch)) - r.GET("/api/get_user_recent", PeppyMethod(peppy.GetUserRecent)) - r.GET("/api/get_user_best", PeppyMethod(peppy.GetUserBest)) - r.GET("/api/get_scores", PeppyMethod(peppy.GetScores)) - r.GET("/api/get_beatmaps", PeppyMethod(peppy.GetBeatmap)) + r.Peppy("/api/get_user", peppy.GetUser) + r.Peppy("/api/get_match", peppy.GetMatch) + r.Peppy("/api/get_user_recent", peppy.GetUserRecent) + r.Peppy("/api/get_user_best", peppy.GetUserBest) + r.Peppy("/api/get_scores", peppy.GetScores) + r.Peppy("/api/get_beatmaps", peppy.GetBeatmap) } // v1 API { - r.POST("/api/v1/tokens", Method(v1.TokenNewPOST)) - r.POST("/api/v1/tokens/new", Method(v1.TokenNewPOST)) - r.POST("/api/v1/tokens/self/delete", Method(v1.TokenSelfDeletePOST)) + r.POSTMethod("/api/v1/tokens", v1.TokenNewPOST) + r.POSTMethod("/api/v1/tokens/new", v1.TokenNewPOST) + r.POSTMethod("/api/v1/tokens/self/delete", v1.TokenSelfDeletePOST) // Auth-free API endpoints (public data) - r.GET("/api/v1/ping", Method(v1.PingGET)) - r.GET("/api/v1/surprise_me", Method(v1.SurpriseMeGET)) - r.GET("/api/v1/doc", Method(v1.DocGET)) - r.GET("/api/v1/doc/content", Method(v1.DocContentGET)) - r.GET("/api/v1/doc/rules", Method(v1.DocRulesGET)) - r.GET("/api/v1/users", Method(v1.UsersGET)) - r.GET("/api/v1/users/whatid", Method(v1.UserWhatsTheIDGET)) - r.GET("/api/v1/users/full", Method(v1.UserFullGET)) - r.GET("/api/v1/users/userpage", Method(v1.UserUserpageGET)) - r.GET("/api/v1/users/lookup", Method(v1.UserLookupGET)) - r.GET("/api/v1/users/scores/best", Method(v1.UserScoresBestGET)) - r.GET("/api/v1/users/scores/recent", Method(v1.UserScoresRecentGET)) - r.GET("/api/v1/badges", Method(v1.BadgesGET)) - r.GET("/api/v1/beatmaps", Method(v1.BeatmapGET)) - r.GET("/api/v1/leaderboard", Method(v1.LeaderboardGET)) - r.GET("/api/v1/tokens", Method(v1.TokenGET)) - r.GET("/api/v1/users/self", Method(v1.UserSelfGET)) - r.GET("/api/v1/tokens/self", Method(v1.TokenSelfGET)) - r.GET("/api/v1/blog/posts", Method(v1.BlogPostsGET)) - r.GET("/api/v1/scores", Method(v1.ScoresGET)) - r.GET("/api/v1/beatmaps/rank_requests/status", Method(v1.BeatmapRankRequestsStatusGET)) + r.Method("/api/v1/ping", v1.PingGET) + r.Method("/api/v1/surprise_me", v1.SurpriseMeGET) + r.Method("/api/v1/doc", v1.DocGET) + r.Method("/api/v1/doc/content", v1.DocContentGET) + r.Method("/api/v1/doc/rules", v1.DocRulesGET) + r.Method("/api/v1/users", v1.UsersGET) + r.Method("/api/v1/users/whatid", v1.UserWhatsTheIDGET) + r.Method("/api/v1/users/full", v1.UserFullGET) + r.Method("/api/v1/users/userpage", v1.UserUserpageGET) + r.Method("/api/v1/users/lookup", v1.UserLookupGET) + r.Method("/api/v1/users/scores/best", v1.UserScoresBestGET) + r.Method("/api/v1/users/scores/recent", v1.UserScoresRecentGET) + r.Method("/api/v1/badges", v1.BadgesGET) + r.Method("/api/v1/beatmaps", v1.BeatmapGET) + r.Method("/api/v1/leaderboard", v1.LeaderboardGET) + r.Method("/api/v1/tokens", v1.TokenGET) + r.Method("/api/v1/users/self", v1.UserSelfGET) + r.Method("/api/v1/tokens/self", v1.TokenSelfGET) + r.Method("/api/v1/blog/posts", v1.BlogPostsGET) + r.Method("/api/v1/scores", v1.ScoresGET) + r.Method("/api/v1/beatmaps/rank_requests/status", v1.BeatmapRankRequestsStatusGET) // ReadConfidential privilege required - r.GET("/api/v1/friends", Method(v1.FriendsGET, common.PrivilegeReadConfidential)) - r.GET("/api/v1/friends/with", Method(v1.FriendsWithGET, common.PrivilegeReadConfidential)) - r.GET("/api/v1/users/self/donor_info", Method(v1.UsersSelfDonorInfoGET, common.PrivilegeReadConfidential)) - r.GET("/api/v1/users/self/favourite_mode", Method(v1.UsersSelfFavouriteModeGET, common.PrivilegeReadConfidential)) - r.GET("/api/v1/users/self/settings", Method(v1.UsersSelfSettingsGET, common.PrivilegeReadConfidential)) + r.Method("/api/v1/friends", v1.FriendsGET, common.PrivilegeReadConfidential) + r.Method("/api/v1/friends/with", v1.FriendsWithGET, common.PrivilegeReadConfidential) + r.Method("/api/v1/users/self/donor_info", v1.UsersSelfDonorInfoGET, common.PrivilegeReadConfidential) + r.Method("/api/v1/users/self/favourite_mode", v1.UsersSelfFavouriteModeGET, common.PrivilegeReadConfidential) + r.Method("/api/v1/users/self/settings", v1.UsersSelfSettingsGET, common.PrivilegeReadConfidential) // Write privilege required - r.POST("/api/v1/friends/add", Method(v1.FriendsAddPOST, common.PrivilegeWrite)) - r.POST("/api/v1/friends/del", Method(v1.FriendsDelPOST, common.PrivilegeWrite)) - r.POST("/api/v1/users/self/settings", Method(v1.UsersSelfSettingsPOST, common.PrivilegeWrite)) - r.POST("/api/v1/users/self/userpage", Method(v1.UserSelfUserpagePOST, common.PrivilegeWrite)) - r.POST("/api/v1/beatmaps/rank_requests", Method(v1.BeatmapRankRequestsSubmitPOST, common.PrivilegeWrite)) + r.POSTMethod("/api/v1/friends/add", v1.FriendsAddPOST, common.PrivilegeWrite) + r.POSTMethod("/api/v1/friends/del", v1.FriendsDelPOST, common.PrivilegeWrite) + r.POSTMethod("/api/v1/users/self/settings", v1.UsersSelfSettingsPOST, common.PrivilegeWrite) + r.POSTMethod("/api/v1/users/self/userpage", v1.UserSelfUserpagePOST, common.PrivilegeWrite) + r.POSTMethod("/api/v1/beatmaps/rank_requests", v1.BeatmapRankRequestsSubmitPOST, common.PrivilegeWrite) // Admin: beatmap - r.POST("/api/v1/beatmaps/set_status", Method(v1.BeatmapSetStatusPOST, common.PrivilegeBeatmap)) - r.GET("/api/v1/beatmaps/ranked_frozen_full", Method(v1.BeatmapRankedFrozenFullGET, common.PrivilegeBeatmap)) + r.POSTMethod("/api/v1/beatmaps/set_status", v1.BeatmapSetStatusPOST, common.PrivilegeBeatmap) + r.Method("/api/v1/beatmaps/ranked_frozen_full", v1.BeatmapRankedFrozenFullGET, common.PrivilegeBeatmap) // Admin: user managing - r.POST("/api/v1/users/manage/set_allowed", Method(v1.UserManageSetAllowedPOST, common.PrivilegeManageUser)) + r.POSTMethod("/api/v1/users/manage/set_allowed", v1.UserManageSetAllowedPOST, common.PrivilegeManageUser) // M E T A // E T "wow thats so meta" // T E -- the one who said "wow thats so meta" // A T E M - r.GET("/api/v1/meta/restart", Method(v1.MetaRestartGET, common.PrivilegeAPIMeta)) - r.GET("/api/v1/meta/kill", Method(v1.MetaKillGET, common.PrivilegeAPIMeta)) - r.GET("/api/v1/meta/up_since", Method(v1.MetaUpSinceGET, common.PrivilegeAPIMeta)) - r.GET("/api/v1/meta/update", Method(v1.MetaUpdateGET, common.PrivilegeAPIMeta)) + r.Method("/api/v1/meta/restart", v1.MetaRestartGET, common.PrivilegeAPIMeta) + r.Method("/api/v1/meta/kill", v1.MetaKillGET, common.PrivilegeAPIMeta) + r.Method("/api/v1/meta/up_since", v1.MetaUpSinceGET, common.PrivilegeAPIMeta) + r.Method("/api/v1/meta/update", v1.MetaUpdateGET, common.PrivilegeAPIMeta) // User Managing + meta - r.POST("/api/v1/tokens/fix_privileges", Method(v1.TokenFixPrivilegesPOST, - common.PrivilegeManageUser, common.PrivilegeAPIMeta)) + r.POSTMethod("/api/v1/tokens/fix_privileges", v1.TokenFixPrivilegesPOST, + common.PrivilegeManageUser, common.PrivilegeAPIMeta) } // in the new osu-web, the old endpoints are also in /v1 it seems. So /shrug { - r.GET("/api/v1/get_user", PeppyMethod(peppy.GetUser)) - r.GET("/api/v1/get_match", PeppyMethod(peppy.GetMatch)) - r.GET("/api/v1/get_user_recent", PeppyMethod(peppy.GetUserRecent)) - r.GET("/api/v1/get_user_best", PeppyMethod(peppy.GetUserBest)) - r.GET("/api/v1/get_scores", PeppyMethod(peppy.GetScores)) - r.GET("/api/v1/get_beatmaps", PeppyMethod(peppy.GetBeatmap)) + r.Peppy("/api/v1/get_user", peppy.GetUser) + r.Peppy("/api/v1/get_match", peppy.GetMatch) + r.Peppy("/api/v1/get_user_recent", peppy.GetUserRecent) + r.Peppy("/api/v1/get_user_best", peppy.GetUserBest) + r.Peppy("/api/v1/get_scores", peppy.GetScores) + r.Peppy("/api/v1/get_beatmaps", peppy.GetBeatmap) } r.GET("/api/status", internals.Status) - r.NotFound = v1.Handle404 + rawRouter.NotFound = v1.Handle404 - return r + return rawRouter }