-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeRequest.go
109 lines (92 loc) · 2.17 KB
/
makeRequest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package pogifyapi
import (
"fmt"
"log"
"net/http"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
)
type request struct {
Session string `json:"session" binding:"required"`
Provider string `json:"provider" binding:"required"`
Token string `json:"token" binding:"required"`
Request string `json:"request" binding:"required"`
}
// var rx = /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/;
func (r *request) validateRequest() {
// r.Request
}
func (s *server) makeRequest(c *gin.Context) {
var r request
err := c.ShouldBindJSON(&r)
if err != nil {
c.Error(err)
c.String(400, fmt.Sprint(err))
return
}
// var for identifier
var id string
// validate token against provider
switch r.Provider {
case "twitch":
// validate token with twitch
// s.auth.getTwitchKeys()
token, err := s.auth.ValidateTwitchToken(r.Token)
if err != nil {
c.Error(err)
c.String(401, fmt.Sprint(err))
return
}
id = token.Claims.(jwt.MapClaims)["sub"].(string)
case "google":
// validate token with google
s.auth.getGooglePEM()
token, err := s.auth.ValidateGoogleToken(r.Token)
if err != nil {
c.Error(err)
c.String(401, fmt.Sprint(err))
return
}
id = token.Claims.(jwt.MapClaims)["sub"].(string)
default:
if _testing {
id = "test"
} else {
c.String(400, "invalid provider")
return
}
}
// eager increment rate limit
rateLimit, err := s.redis.rateLimitRequest(r.Session, id)
if err != nil {
c.AbortWithError(500, err)
return
}
if rateLimit[0] > 1 {
c.Header("retry-after", fmt.Sprint(rateLimit[1]))
c.Status(429)
return
}
// check active session
res, err := http.Get(fmt.Sprintf("%v/channels-stats?id=%v", s.pubsub.url, r.Session))
if err != nil {
go s.redis.reverseRateLimit(r.Session, id)
c.AbortWithError(500, err)
return
}
if res.StatusCode == 404 {
c.String(404, "inactive session")
return
}
tA := time.Now()
ch := make(chan *http.Response)
errCh := make(chan error)
go s.pubsub.pub(ch, errCh, "host_"+r.Session, []byte(r.Request))
select {
case <-ch:
case err = <-errCh:
c.AbortWithError(500, err)
}
log.Print(time.Now().Sub(tA))
}