-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.go
More file actions
226 lines (184 loc) · 5.22 KB
/
init.go
File metadata and controls
226 lines (184 loc) · 5.22 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package mongodb
import (
"context"
"log"
"time"
"github.com/sinfo/deck2/src/config"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var ctx context.Context
var db *mongo.Database
var (
//Events is an instance of a mongodb collection
Events *EventsType
//Companies is an instance of a mongodb collection
Companies *CompaniesType
//Speakers is an instance of a mongodb collection
Speakers *SpeakersType
//Teams is an instance of a mongodb collection
Teams *TeamsType
//Members is an instance of a mongodb collection
Members *MembersType
//Items is an instance of a mongodb collection
Items *ItemsType
//Categories is an instance for item categories
Categories *ItemCategoriesType
//Packages is an instance of a mongodb collection
Packages *PackagesType
//Meetings is an instance of a mongodb collection
Meetings *MeetingsType
//Contacts is an instance of a mongodb collection
Contacts *ContactsType
//Threads is an instance of a mongodb collection
Threads *ThreadsType
//Posts is an instance of a mongodb collection
Posts *PostsType
//FlightInfo is an instance of a mongodb collection
FlightInfo *FlightInfoType
//Sessions is an instance of a mongodb collection
Sessions *SessionsType
//Billings is an instance of a mongodb collection
Billings *BillingsType
//CompanyReps is an instance of a mongodb collection
CompanyReps *CompanyRepsType
//Notifications is an instance of a mongodb collection
Notifications *NotificationsType
//Templates is an instance of a mongodb collection
Templates *TemplateType
// CoordinationTeams is an instance of coordination teams collection
CoordinationTeams *CoordinationTeamsType
)
var (
indexUnique = true
)
const (
cacheCleanupPeriod = 1 * time.Hour
)
// CleanupCache : In case you decide to run many instances of this program at the same time (for example
// using a load balancer), the cached versions between these instances will not coincide.
// A workaround is to just clean up the cached content every X time.
func CleanupCache() {
ticker := time.NewTicker(cacheCleanupPeriod)
defer ticker.Stop()
for range ticker.C {
ResetCurrentPublicEvent()
ResetCurrentPublicCompanies()
ResetCurrentPublicMembers()
ResetCurrentPublicSpeakers()
ResetCurrentPublicSessions()
log.Println("Cleaned up cache")
}
}
// InitializeDatabase initializes the database connection
func InitializeDatabase() {
client, err := mongo.NewClient(options.Client().ApplyURI(config.DatabaseURI))
if err != nil {
log.Fatal(err)
return
}
ctx = context.Background()
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
return
}
// Check the connection
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
}
db = client.Database(config.DatabaseName)
Events = &EventsType{
Collection: db.Collection("events"),
}
Companies = &CompaniesType{
Collection: db.Collection("companies"),
}
Speakers = &SpeakersType{
Collection: db.Collection("speakers"),
}
Teams = &TeamsType{
Collection: db.Collection("teams"),
}
Members = &MembersType{
Collection: db.Collection("members"),
}
Items = &ItemsType{
Collection: db.Collection("items"),
}
Categories = &ItemCategoriesType{
Collection: db.Collection("itemCategories"),
}
Packages = &PackagesType{
Collection: db.Collection("packages"),
}
Meetings = &MeetingsType{
Collection: db.Collection("meetings"),
}
Contacts = &ContactsType{
Collection: db.Collection("contacts"),
}
Threads = &ThreadsType{
Collection: db.Collection("threads"),
}
Posts = &PostsType{
Collection: db.Collection("posts"),
}
FlightInfo = &FlightInfoType{
Collection: db.Collection("flightInfo"),
}
Sessions = &SessionsType{
Collection: db.Collection("sessions"),
}
Billings = &BillingsType{
Collection: db.Collection("billings"),
}
CompanyReps = &CompanyRepsType{
Collection: db.Collection("companyReps"),
}
Notifications = &NotificationsType{
Collection: db.Collection("notifications"),
}
Templates = &TemplateType{
Collection: db.Collection("templates"),
}
CoordinationTeams = &CoordinationTeamsType{
Collection: db.Collection("coordinationTeams"),
}
// Ensure index for categories
if err := Categories.EnsureIndexes(); err != nil {
log.Println("Warning: failed to ensure categories indexes:", err)
}
// Seed default categories if none exist yet
func() {
ctx := context.Background()
cnt, err := Categories.Collection.CountDocuments(ctx, bson.M{})
if err != nil {
log.Println("Could not count categories:", err)
return
}
if cnt == 0 {
defaultCats := []string{"Publicity", "Merchandise", "Stands", "Talk", "Sponsorship", "Service", "Other"}
docs := make([]interface{}, 0, len(defaultCats))
for _, cat := range defaultCats {
docs = append(docs, bson.M{"name": cat})
}
if len(docs) > 0 {
if _, err := Categories.Collection.InsertMany(ctx, docs); err != nil {
log.Println("Could not seed default categories:", err)
}
}
}
}()
_, err = Templates.Collection.Indexes().CreateOne(
context.Background(),
mongo.IndexModel{
Keys: bson.D{{Key: "name", Value: 1}},
Options: options.Index().SetUnique(true),
},
)
log.Println("Connected to the database successfully")
go CleanupCache()
}