-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
138 lines (118 loc) · 3.33 KB
/
main.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/garyburd/redigo/redis"
"github.com/gin-gonic/gin"
)
const (
downloadTypeGit string = "git"
downloadTypeCurl string = "curl"
)
func main() {
log.Println("Booting Badgeit API server")
log.Println("Initializing Redis Message Queue")
conn, err := redis.Dial("tcp", fmt.Sprintf("%s:%s", os.Getenv("REDIS_HOSTNAME"), os.Getenv("REDIS_PORT")))
if err != nil {
log.Fatalln("Failed to initialize redis message queue", err)
}
defer conn.Close()
log.Println("Initialized Redis Message Queue successfully")
log.Println("Initializing API Server")
initAPIServer(conn)
}
func initAPIServer(redisConn redis.Conn) {
r := gin.Default()
r.POST("/test/callback", func(c *gin.Context) {
io.Copy(os.Stdout, c.Request.Body)
defer c.Request.Body.Close()
c.JSON(http.StatusOK, gin.H{
"test": "ok",
})
return
})
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"app": "badgeit-api",
"version": "experimental",
})
return
})
r.GET("/badges", func(c *gin.Context) {
downloadType, hasDownloadType := c.GetQuery("download")
if !hasDownloadType || downloadType == "" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "download is a required",
})
return
}
if downloadType != downloadTypeGit && downloadType != downloadTypeCurl {
c.JSON(http.StatusBadRequest, gin.H{
"error": fmt.Sprintf("Allowed download types are %s, %s", downloadTypeGit, downloadTypeCurl),
})
return
}
remote, hasRemote := c.GetQuery("remote")
if !hasRemote || remote == "" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "remote is a required",
})
return
}
callback, hasCallback := c.GetQuery("callback")
if !hasCallback || callback == "" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "callback is a required",
})
return
}
// check for cached data
cachedData, _ := redis.String(redisConn.Do("GET", fmt.Sprintf("badge:%s", remote)))
payload := gin.H{
"download": downloadType,
"remote": remote,
"callback": callback,
"cache": cachedData,
}
// check if worker is already working on badge computation
alreadyProcessing, err := redis.Bool(redisConn.Do("SISMEMBER", "badgeit:processingRemotes", remote))
if err != nil {
log.Println("Unable to check if remote is already processing due to err", err)
}
if alreadyProcessing {
payload["status"] = "already processing"
c.JSON(http.StatusOK, payload)
return
}
alreadyQueued, err := redis.Bool(redisConn.Do("SISMEMBER", "badgeit:queuedRemotes", remote))
if err != nil {
log.Println("Unable to check if remote is already queued due to err", err)
}
if alreadyQueued {
payload["status"] = "already queued for processing"
c.JSON(http.StatusOK, payload)
return
}
// queue a task for the worker
jsonPayload, _ := json.Marshal(payload)
redisConn.Send("MULTI")
redisConn.Send("SADD", "badgeit:queuedRemotes", remote)
redisConn.Send("LPUSH", "badge:worker", []byte(jsonPayload))
_, err = redisConn.Do("EXEC")
if err != nil {
log.Println("Unable to queue request", err)
c.JSON(http.StatusServiceUnavailable, gin.H{
"error": "Unable to queue request",
})
return
}
payload["status"] = "successfully queued for processing"
c.JSON(http.StatusAccepted, payload)
return
})
r.Run(":8080")
}