-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (104 loc) · 2.73 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
139
140
package main
import(
"github.com/gin-gonic/gin"
"net/http"
"database/sql"
_ "github.com/lib/pq"
"log"
"time"
"context"
amqp "github.com/rabbitmq/amqp091-go"
)
func failOnError(err error, msg string) {
if err != nil {
log.Panicf("%s: %s", msg, err)
}
}
type voteRequest struct {
NAME string `json:"name"`
}
type VoteDb struct {
Name string
QtdVotes int
}
type VoteResponse struct {
Name string `json:"name"`
Votes float64 `json:"votes"`
}
func calculate(votedb []VoteDb) ([]VoteResponse) {
var total = 0
var result []VoteResponse
for _, vote := range votedb {
total += vote.QtdVotes
}
for _, vote := range votedb {
println(vote.Name)
println(vote.QtdVotes)
v := VoteResponse{Name: vote.Name, Votes: (float64(vote.QtdVotes)/float64(total))*100 }
result = append(result, v)
}
return result
}
func postVote(c *gin.Context) {
conn, err := amqp.Dial("amqp://guest:guest@172.17.0.2:5672")
failOnError(err, "Failed to connect to RabbitMQ")
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer ch.Close()
err = ch.ExchangeDeclare(
"votes", // name
"fanout", // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
nil, // arguments
)
failOnError(err, "Failed to declare an exchange")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var newVote voteRequest
if err := c.BindJSON(&newVote); err != nil {
return
}
body := newVote.NAME
err = ch.PublishWithContext(ctx,
"votes", // exchange
"", // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
DeliveryMode: amqp.Persistent,
ContentType: "text/plain",
Body: []byte(body),
})
failOnError(err, "Failed to publish a message")
log.Printf(" [x] Sent %s", body)
log.Print("Vote received: " + newVote.NAME)
c.IndentedJSON(http.StatusCreated, newVote)
}
func getVotes(c *gin.Context) {
connStr := "postgres://postgres:postgres@172.17.0.3/votedb?sslmode=disable"
db, err := sql.Open("postgres", connStr)
rows, err := db.Query("SELECT name, count(*) as qtdVotes FROM public.votes GROUP by name")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var votes []VoteDb
for rows.Next() {
var vdb VoteDb
if err := rows.Scan(&vdb.Name, &vdb.QtdVotes); err != nil {
log.Fatalf("error %v: %q", &vdb.Name, err)
}
votes = append(votes, vdb)
}
calculate(votes)
c.IndentedJSON(http.StatusCreated, calculate(votes))
}
func main() {
router := gin.Default()
router.POST("/api/votes", postVote)
router.GET("/api/votes", getVotes)
router.Run()
}