-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
170 lines (139 loc) · 3.92 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/Vauhtijuoksu/gonator/helpers"
"github.com/Vauhtijuoksu/gonator/poll"
"github.com/gorilla/websocket"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/rs/cors"
)
type DonationMessage struct {
OperationType string `bson:"operationType"`
Donation helpers.Donation `bson:"fullDocument"`
}
type UpdateWebsocket struct {
Donations []DonationMessage `json:"Donations"`
}
var (
collection *mongo.Collection
ctx context.Context
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
)
func main() {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://mongodb:27017"))
if err != nil {
panic(err)
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, nil)
ctx = context.Background()
if err != nil {
log.Println(err)
}
log.Println("Connected to MongoDB")
collection = client.Database("gonator").Collection("donations")
go poll.Poll(ctx, collection, "https://potti.mieli.fi/f/Donation/GetDonations/?collectionId=COL-16-2330&pageSize=50&startAt=0", 10)
mux := http.NewServeMux()
mux.HandleFunc("/", index)
mux.HandleFunc("/donations", donations)
mux.HandleFunc("/goal", goal)
mux.HandleFunc("/getDonations", getDonations)
mux.HandleFunc("/bar", bar)
handler := cors.Default().Handler(mux)
http.ListenAndServe(":8080", handler)
}
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/html/index.html")
}
func donations(w http.ResponseWriter, r *http.Request) {
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(err)
}
// Get all donations and send them first.
cur, err := collection.Find(ctx, bson.D{{}}, options.Find())
if err != nil {
log.Println(err)
}
var updateWebsocket UpdateWebsocket
for cur.Next(ctx) {
var donation helpers.Donation
var donationMessage DonationMessage
err := cur.Decode(&donation)
if err != nil {
log.Println(err)
}
donationMessage.Donation = donation
donationMessage.OperationType = "firstInsert"
updateWebsocket.Donations = append(updateWebsocket.Donations, donationMessage)
}
if err := conn.WriteJSON(updateWebsocket); err != nil {
log.Println(err)
}
// Watch if there are changes at mongodb, if there are, send them.
cs, err := collection.Watch(ctx, mongo.Pipeline{}, options.ChangeStream().SetFullDocument(options.UpdateLookup))
if err != nil {
log.Println(err)
}
for cs.Next(ctx) {
var updateWebsocket UpdateWebsocket
var donationMessage DonationMessage
err := cs.Decode(&donationMessage)
if err != nil {
log.Println(err)
}
updateWebsocket.Donations = append(updateWebsocket.Donations, donationMessage)
if err := conn.WriteJSON(updateWebsocket); err != nil {
log.Println(err)
}
}
}
func goal(w http.ResponseWriter, r *http.Request) {
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
}
var goal int
for {
fetchgoal := helpers.GetGoal("https://vauhtijuoksu.fi/api/goal")
if fetchgoal != goal {
goal = fetchgoal
if err := conn.WriteJSON(goal); err != nil {
log.Println(err)
}
}
time.Sleep(60 * time.Second)
}
}
func getDonations(w http.ResponseWriter, r *http.Request) {
cur, err := collection.Find(ctx, bson.D{{}}, options.Find())
if err != nil {
log.Println(err)
}
var donations []helpers.Donation
if err = cur.All(ctx, &donations); err != nil {
log.Println(err)
}
e, err := json.Marshal(donations)
if err != nil {
log.Println(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(e)
}
func bar(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/html/bar.html")
}