Skip to content

Commit 7cc011e

Browse files
committed
Added function to pull config from .env.
1 parent 3d156fb commit 7cc011e

12 files changed

Lines changed: 779 additions & 2 deletions

File tree

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
PORT=8088
3+
24
MYSQL_HOST=db
35
MYSQL_ROOT_PASSWORD=pass12
46
MYSQL_DATABASE=dhapi

cmd/server/run.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func run() error {
3636
return fmt.Errorf("error starting main. %s", err.Error())
3737
}
3838

39+
fmt.Printf(">>>>>>>>>>>>>> HOST: %s\n", cfg.DB.Host)
40+
3941
// Setup logger.
4042
l, err := logga.New(&cfg.Logging)
4143
if err != nil {

config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"host": {
3-
"port": "8088"
3+
"port": "ENV:PORT"
44
},
55
"logging": {
66
"writer": "stdout",
77
"logLevel": "DEBUG",
88
"outputFormat": "text"
99
},
1010
"database": {
11-
"host": "localhost",
11+
"host": "ENV:MYSQL_HOST",
1212
"name": "dhapi",
1313
"user": "dev",
1414
"password": "pass12"

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/go-sql-driver/mysql v1.8.1
1212
github.com/jinzhu/copier v0.4.0
1313
github.com/jmoiron/sqlx v1.4.0
14+
github.com/joho/godotenv v1.5.1
1415
github.com/julienschmidt/httprouter v1.3.0
1516
github.com/mythrnr/httprouter-group v0.9.1
1617
github.com/stretchr/testify v1.9.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
1212
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
1313
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
1414
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
15+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
16+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
1517
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
1618
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
1719
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=

internal/config/config.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import (
66
"fmt"
77
"log"
88
"os"
9+
"reflect"
10+
"strings"
11+
12+
"github.com/joho/godotenv"
913
)
1014

1115
type Config struct {
@@ -39,6 +43,11 @@ type DB struct {
3943
func New(configFile string) (*Config, error) {
4044
log.Printf("Loading config from file: %s", configFile)
4145

46+
err := godotenv.Load()
47+
if err != nil {
48+
log.Println("Unable to load .env")
49+
}
50+
4251
var c Config
4352

4453
pwd, _ := os.Getwd()
@@ -58,9 +67,42 @@ func New(configFile string) (*Config, error) {
5867
return nil, err
5968
}
6069

70+
resolveEnvInStruct(&c)
71+
6172
if c.DB.Host == "" || c.DB.Name == "" || c.DB.User == "" || c.DB.Pass == "" {
6273
return &c, fmt.Errorf("some configuration is missing")
6374
}
6475

6576
return &c, nil
6677
}
78+
79+
// resolveEnvInStruct will recursively look through config and replace any values with `ENV:XXX` with the
80+
// corresponding value found in `.env`.
81+
func resolveEnvInStruct(s interface{}) {
82+
val := reflect.ValueOf(s)
83+
84+
if val.Kind() != reflect.Ptr || val.Elem().Kind() != reflect.Struct {
85+
return
86+
}
87+
val = val.Elem()
88+
// typ := val.Type()
89+
90+
for i := 0; i < val.NumField(); i++ {
91+
field := val.Field(i)
92+
// fieldType := typ.Field(i)
93+
94+
switch field.Kind() {
95+
case reflect.String:
96+
if field.CanSet() && strings.HasPrefix(field.String(), "ENV:") {
97+
envKey := strings.TrimPrefix(field.String(), "ENV:")
98+
if envVal, ok := os.LookupEnv(envKey); ok {
99+
field.SetString(envVal)
100+
} else {
101+
fmt.Printf("Warning: env var %s not set\n", envKey)
102+
}
103+
}
104+
case reflect.Struct:
105+
resolveEnvInStruct(field.Addr().Interface())
106+
}
107+
}
108+
}

vendor/github.com/joho/godotenv/.gitignore

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/joho/godotenv/LICENCE

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/joho/godotenv/README.md

Lines changed: 202 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)