6
|
1 package main
|
|
2
|
|
3 import (
|
9
|
4 "log"
|
6
|
5 "net/http"
|
|
6
|
9
|
7 "github.com/denniscmcom/pacobot/auth"
|
|
8 "github.com/denniscmcom/pacobot/bot"
|
|
9 "github.com/denniscmcom/pacobot/socket"
|
6
|
10 "github.com/gin-gonic/gin"
|
|
11 )
|
|
12
|
9
|
13 type PageData struct {
|
|
14 Title string
|
|
15 }
|
|
16
|
6
|
17 func main() {
|
|
18 gin.SetMode(gin.DebugMode)
|
|
19
|
9
|
20 r := gin.Default()
|
|
21 r.LoadHTMLGlob("./www/*.html")
|
|
22
|
|
23 var authRes auth.AuthRes
|
6
|
24
|
|
25 r.GET("/", func(c *gin.Context) {
|
9
|
26 c.HTML(http.StatusOK, "index.html", gin.H{
|
|
27 "Title": "Index",
|
6
|
28 })
|
|
29 })
|
|
30
|
9
|
31 r.GET("/user", func(c *gin.Context) {
|
|
32 userName := c.Query("username")
|
|
33 user := auth.GetUser(userName, authRes.AccessToken)
|
6
|
34
|
|
35 c.JSON(http.StatusOK, gin.H{
|
9
|
36 "message": user.Data[len(user.Data)-1].Id,
|
6
|
37 })
|
|
38 })
|
|
39
|
|
40 r.GET("/auth", func(c *gin.Context) {
|
9
|
41 authUrl := auth.GetAuthUrl()
|
6
|
42
|
|
43 c.Redirect(http.StatusMovedPermanently, authUrl)
|
|
44 })
|
|
45
|
|
46 r.GET("/auth-validate", func(c *gin.Context) {
|
|
47 msg := "failed"
|
|
48
|
9
|
49 if auth.IsAuthTokenValid(authRes.AccessToken) {
|
6
|
50 msg = "ok"
|
|
51 }
|
|
52
|
|
53 c.JSON(http.StatusOK, gin.H{
|
|
54 "message": msg,
|
|
55 })
|
|
56 })
|
|
57
|
|
58 r.GET("/auth-refresh", func(c *gin.Context) {
|
9
|
59 authRes = auth.RefreshAuthToken(authRes.AccessToken, authRes.RefreshToken)
|
6
|
60
|
|
61 c.JSON(http.StatusOK, gin.H{
|
|
62 "message": "ok",
|
|
63 })
|
|
64 })
|
|
65
|
|
66 r.GET("/auth-revoke", func(c *gin.Context) {
|
9
|
67 auth.RevokeAuthToken(authRes.AccessToken)
|
6
|
68
|
|
69 c.JSON(http.StatusOK, gin.H{
|
|
70 "message": "ok",
|
|
71 })
|
|
72 })
|
|
73
|
|
74 r.GET("/twitch-auth-code-callback", func(c *gin.Context) {
|
|
75 authCode := c.Query("code")
|
9
|
76 authRes = auth.GetAuthToken(authCode)
|
6
|
77
|
9
|
78 c.Redirect(http.StatusMovedPermanently, "/")
|
6
|
79 })
|
|
80
|
|
81 r.GET("/connect", func(c *gin.Context) {
|
9
|
82 go socket.Connect(authRes.AccessToken)
|
|
83
|
|
84 c.Redirect(http.StatusMovedPermanently, "/")
|
|
85 })
|
6
|
86
|
9
|
87 r.POST("/timer", func(c *gin.Context) {
|
|
88 form := c.Request.PostForm
|
|
89 log.Println(form)
|
|
90 timesec := form.Get("tiempo-oculto")
|
|
91 log.Println(timesec)
|
|
92 args := []string{"timer", timesec}
|
|
93 bot.HandleCmd(args)
|
6
|
94 })
|
|
95
|
|
96 r.Run()
|
|
97 }
|