Skip to content

Commit 6810438

Browse files
committed
feat: support mongodb uri for connecting
1 parent 33fac94 commit 6810438

File tree

6 files changed

+46
-28
lines changed

6 files changed

+46
-28
lines changed

cli/cli.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func Execute() {
1818
cmdServe(),
1919
cmdMigrate(),
2020
cmdVersion(),
21+
cmdShowConfigs(),
2122
)
2223

2324
cmdx.SetHelp(rootCmd)

cli/config.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package cli
22

33
import (
4+
"fmt"
5+
"os"
6+
47
"github.com/odpf/salt/config"
58
"github.com/spf13/cobra"
9+
"gopkg.in/yaml.v2"
610

711
"github.com/odpf/entropy/internal/server"
812
"github.com/odpf/entropy/internal/store/mongodb"
@@ -12,9 +16,24 @@ import (
1216

1317
const configFlag = "config"
1418

19+
func cmdShowConfigs() *cobra.Command {
20+
return &cobra.Command{
21+
Use: "configs",
22+
Short: "Display configurations currently loaded",
23+
Run: func(cmd *cobra.Command, args []string) {
24+
cfg, err := loadConfig(cmd)
25+
if err != nil {
26+
fmt.Printf("failed to read configs: %v\n", err)
27+
os.Exit(1)
28+
}
29+
_ = yaml.NewEncoder(os.Stdout).Encode(cfg)
30+
},
31+
}
32+
}
33+
1534
// Config contains the application configuration
1635
type Config struct {
17-
DB mongodb.DBConfig `mapstructure:"db"`
36+
DB mongodb.Config `mapstructure:"db"`
1837
Log logger.LogConfig `mapstructure:"log"`
1938
Service server.Config `mapstructure:"service"`
2039
NewRelic metric.NewRelicConfig `mapstructure:"newrelic"`

cli/migrate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func cmdMigrate() *cobra.Command {
3131
return cmd
3232
}
3333

34-
func runMigrations(ctx context.Context, cfg mongodb.DBConfig) error {
35-
mongoStore, err := mongodb.New(&cfg)
34+
func runMigrations(ctx context.Context, cfg mongodb.Config) error {
35+
mongoStore, err := mongodb.Connect(cfg)
3636
if err != nil {
3737
return err
3838
}

cli/serve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func runServer(c Config) error {
5050
return err
5151
}
5252

53-
mongoStore, err := mongodb.New(&c.DB)
53+
mongoStore, err := mongodb.Connect(c.DB)
5454
if err != nil {
5555
return err
5656
}

entropy.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ service:
22
host: localhost
33
port: 8080
44

5+
# MongoDB configurations
6+
# Refer https://www.mongodb.com/docs/manual/reference/connection-string/ for the
7+
# uri format.
58
db:
6-
host: localhost
9+
uri: "mongodb://localhost:27017/?connectTimeoutMS=1000&socketTimeoutMS=1000&readPreference=secondaryPreferred"
710
name: entropy
8-
port: 27017
11+
ping_timeout: 3s
912

1013
newrelic:
1114
enabled: false
12-
appname: entropy-dev
15+
appname: Entropy
1316
license: ____LICENSE_STRING_OF_40_CHARACTERS_____
1417

1518
log:

internal/store/mongodb/db.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,37 @@ package mongodb
22

33
import (
44
"context"
5-
"fmt"
65
"time"
76

87
"go.mongodb.org/mongo-driver/mongo"
98
"go.mongodb.org/mongo-driver/mongo/options"
109
)
1110

12-
// DBConfig contains the database configuration
13-
type DBConfig struct {
14-
Host string `mapstructure:"host" default:"localhost"`
15-
Port string `mapstructure:"port" default:"27017"`
11+
// Config contains the database configurations.
12+
type Config struct {
13+
// URI should be valid MongodDB connection string.
14+
// https://www.mongodb.com/docs/manual/reference/connection-string/
15+
URI string `mapstructure:"uri" default:"mongodb://localhost:27017"`
16+
17+
// Name should be the name of the database to use.
1618
Name string `mapstructure:"name" default:"entropy"`
17-
}
1819

19-
// New returns the database instance
20-
func New(config *DBConfig) (*mongo.Database, error) {
21-
uri := fmt.Sprintf(
22-
"mongodb://%s:%s/%s",
23-
config.Host,
24-
config.Port,
25-
config.Name,
26-
)
20+
// PingTimeout decides the maximum time to wait for ping response.
21+
PingTimeout time.Duration `mapstructure:"ping_timeout" default:"3s"`
22+
}
2723

28-
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
24+
// Connect returns the database instance
25+
func Connect(cfg Config) (*mongo.Database, error) {
26+
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(cfg.URI))
2927
if err != nil {
3028
return nil, err
3129
}
3230

33-
pingCtx, pingCancel := context.WithTimeout(context.TODO(), time.Second*5)
31+
pingCtx, pingCancel := context.WithTimeout(context.TODO(), cfg.PingTimeout)
3432
defer pingCancel()
35-
36-
err = client.Ping(pingCtx, nil)
37-
38-
if err != nil {
33+
if err := client.Ping(pingCtx, nil); err != nil {
3934
return nil, err
4035
}
4136

42-
return client.Database(config.Name), nil
37+
return client.Database(cfg.Name), nil
4338
}

0 commit comments

Comments
 (0)