Skip to content

Commit

Permalink
chore(db): Using a unified environment variable for the db connection…
Browse files Browse the repository at this point in the history
… string (#40)

Using a unified environment variable for the db connection string
  • Loading branch information
Jacobbrewer1 authored Jan 25, 2024
1 parent a5c027d commit 77432bf
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ data retention on a more reliable database.

#### MySQL

When using MySQL, you will be required to specify a `MYSQL_CONNECTION` environment variable with the connection string
When using MySQL, you will be required to specify a `DB_CONN_STR` environment variable with the connection string
to your MySQL database. For example:

```text
MYSQL_CONNECTION="root:Password01@tcp(localhost:3306)/puppet-summary?timeout=90s&multiStatements=true&parseTime=true"
DB_CONN_STR="root:Password01@tcp(localhost:3306)/puppet-summary?timeout=90s&multiStatements=true&parseTime=true"
```

#### MongoDB

When using MongoDB, you will be required to specify a `MONGO_URI` environment variable with the connection URI to your
When using MongoDB, you will be required to specify a `DB_CONN_STR` environment variable with the connection URI to your
MongoDB database. For example:

```text
MONGO_URI="mongodb+srv://user:password@host/?retryWrites=true"
DB_CONN_STR="mongodb+srv://user:password@host/?retryWrites=true"
```

#### Google Cloud Storage
Expand Down
2 changes: 2 additions & 0 deletions pkg/dataaccess/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/Jacobbrewer1/puppet-summary/pkg/entities"
)

const envDbConnStr = "DB_CONN_STR"

var DB Database

type Database interface {
Expand Down
4 changes: 1 addition & 3 deletions pkg/dataaccess/db_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)

const EnvMongoURI = `MONGO_URI`

const mongoDatabase = "puppet-summary"

func connectMongoDB(ctx context.Context) {
connectionString := os.Getenv(EnvMongoURI)
connectionString := os.Getenv(envDbConnStr)
if connectionString != "" {
slog.Debug("Found MongoDB URI in environment")
} else {
Expand Down
4 changes: 1 addition & 3 deletions pkg/dataaccess/db_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

const EnvMySQLConnection = `MYSQL_CONNECTION`

func connectMysql() {
connectionString := os.Getenv(EnvMySQLConnection)
connectionString := os.Getenv(envDbConnStr)
if connectionString != "" {
slog.Debug("Found MySQL URI in environment")
} else {
Expand Down

0 comments on commit 77432bf

Please sign in to comment.