ripple-api/app/start.go

165 lines
5.4 KiB
Go
Raw Permalink Normal View History

2016-04-03 17:59:27 +00:00
package app
import (
"fmt"
2016-04-03 17:59:27 +00:00
2016-10-02 17:47:31 +00:00
"github.com/DataDog/datadog-go/statsd"
fhr "github.com/buaazp/fasthttprouter"
"github.com/getsentry/raven-go"
"github.com/jmoiron/sqlx"
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"
2017-02-19 17:19:59 +00:00
"zxq.co/ripple/rippleapi/app/websockets"
2017-01-28 14:06:12 +00:00
"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-04-03 17:59:27 +00:00
// Start begins taking HTTP connections.
func Start(conf common.Conf, dbO *sqlx.DB) *fhr.Router {
db = dbO
2016-08-09 21:06:23 +00:00
cf = conf
2017-02-02 12:53:36 +00:00
rawRouter := fhr.New()
r := router{rawRouter}
// TODO: add back gzip
// TODO: add logging
// TODO: add sentry panic recovering
2016-10-02 17:47:31 +00:00
// sentry
if conf.SentryDSN != "" {
ravenClient, err := raven.New(conf.SentryDSN)
2016-06-17 08:34:53 +00:00
ravenClient.SetRelease(common.Version)
if err != nil {
fmt.Println(err)
} else {
// r.Use(Recovery(ravenClient, false))
common.RavenClient = ravenClient
}
}
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."
2016-11-19 18:53:55 +00:00
// redis
red = redis.NewClient(&redis.Options{
Addr: conf.RedisAddr,
Password: conf.RedisPassword,
DB: conf.RedisDB,
})
2017-04-11 21:18:02 +00:00
peppy.R = red
2016-11-19 18:53:55 +00:00
// token updater
go tokenUpdater(db)
2017-02-19 17:19:59 +00:00
// start websocket
websockets.Start(red, db)
// peppyapi
2016-04-03 17:59:27 +00:00
{
2017-02-02 12:53:36 +00:00
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)
}
2016-09-02 16:14:23 +00:00
// v1 API
{
2017-06-17 16:11:10 +00:00
// These require an user to pass the password in cleartext and are
// as such really insecure.
//r.POSTMethod("/api/v1/tokens", v1.TokenNewPOST)
//r.POSTMethod("/api/v1/tokens/new", v1.TokenNewPOST)
2017-02-02 12:53:36 +00:00
r.POSTMethod("/api/v1/tokens/self/delete", v1.TokenSelfDeletePOST)
// Auth-free API endpoints (public data)
2017-02-02 12:53:36 +00:00
r.Method("/api/v1/ping", v1.PingGET)
r.Method("/api/v1/surprise_me", v1.SurpriseMeGET)
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/badges/members", v1.BadgeMembersGET)
2017-02-02 12:53:36 +00:00
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
2017-02-02 12:53:36 +00:00
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
2017-02-02 12:53:36 +00:00
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
2017-02-02 12:53:36 +00:00
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
2017-02-02 12:53:36 +00:00
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
2017-02-02 12:53:36 +00:00
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
2017-02-02 12:53:36 +00:00
r.POSTMethod("/api/v1/tokens/fix_privileges", v1.TokenFixPrivilegesPOST,
common.PrivilegeManageUser, common.PrivilegeAPIMeta)
}
2017-02-19 17:19:59 +00:00
// Websocket API
{
r.PlainGET("/api/v1/ws", websockets.WebsocketV1Entry)
}
// in the new osu-web, the old endpoints are also in /v1 it seems. So /shrug
{
2017-02-02 12:53:36 +00:00
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)
2016-04-03 17:59:27 +00:00
}
r.GET("/api/status", internals.Status)
2017-02-02 12:53:36 +00:00
rawRouter.NotFound = v1.Handle404
2016-04-07 17:32:48 +00:00
2017-02-02 12:53:36 +00:00
return rawRouter
2016-04-03 17:59:27 +00:00
}