From 78282f61deeb224f2d60e61069e2dd0e752f8a72 Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Sun, 9 Dec 2018 11:08:05 -0800 Subject: [PATCH] Add user info API endpoint --- pkg/handler/handler.go | 127 +++++++++++++++++++++++------------------ 1 file changed, 70 insertions(+), 57 deletions(-) diff --git a/pkg/handler/handler.go b/pkg/handler/handler.go index 393abde..9a6d30f 100644 --- a/pkg/handler/handler.go +++ b/pkg/handler/handler.go @@ -9,7 +9,7 @@ import ( "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" - patreon "github.com/mxpv/patreon-go" + "github.com/mxpv/patreon-go" itunes "github.com/mxpv/podcast" "golang.org/x/oauth2" @@ -44,6 +44,63 @@ type handler struct { patreon patreonService } +func New(feed feedService, support patreonService, cfg *config.AppConfig) http.Handler { + r := gin.New() + r.Use(gin.Recovery()) + + store := sessions.NewCookieStore([]byte(cfg.CookieSecret)) + r.Use(sessions.Sessions("podsync", store)) + + // Static files + HTML + + log.Printf("using assets path: %s", cfg.AssetsPath) + if cfg.AssetsPath != "" { + r.Static("/assets", cfg.AssetsPath) + } + + log.Printf("using templates path: %s", cfg.TemplatesPath) + if cfg.TemplatesPath != "" { + r.LoadHTMLGlob(path.Join(cfg.TemplatesPath, "*.html")) + } + + h := handler{ + feed: feed, + patreon: support, + cfg: cfg, + } + + // OAuth 2 configuration + + h.oauth2 = oauth2.Config{ + ClientID: cfg.PatreonClientId, + ClientSecret: cfg.PatreonSecret, + RedirectURL: cfg.PatreonRedirectURL, + Scopes: []string{"users", "pledges-to-me", "my-campaign"}, + Endpoint: oauth2.Endpoint{ + AuthURL: patreon.AuthorizationURL, + TokenURL: patreon.AccessTokenURL, + }, + } + + // Handlers + + r.GET("/", h.index) + r.GET("/login", h.login) + r.GET("/logout", h.logout) + r.GET("/patreon", h.patreonCallback) + r.GET("/robots.txt", h.robots) + + r.GET("/api/ping", h.ping) + r.GET("/api/user", h.user) + r.POST("/api/create", h.create) + r.GET("/api/metadata/:hashId", h.metadata) + r.POST("/api/webhooks", h.webhook) + + r.NoRoute(h.getFeed) + + return r +} + func (h handler) index(c *gin.Context) { identity, err := session.GetIdentity(c) if err != nil { @@ -121,6 +178,18 @@ func (h handler) ping(c *gin.Context) { c.String(http.StatusOK, "ok") } +func (h handler) user(c *gin.Context) { + identity, err := session.GetIdentity(c) + if err != nil { + identity = &api.Identity{} + } + + c.JSON(http.StatusOK, gin.H{ + "user_id": identity.UserId, + "feature_level": identity.FeatureLevel, + }) +} + func (h handler) create(c *gin.Context) { req := &api.CreateFeedRequest{} @@ -258,62 +327,6 @@ func (h handler) webhook(c *gin.Context) { log.Infof("sucessfully processed patreon event %s (%s)", pledge.Data.ID, eventName) } -func New(feed feedService, support patreonService, cfg *config.AppConfig) http.Handler { - r := gin.New() - r.Use(gin.Recovery()) - - store := sessions.NewCookieStore([]byte(cfg.CookieSecret)) - r.Use(sessions.Sessions("podsync", store)) - - // Static files + HTML - - log.Printf("using assets path: %s", cfg.AssetsPath) - if cfg.AssetsPath != "" { - r.Static("/assets", cfg.AssetsPath) - } - - log.Printf("using templates path: %s", cfg.TemplatesPath) - if cfg.TemplatesPath != "" { - r.LoadHTMLGlob(path.Join(cfg.TemplatesPath, "*.html")) - } - - h := handler{ - feed: feed, - patreon: support, - cfg: cfg, - } - - // OAuth 2 configuration - - h.oauth2 = oauth2.Config{ - ClientID: cfg.PatreonClientId, - ClientSecret: cfg.PatreonSecret, - RedirectURL: cfg.PatreonRedirectURL, - Scopes: []string{"users", "pledges-to-me", "my-campaign"}, - Endpoint: oauth2.Endpoint{ - AuthURL: patreon.AuthorizationURL, - TokenURL: patreon.AccessTokenURL, - }, - } - - // Handlers - - r.GET("/", h.index) - r.GET("/login", h.login) - r.GET("/logout", h.logout) - r.GET("/patreon", h.patreonCallback) - r.GET("/robots.txt", h.robots) - - r.GET("/api/ping", h.ping) - r.POST("/api/create", h.create) - r.GET("/api/metadata/:hashId", h.metadata) - r.POST("/api/webhooks", h.webhook) - - r.NoRoute(h.getFeed) - - return r -} - func badRequest(err error) (int, interface{}) { return http.StatusBadRequest, gin.H{"error": err.Error()} }