-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.go
107 lines (76 loc) · 2.36 KB
/
mongo.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
package main
import (
"context"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const database = "reminderbot"
const collection = "channels"
// ChannelRecord is a persistency channel records stored in the database
type ChannelRecord struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
ChannelID int64 `bson:"channel_id"`
}
func process(unit func(*mongo.Collection, *context.Context) error) {
url := "mongodb://" + mongoRouterHost + ":27017"
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(url))
if err != nil {
log.Printf("Could not establish a connection to %s.\n", url)
log.Fatal(err)
}
defer func() {
if err := client.Disconnect(ctx); err != nil {
log.Printf("Could not properly disconnect from %s.\n", url)
log.Fatal(err)
}
}()
databaseHandler := client.Database(database)
collectionHandler := databaseHandler.Collection(collection)
err = unit(collectionHandler, &ctx)
if err != nil {
log.Printf("Could not process the mongodb operation.\n")
log.Fatal(err)
}
}
func listChannels() *[]ChannelRecord {
var channels []ChannelRecord
log.Println("querying the database for known channels")
querier := func(collection *mongo.Collection, ctx *context.Context) error {
cursor, err := collection.Find(*ctx, bson.M{})
if err != nil {
return err
}
err = cursor.All(*ctx, &channels)
return err
}
process(querier)
log.Printf("known channels: %v\n", channels)
return &channels
}
func registerChannel(id int64) {
log.Printf("registering new channel %d", id)
inserter := func(collection *mongo.Collection, ctx *context.Context) error {
channelRecord := ChannelRecord{ChannelID: id}
_, err := collection.InsertOne(*ctx, channelRecord)
if err != nil {
log.Printf("registering returned an error: %s\n", err.Error())
log.Println("this probably means that channel is already known.")
}
return err
}
process(inserter)
}
func deregisterChannel(id int64) {
log.Printf("deregistering channel %d", id)
deleter := func(collection *mongo.Collection, ctx *context.Context) error {
_, err := collection.DeleteOne(*ctx, bson.M{"channel_id": id})
return err
}
process(deleter)
}