-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromService.go
executable file
·137 lines (116 loc) · 3.15 KB
/
romService.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
package main
import (
"context"
"errors"
"fmt"
"github.com/MP281X/romLinks_backend/packages/api"
"github.com/MP281X/romLinks_backend/packages/db"
"github.com/MP281X/romLinks_backend/packages/logger"
textsearch "github.com/MP281X/romLinks_backend/packages/textSearch"
romhandler "github.com/MP281X/romLinks_backend/services/romService/romHandler"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
// initialize the logger
l, err := logger.InitLogger("romService")
if err != nil {
fmt.Println(err.Error())
return
}
l.Info("logger initialized")
// connect to mongodb
db, err := db.InitDB("romService")
if err != nil {
l.Error("db initialized")
return
}
l.Info("db initialized")
// set the index in mongodb
err = setDbIndex(db)
if err != nil {
l.Error("added index to the db")
return
}
l.Info("added index to the db")
// pass the logger and the db collection to the routes handler
r := &romhandler.DbLog{
L: l,
DbR: db.Collection("rom"),
DbV: db.Collection("version"),
DbC: db.Collection("comment"),
DbReq: db.Collection("request"),
RN: textsearch.TextList{T: []string{}},
}
// get the rom name list
err = r.GetRomName()
if err != nil {
l.Error("unable to get the rom name list")
return
}
l.Info("initialized the rom name list")
// init the api with the routes
api.InitApi(r.RomRoutes, ":9092", "romService", l)
}
// set the mongo db index
func setDbIndex(db *mongo.Database) error {
index1 := mongo.IndexModel{
Keys: bson.D{
{"romname", 1},
{"androidversion", 1},
},
Options: options.Index().SetUnique(true).SetName("unique rom"),
}
index2 := mongo.IndexModel{
Keys: bson.D{
{"codename", 1},
{"username", 1},
{"romid", 1},
{"msg", 1},
},
Options: options.Index().SetUnique(true).SetName("unique comment"),
}
index3 := mongo.IndexModel{
Keys: bson.D{
{"romid", 1},
{"codename", 1},
{"date", 1},
{"version", 1},
{"gappslink", 1},
{"vanillalink", 1},
},
Options: options.Index().SetUnique(true).SetName("unique version"),
}
index4 := mongo.IndexModel{
Keys: bson.D{
{"codename", 1},
{"androidversion", 1},
{"romname", 1},
},
Options: options.Index().SetUnique(true).SetName("unique request"),
}
// delete all the current index
db.Collection("rom").Indexes().DropAll(context.TODO())
db.Collection("comment").Indexes().DropAll(context.TODO())
db.Collection("version").Indexes().DropAll(context.TODO())
db.Collection("request").Indexes().DropAll(context.TODO())
// add the index to the db
_, err := db.Collection("rom").Indexes().CreateOne(context.TODO(), index1)
if err != nil {
return errors.New("index: " + err.Error())
}
_, err = db.Collection("comment").Indexes().CreateOne(context.TODO(), index2)
if err != nil {
return errors.New("index: " + err.Error())
}
_, err = db.Collection("version").Indexes().CreateOne(context.TODO(), index3)
if err != nil {
return errors.New("index: " + err.Error())
}
_, err = db.Collection("request").Indexes().CreateOne(context.TODO(), index4)
if err != nil {
return errors.New("index: " + err.Error())
}
return nil
}