forked from openshift-pipelines/vote-api
-
Notifications
You must be signed in to change notification settings - Fork 556
/
main.go
50 lines (43 loc) · 883 Bytes
/
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
package main
import (
"encoding/json"
"net/http"
"github.com/gin-gonic/gin"
)
var inMemoryStore = make(map[string]string)
var redirectURL = "http://0.0.0.0:9000"
func setupRouter() *gin.Engine {
r := gin.Default()
r.GET("/vote", func(c *gin.Context) {
payload := gin.H{}
voteA := 0
voteB := 0
for _, v := range inMemoryStore {
switch v {
case "a":
voteA++
case "b":
voteB++
}
}
payload["a"] = voteA
payload["b"] = voteB
c.JSON(http.StatusOK, payload)
})
r.POST("/vote", func(c *gin.Context) {
buf := make([]byte, 1024)
num, _ := c.Request.Body.Read(buf)
reqBody := buf[0:num]
temp := map[string]string{}
json.Unmarshal(reqBody, &temp)
c.JSON(http.StatusOK, reqBody)
voter_id := temp["voter_id"]
vote := temp["vote"]
inMemoryStore[voter_id] = vote
})
return r
}
func main() {
r := setupRouter()
r.Run(":9000")
}