2016-04-03 17:59:27 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2016-06-16 10:45:10 +00:00
|
|
|
"fmt"
|
2016-04-03 17:59:27 +00:00
|
|
|
|
2016-10-02 17:47:31 +00:00
|
|
|
"github.com/DataDog/datadog-go/statsd"
|
2016-06-16 10:45:10 +00:00
|
|
|
"github.com/getsentry/raven-go"
|
2016-04-03 17:59:27 +00:00
|
|
|
"github.com/gin-gonic/contrib/gzip"
|
|
|
|
"github.com/gin-gonic/gin"
|
2016-08-15 11:37:03 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2016-08-15 17:59:46 +00:00
|
|
|
"github.com/serenize/snaker"
|
2016-11-19 18:53:55 +00:00
|
|
|
"gopkg.in/redis.v5"
|
2017-01-28 14:06:12 +00:00
|
|
|
"zxq.co/ripple/rippleapi/app/internals"
|
|
|
|
"zxq.co/ripple/rippleapi/app/peppy"
|
|
|
|
"zxq.co/ripple/rippleapi/app/v1"
|
|
|
|
"zxq.co/ripple/rippleapi/common"
|
2016-04-03 17:59:27 +00:00
|
|
|
)
|
|
|
|
|
2016-08-09 21:06:23 +00:00
|
|
|
var (
|
2016-10-02 17:47:31 +00:00
|
|
|
db *sqlx.DB
|
|
|
|
cf common.Conf
|
|
|
|
doggo *statsd.Client
|
2016-11-19 18:53:55 +00:00
|
|
|
red *redis.Client
|
2016-08-09 21:06:23 +00:00
|
|
|
)
|
2016-05-15 11:57:04 +00:00
|
|
|
|
2016-08-15 17:59:46 +00:00
|
|
|
var commonClusterfucks = map[string]string{
|
|
|
|
"RegisteredOn": "register_datetime",
|
|
|
|
"UsernameAKA": "username_aka",
|
|
|
|
}
|
|
|
|
|
2016-04-03 17:59:27 +00:00
|
|
|
// Start begins taking HTTP connections.
|
2016-08-15 11:37:03 +00:00
|
|
|
func Start(conf common.Conf, dbO *sqlx.DB) *gin.Engine {
|
2016-05-15 11:57:04 +00:00
|
|
|
db = dbO
|
2016-08-09 21:06:23 +00:00
|
|
|
cf = conf
|
2016-07-06 14:33:58 +00:00
|
|
|
|
2016-08-15 17:59:46 +00:00
|
|
|
db.MapperFunc(func(s string) string {
|
|
|
|
if x, ok := commonClusterfucks[s]; ok {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return snaker.CamelToSnake(s)
|
|
|
|
})
|
|
|
|
|
2016-07-06 14:33:58 +00:00
|
|
|
setUpLimiter()
|
|
|
|
|
2016-04-03 17:59:27 +00:00
|
|
|
r := gin.Default()
|
2016-06-16 10:45:10 +00:00
|
|
|
r.Use(gzip.Gzip(gzip.DefaultCompression))
|
|
|
|
|
2016-10-02 17:47:31 +00:00
|
|
|
// sentry
|
2016-06-16 10:45:10 +00:00
|
|
|
if conf.SentryDSN != "" {
|
|
|
|
ravenClient, err := raven.New(conf.SentryDSN)
|
2016-06-17 08:34:53 +00:00
|
|
|
ravenClient.SetRelease(common.Version)
|
2016-06-16 10:45:10 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
2016-06-16 11:49:35 +00:00
|
|
|
r.Use(Recovery(ravenClient, false))
|
2016-06-16 10:45:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-03 17:59:27 +00:00
|
|
|
|
2016-10-02 17:47:31 +00:00
|
|
|
// datadog
|
2016-10-02 19:58:43 +00:00
|
|
|
var err error
|
|
|
|
doggo, err = statsd.New("127.0.0.1:8125")
|
2016-10-02 17:47:31 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
doggo.Namespace = "api."
|
|
|
|
r.Use(func(c *gin.Context) {
|
|
|
|
doggo.Incr("requests", nil, 1)
|
|
|
|
})
|
|
|
|
|
2016-11-19 18:53:55 +00:00
|
|
|
// redis
|
|
|
|
red = redis.NewClient(&redis.Options{
|
|
|
|
Addr: conf.RedisAddr,
|
|
|
|
Password: conf.RedisPassword,
|
|
|
|
DB: conf.RedisDB,
|
|
|
|
})
|
|
|
|
|
2016-12-01 22:16:36 +00:00
|
|
|
// token updater
|
|
|
|
go tokenUpdater(db)
|
|
|
|
|
2016-04-03 17:59:27 +00:00
|
|
|
api := r.Group("/api")
|
|
|
|
{
|
2016-09-02 16:14:23 +00:00
|
|
|
p := api.Group("/")
|
|
|
|
{
|
|
|
|
p.GET("/get_user", PeppyMethod(peppy.GetUser))
|
|
|
|
p.GET("/get_match", PeppyMethod(peppy.GetMatch))
|
|
|
|
p.GET("/get_user_recent", PeppyMethod(peppy.GetUserRecent))
|
|
|
|
p.GET("/get_user_best", PeppyMethod(peppy.GetUserBest))
|
|
|
|
p.GET("/get_scores", PeppyMethod(peppy.GetScores))
|
|
|
|
p.GET("/get_beatmaps", PeppyMethod(peppy.GetBeatmap))
|
|
|
|
}
|
|
|
|
|
2016-04-03 17:59:27 +00:00
|
|
|
gv1 := api.Group("/v1")
|
|
|
|
{
|
2016-06-14 22:23:55 +00:00
|
|
|
gv1.POST("/tokens", Method(v1.TokenNewPOST))
|
2016-05-15 11:57:04 +00:00
|
|
|
gv1.POST("/tokens/new", Method(v1.TokenNewPOST))
|
2016-11-21 16:04:27 +00:00
|
|
|
gv1.POST("/tokens/self/delete", Method(v1.TokenSelfDeletePOST))
|
2016-04-05 20:22:13 +00:00
|
|
|
|
2016-07-06 12:22:43 +00:00
|
|
|
// Auth-free API endpoints (public data)
|
2016-05-15 11:57:04 +00:00
|
|
|
gv1.GET("/ping", Method(v1.PingGET))
|
|
|
|
gv1.GET("/surprise_me", Method(v1.SurpriseMeGET))
|
2016-06-13 20:40:01 +00:00
|
|
|
gv1.GET("/doc", Method(v1.DocGET))
|
|
|
|
gv1.GET("/doc/content", Method(v1.DocContentGET))
|
|
|
|
gv1.GET("/doc/rules", Method(v1.DocRulesGET))
|
2016-07-06 12:22:43 +00:00
|
|
|
gv1.GET("/users", Method(v1.UsersGET))
|
|
|
|
gv1.GET("/users/whatid", Method(v1.UserWhatsTheIDGET))
|
|
|
|
gv1.GET("/users/full", Method(v1.UserFullGET))
|
|
|
|
gv1.GET("/users/userpage", Method(v1.UserUserpageGET))
|
|
|
|
gv1.GET("/users/lookup", Method(v1.UserLookupGET))
|
|
|
|
gv1.GET("/users/scores/best", Method(v1.UserScoresBestGET))
|
|
|
|
gv1.GET("/users/scores/recent", Method(v1.UserScoresRecentGET))
|
|
|
|
gv1.GET("/badges", Method(v1.BadgesGET))
|
|
|
|
gv1.GET("/beatmaps", Method(v1.BeatmapGET))
|
|
|
|
gv1.GET("/leaderboard", Method(v1.LeaderboardGET))
|
|
|
|
gv1.GET("/tokens", Method(v1.TokenGET))
|
|
|
|
gv1.GET("/users/self", Method(v1.UserSelfGET))
|
|
|
|
gv1.GET("/tokens/self", Method(v1.TokenSelfGET))
|
2016-07-06 22:20:36 +00:00
|
|
|
gv1.GET("/blog/posts", Method(v1.BlogPostsGET))
|
2016-08-15 17:59:46 +00:00
|
|
|
gv1.GET("/scores", Method(v1.ScoresGET))
|
2016-09-24 17:45:07 +00:00
|
|
|
gv1.GET("/beatmaps/rank_requests/status", Method(v1.BeatmapRankRequestsStatusGET))
|
2016-04-07 09:20:35 +00:00
|
|
|
|
|
|
|
// ReadConfidential privilege required
|
2016-05-15 11:57:04 +00:00
|
|
|
gv1.GET("/friends", Method(v1.FriendsGET, common.PrivilegeReadConfidential))
|
|
|
|
gv1.GET("/friends/with", Method(v1.FriendsWithGET, common.PrivilegeReadConfidential))
|
2016-09-02 16:27:15 +00:00
|
|
|
gv1.GET("/users/self/donor_info", Method(v1.UsersSelfDonorInfoGET, common.PrivilegeReadConfidential))
|
2016-09-27 21:13:55 +00:00
|
|
|
gv1.GET("/users/self/favourite_mode", Method(v1.UsersSelfFavouriteModeGET, common.PrivilegeReadConfidential))
|
2016-11-06 21:51:21 +00:00
|
|
|
gv1.GET("/users/self/settings", Method(v1.UsersSelfSettingsGET, common.PrivilegeReadConfidential))
|
2016-04-07 17:32:48 +00:00
|
|
|
|
2016-04-08 17:05:54 +00:00
|
|
|
// Write privilege required
|
2016-11-16 17:03:47 +00:00
|
|
|
gv1.POST("/friends/add", Method(v1.FriendsAddPOST, common.PrivilegeWrite))
|
|
|
|
gv1.POST("/friends/del", Method(v1.FriendsDelPOST, common.PrivilegeWrite))
|
2016-11-06 21:51:21 +00:00
|
|
|
gv1.POST("/users/self/settings", Method(v1.UsersSelfSettingsPOST, common.PrivilegeWrite))
|
2016-11-07 17:34:53 +00:00
|
|
|
gv1.POST("/users/self/userpage", Method(v1.UserSelfUserpagePOST, common.PrivilegeWrite))
|
2016-11-19 18:53:55 +00:00
|
|
|
gv1.POST("/beatmaps/rank_requests", Method(v1.BeatmapRankRequestsSubmitPOST, common.PrivilegeWrite))
|
2016-04-08 17:05:54 +00:00
|
|
|
|
2016-04-27 18:03:06 +00:00
|
|
|
// Admin: beatmap
|
2016-05-15 11:57:04 +00:00
|
|
|
gv1.POST("/beatmaps/set_status", Method(v1.BeatmapSetStatusPOST, common.PrivilegeBeatmap))
|
2016-06-08 21:33:08 +00:00
|
|
|
gv1.GET("/beatmaps/ranked_frozen_full", Method(v1.BeatmapRankedFrozenFullGET, common.PrivilegeBeatmap))
|
2016-04-27 18:03:06 +00:00
|
|
|
|
2016-04-12 19:41:08 +00:00
|
|
|
// Admin: user managing
|
2016-05-15 11:57:04 +00:00
|
|
|
gv1.POST("/users/manage/set_allowed", Method(v1.UserManageSetAllowedPOST, common.PrivilegeManageUser))
|
2016-04-12 19:41:08 +00:00
|
|
|
|
2016-04-07 17:32:48 +00:00
|
|
|
// M E T A
|
|
|
|
// E T "wow thats so meta"
|
2016-04-09 18:31:50 +00:00
|
|
|
// T E -- the one who said "wow thats so meta"
|
2016-04-07 17:32:48 +00:00
|
|
|
// A T E M
|
2016-05-15 11:57:04 +00:00
|
|
|
gv1.GET("/meta/restart", Method(v1.MetaRestartGET, common.PrivilegeAPIMeta))
|
|
|
|
gv1.GET("/meta/kill", Method(v1.MetaKillGET, common.PrivilegeAPIMeta))
|
|
|
|
gv1.GET("/meta/up_since", Method(v1.MetaUpSinceGET, common.PrivilegeAPIMeta))
|
|
|
|
gv1.GET("/meta/update", Method(v1.MetaUpdateGET, common.PrivilegeAPIMeta))
|
2016-06-14 09:32:11 +00:00
|
|
|
|
|
|
|
// User Managing + meta
|
2016-11-21 16:04:27 +00:00
|
|
|
gv1.POST("/tokens/fix_privileges", Method(v1.TokenFixPrivilegesPOST,
|
2016-06-14 09:32:11 +00:00
|
|
|
common.PrivilegeManageUser, common.PrivilegeAPIMeta))
|
2016-09-06 17:15:37 +00:00
|
|
|
|
|
|
|
// in the new osu-web, the old endpoints are also in /v1 it seems. So /shrug
|
2016-09-06 18:11:46 +00:00
|
|
|
gv1.GET("/get_user", PeppyMethod(peppy.GetUser))
|
|
|
|
gv1.GET("/get_match", PeppyMethod(peppy.GetMatch))
|
|
|
|
gv1.GET("/get_user_recent", PeppyMethod(peppy.GetUserRecent))
|
|
|
|
gv1.GET("/get_user_best", PeppyMethod(peppy.GetUserBest))
|
|
|
|
gv1.GET("/get_scores", PeppyMethod(peppy.GetScores))
|
|
|
|
gv1.GET("/get_beatmaps", PeppyMethod(peppy.GetBeatmap))
|
2016-04-03 17:59:27 +00:00
|
|
|
}
|
2016-04-10 21:14:44 +00:00
|
|
|
|
|
|
|
api.GET("/status", internals.Status)
|
2016-04-03 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.NoRoute(v1.Handle404)
|
2016-04-07 17:32:48 +00:00
|
|
|
|
|
|
|
return r
|
2016-04-03 17:59:27 +00:00
|
|
|
}
|