comparison api/handlers.go @ 12:aaf85ae1f942

add very simple html template
author Dennis C. M. <dennis@denniscm.com>
date Thu, 20 Mar 2025 11:12:21 +0000
parents 5c124578fed2
children
comparison
equal deleted inserted replaced
11:6d91c612310a 12:aaf85ae1f942
3 import ( 3 import (
4 "net/http" 4 "net/http"
5 5
6 "github.com/denniscmcom/pacobot/auth" 6 "github.com/denniscmcom/pacobot/auth"
7 "github.com/denniscmcom/pacobot/socket" 7 "github.com/denniscmcom/pacobot/socket"
8 "github.com/denniscmcom/pacobot/store"
8 "github.com/gin-gonic/gin" 9 "github.com/gin-gonic/gin"
9 ) 10 )
10 11
11 func GetUserHandler(c *gin.Context) { 12 func GetUser(c *gin.Context) {
12 userName := c.Query("username") 13 userName := c.Query("username")
13 user := auth.GetUser(userName, getAccessToken()) 14 user := auth.GetUser(userName, store.GetAccessToken())
14 15
15 c.JSON(http.StatusOK, gin.H{ 16 c.JSON(http.StatusOK, gin.H{
16 "message": user.Data[len(user.Data)-1].Id, 17 "message": user.Data[len(user.Data)-1].Id,
17 }) 18 })
18 } 19 }
19 20
20 func AuthHandler(c *gin.Context) { 21 func Auth(c *gin.Context) {
21 authUrl := auth.GetAuthUrl() 22 authUrl := auth.GetAuthUrl()
22 c.Redirect(http.StatusMovedPermanently, authUrl) 23 c.Redirect(http.StatusMovedPermanently, authUrl)
23 } 24 }
24 25
25 func AuthValidateHandler(c *gin.Context) { 26 func AuthValidate(c *gin.Context) {
26 msg := "failed" 27 msg := "failed"
27 28
28 if auth.IsAuthTokenValid(getAccessToken()) { 29 if auth.IsAuthTokenValid(store.GetAccessToken()) {
29 msg = "ok" 30 msg = "ok"
30 } 31 }
31 32
32 c.JSON(http.StatusOK, gin.H{ 33 c.JSON(http.StatusOK, gin.H{
33 "message": msg, 34 "message": msg,
34 }) 35 })
35 } 36 }
36 37
37 func AuthRefreshHandler(c *gin.Context) { 38 func AuthRefresh(c *gin.Context) {
38 authRes := auth.RefreshAuthToken(getAccessToken(), getRefreshToken()) 39 authRes := auth.RefreshAuthToken(store.GetAccessToken(), store.GetRefreshToken())
39 setAccessToken(authRes.AccessToken) 40 store.SetAccessToken(authRes.AccessToken)
40 setRefreshToken(authRes.RefreshToken) 41 store.SetRefreshToken(authRes.RefreshToken)
41 42
42 c.JSON(http.StatusOK, gin.H{ 43 c.JSON(http.StatusOK, gin.H{
43 "message": "ok", 44 "message": "ok",
44 }) 45 })
45 } 46 }
46 47
47 func AuthRevokeHandler(c *gin.Context) { 48 func AuthRevoke(c *gin.Context) {
48 auth.RevokeAuthToken(getAccessToken()) 49 auth.RevokeAuthToken(store.GetAccessToken())
49 50
50 c.JSON(http.StatusOK, gin.H{ 51 c.JSON(http.StatusOK, gin.H{
51 "message": "ok", 52 "message": "ok",
52 }) 53 })
53 } 54 }
54 55
55 func TwitchCallbackHandler(c *gin.Context) { 56 func Twitch(c *gin.Context) {
56 authCode := c.Query("code") 57 authCode := c.Query("code")
57 authRes := auth.GetAuthToken(authCode) 58 authRes := auth.GetAuthToken(authCode)
58 authStore.Store("accessToken", authRes.AccessToken) 59 store.SetAccessToken(authRes.AccessToken)
59 authStore.Store("refreshToken", authRes.RefreshToken) 60 store.SetRefreshToken(authRes.RefreshToken)
61 c.Redirect(http.StatusFound, "/")
62 }
63
64 func Connect(c *gin.Context) {
65 go socket.Connect(store.GetAccessToken())
60 66
61 c.JSON(http.StatusOK, gin.H{ 67 c.JSON(http.StatusOK, gin.H{
62 "message": "ok", 68 "message": "ok",
63 }) 69 })
64 } 70 }
65
66 func ConnectHandler(c *gin.Context) {
67 go socket.Connect(getAccessToken())
68
69 c.JSON(http.StatusOK, gin.H{
70 "message": "ok",
71 })
72 }