Skip to content

Commit 18f7e51

Browse files
committed
added: dotenv library for local testing
1 parent 1f82af9 commit 18f7e51

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*.so
66
*.dylib
77

8+
.env
9+
810
# Test binary, built with `go test -c`
911
*.test
1012

.sample.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DATABASE_URL=<URL>
2+
DATABASE_COLLECTION=<COLLECTION>
3+
DATABASE_NAME=<NAME>
4+
PORT=<PORT>

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/go-stack/stack v1.8.0 // indirect
88
github.com/gofiber/fiber/v2 v2.23.0 // indirect
99
github.com/golang/snappy v0.0.3 // indirect
10+
github.com/joho/godotenv v1.4.0 // indirect
1011
github.com/klauspost/compress v1.13.6 // indirect
1112
github.com/pkg/errors v0.9.1 // indirect
1213
github.com/valyala/bytebufferpool v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
1010
github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA=
1111
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
1212
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
13+
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
14+
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
1315
github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s=
1416
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
1517
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=

server.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/gofiber/fiber/v2"
1010
"github.com/gofiber/fiber/v2/middleware/cors"
1111
"github.com/hansmrtn/pay-party-api/models"
12+
"github.com/joho/godotenv"
1213
"go.mongodb.org/mongo-driver/bson"
1314
"go.mongodb.org/mongo-driver/bson/primitive"
1415
"go.mongodb.org/mongo-driver/mongo"
@@ -20,15 +21,24 @@ type MongoInstance struct {
2021
DB *mongo.Database
2122
}
2223

23-
// Get env variables
24-
var mongoURI = os.Getenv("DATABASE_URL")
25-
var dbCollection = os.Getenv("DATABASE_COLLECTION")
26-
var dbName = os.Getenv("DATABASE_NAME")
27-
var port = os.Getenv("PORT")
24+
var mongoURI string
25+
var dbCollection string
26+
var dbName string
27+
var port string
2828

2929
var mg MongoInstance
3030

3131
func init() {
32+
err := godotenv.Load(".env")
33+
if err != nil {
34+
log.Print("Error loading .env file")
35+
}
36+
// Get env variables
37+
mongoURI = os.Getenv("DATABASE_URL")
38+
dbCollection = os.Getenv("DATABASE_COLLECTION")
39+
dbName = os.Getenv("DATABASE_NAME")
40+
port = os.Getenv("PORT")
41+
3242
client, err := mongo.NewClient(options.Client().ApplyURI(mongoURI))
3343
if err != nil {
3444
log.Fatal(err)

0 commit comments

Comments
 (0)