diff --git a/constants/constants.go b/constants/constants.go index 27865b0..971bedd 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -5,6 +5,7 @@ const ( DB_HOST = "DB_HOST" DB_NAME = "DB_NAME" DB_OPTS = "DB_OPTS" + DB_PORT = "DB_PORT" DB_PW = "DB_PW" DB_SCHEME = "DB_SCHEME" DB_URI = "DB_URI" diff --git a/database/db.go b/database/db.go index 7ce48a2..ac7f805 100644 --- a/database/db.go +++ b/database/db.go @@ -8,6 +8,7 @@ import ( "github.com/sweetrpg/catalog-api/constants" "github.com/sweetrpg/catalog-api/logging" + "github.com/sweetrpg/catalog-api/util" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" @@ -17,28 +18,38 @@ import ( var Db *mongo.Database var client *mongo.Client -func SetupDatabase() { - var dbUrl *url.URL - var err error +func buildDbURL() (*url.URL, string) { dbUri, found := os.LookupEnv(constants.DB_URI) - dbName := os.Getenv(constants.DB_NAME) if found { - dbUrl, err = url.Parse(dbUri) - } else { - dbScheme := os.Getenv(constants.DB_SCHEME) - dbUser := os.Getenv(constants.DB_USER) - dbPW := os.Getenv(constants.DB_PW) - dbHost := os.Getenv(constants.DB_HOST) - dbOpts := os.Getenv(constants.DB_OPTS) - dbUrl = &url.URL{ - Scheme: dbScheme, - Host: dbHost, - User: url.UserPassword(dbUser, dbPW), - Path: dbName, - RawQuery: dbOpts, - ForceQuery: true, + dbUrl, err := url.Parse(dbUri) + if err != nil { + panic(err) } + + return dbUrl, dbUrl.Path[1:] } + + dbScheme := os.Getenv(constants.DB_SCHEME) + dbUser := os.Getenv(constants.DB_USER) + dbPW := os.Getenv(constants.DB_PW) + dbHost := os.Getenv(constants.DB_HOST) + dbPort := util.GetEnvInt(constants.DB_PORT, 27017) + dbOpts := os.Getenv(constants.DB_OPTS) + dbName := os.Getenv(constants.DB_NAME) + dbUrl := &url.URL{ + Scheme: dbScheme, + Host: fmt.Sprintf("%s:%d", dbHost, dbPort), + User: url.UserPassword(dbUser, dbPW), + Path: dbName, + RawQuery: dbOpts, + ForceQuery: true, + } + + return dbUrl, dbName +} + +func SetupDatabase() { + dbUrl, dbName := buildDbURL() logging.Logger.Info("Connecting to database", "url", dbUrl.Redacted()) client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(dbUrl.String())) if err != nil { diff --git a/database/db_test.go b/database/db_test.go new file mode 100644 index 0000000..369f8c6 --- /dev/null +++ b/database/db_test.go @@ -0,0 +1,38 @@ +package database + +import ( + "github.com/stretchr/testify/assert" + "github.com/sweetrpg/catalog-api/constants" + "os" + "testing" +) + +func TestBuildURLFromURI(t *testing.T) { + os.Setenv(constants.DB_URI, "mongo://user:pass@host:12345/db?opts=these") + dbUrl, dbName := buildDbURL() + assert.Equal(t, dbUrl.Scheme, "mongo") + assert.Equal(t, dbUrl.User.Username(), "user") + // assert.Equal(t, dbUrl.User.Password(), "pass") + assert.Equal(t, dbUrl.Host, "host:12345") + assert.Equal(t, dbUrl.Query().Get("opts"), "these") + assert.Equal(t, dbName, "db") +} + +func TestBuildURLFromParts(t *testing.T) { + os.Unsetenv(constants.DB_URI) + os.Setenv(constants.DB_NAME, "db") + os.Setenv(constants.DB_HOST, "host") + os.Setenv(constants.DB_SCHEME, "mongo") + os.Setenv(constants.DB_USER, "user") + os.Setenv(constants.DB_PW, "pass") + os.Setenv(constants.DB_PORT, "12345") + os.Setenv(constants.DB_OPTS, "opts=these") + + dbUrl, dbName := buildDbURL() + assert.Equal(t, dbUrl.Scheme, "mongo") + assert.Equal(t, dbUrl.User.Username(), "user") + // assert.Equal(t, dbUrl.User.Password(), "pass") + assert.Equal(t, dbUrl.Host, "host:12345") + assert.Equal(t, dbUrl.Query().Get("opts"), "these") + assert.Equal(t, dbName, "db") +} diff --git a/go.mod b/go.mod index 2e04bff..6ab6849 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/google/jsonapi v1.0.0 github.com/joho/godotenv v1.5.1 github.com/penglongli/gin-metrics v0.1.10 + github.com/stretchr/testify v1.9.0 github.com/zerodha/logf v0.5.5 go.mongodb.org/mongo-driver v1.17.1 go.opentelemetry.io/otel v1.31.0 @@ -27,6 +28,7 @@ require ( github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/gabriel-vasile/mimetype v1.4.6 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-logr/logr v1.4.2 // indirect @@ -52,6 +54,7 @@ require ( github.com/montanaflynn/stats v0.7.1 // indirect github.com/pelletier/go-toml/v2 v2.2.3 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.12.0 // indirect github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.32.1 // indirect