-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
64 lines (58 loc) · 1.41 KB
/
state.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
package state
import (
"context"
dapr "github.com/dapr/go-sdk/client"
"github.com/spf13/viper"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
const MONGO_URI = "MONGO_URI"
func init() {
viper.SetDefault(MONGO_URI, "mongodb://localhost:27017")
viper.AutomaticEnv()
}
type Handler struct {
Client dapr.Client
DB mongo.Collection
}
func New(ctx context.Context, c dapr.Client) *Handler {
client, err := mongo.Connect(ctx, options.Client().
ApplyURI(
viper.GetString(MONGO_URI),
).
SetAuth(
options.Credential{Username: viper.GetString("MONGO_USERNAME"), Password: viper.GetString("MONGO_PASSWORD")},
),
)
if err != nil {
panic(err)
}
err = client.Ping(ctx, readpref.Primary())
if err != nil {
panic(err)
}
collection := client.Database("db").Collection("state")
_, err = collection.Indexes().DropAll(ctx)
if err != nil {
panic(err)
}
_, err = collection.Indexes().CreateMany(ctx, []mongo.IndexModel{{
Keys: bson.D{{"timestampid", -1}},
Options: options.Index().SetName("timestampDESC"),
}, {
Keys: bson.D{{"timestampid", 1}},
Options: options.Index().SetName("timestampASC"),
}})
if err != nil {
panic(err)
}
go func() {
<-ctx.Done()
if err = client.Disconnect(ctx); err != nil {
panic(err)
}
}()
return &Handler{Client: c, DB: *collection}
}