Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Fabian Vitt
good2go
Commits
73fdb3b7
Commit
73fdb3b7
authored
Jun 03, 2020
by
Max Richter
Browse files
style(mongo): seperate utils funcs into two files
parent
1b5ce525
Changes
2
Hide whitespace changes
Inline
Side-by-side
database/mongo/utils/db.go
View file @
73fdb3b7
...
...
@@ -2,9 +2,7 @@ package utils
import
(
"context"
"flag"
"log"
"os"
"strings"
"time"
...
...
@@ -12,37 +10,8 @@ import (
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
// Autoloads .env file
godotenv
"github.com/joho/godotenv"
)
// GetEnv gets a env variable, and when not present returns the default value
func
GetEnv
(
variableName
string
,
defaultValue
string
)
string
{
godotenv
.
Load
(
".env"
)
envValue
:=
os
.
Getenv
(
variableName
)
if
envValue
==
""
{
return
defaultValue
}
return
envValue
}
// GetDBName gets the db name from the environment
func
GetDBName
()
string
{
dbName
:=
GetEnv
(
"MONGO_DB"
,
"good2go"
)
if
flag
.
Lookup
(
"test.v"
)
==
nil
{
return
dbName
}
return
dbName
+
"_test"
}
// CreateDBClient creates a mongodb client and connects to the specified database
func
CreateDBClient
(
timeout
time
.
Duration
)
(
client
*
mongo
.
Client
,
ctx
context
.
Context
)
{
...
...
@@ -53,7 +22,7 @@ func CreateDBClient(timeout time.Duration) (client *mongo.Client, ctx context.Co
log
.
Fatal
(
err
)
}
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
timeout
)
ctx
,
cancel
:=
CreateTimeoutCTX
(
timeout
)
defer
cancel
()
err
=
client
.
Connect
(
ctx
)
if
err
!=
nil
{
...
...
@@ -79,6 +48,7 @@ func CreateDBClient(timeout time.Duration) (client *mongo.Client, ctx context.Co
return
client
,
ctx
}
func
CreateTimoutCTX
(
timeout
time
.
Duration
)
(
context
.
Context
,
context
.
CancelFunc
)
{
// CreateTimeoutCTX creates a timeout context.
func
CreateTimeoutCTX
(
timeout
time
.
Duration
)
(
context
.
Context
,
context
.
CancelFunc
)
{
return
context
.
WithTimeout
(
context
.
Background
(),
timeout
)
}
database/mongo/utils/env.go
0 → 100644
View file @
73fdb3b7
package
utils
import
(
"flag"
"os"
godotenv
"github.com/joho/godotenv"
)
// GetEnv gets a env variable, and when not present returns the default value
func
GetEnv
(
variableName
string
,
defaultValue
string
)
string
{
godotenv
.
Load
(
".env"
)
envValue
:=
os
.
Getenv
(
variableName
)
if
envValue
==
""
{
return
defaultValue
}
return
envValue
}
// GetDBName gets the db name from the environment
func
GetDBName
()
string
{
dbName
:=
GetEnv
(
"MONGO_DB"
,
"good2go"
)
if
flag
.
Lookup
(
"test.v"
)
==
nil
{
return
dbName
}
return
dbName
+
"_test"
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment