Skip to content

Commit cb491f7

Browse files
authored
override NATS URL with an environment variable (#2)
* override NATS URL with an environment variable * go fmt
1 parent 82b27ef commit cb491f7

File tree

3 files changed

+9
-18
lines changed

3 files changed

+9
-18
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ observation-encoder -config /path/to/config/file
1919
# Sample config file
2020

2121
```toml
22-
address = "127.0.0.1"
23-
port = "10000"
24-
2522
[cert]
2623
interval = 100
2724
cert_dir = "/path/to/certs/dir"

cmd/observation-encoder/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"github.com/dnstapir/observation-encoder/internal/nats"
2222
)
2323

24+
const c_ENVVAR_OVERRIDE_NATS_URL = "DNSTAPIR_NATS_URL"
25+
2426
var commit = "BAD-BUILD"
2527

2628
type conf struct {
@@ -105,6 +107,12 @@ func main() {
105107
log.Error("Error creating nats log: %s", err)
106108
}
107109

110+
envNatsUrl, overrideNatsUrl := os.LookupEnv(c_ENVVAR_OVERRIDE_NATS_URL)
111+
if overrideNatsUrl {
112+
mainConf.Nats.Url = envNatsUrl
113+
log.Info("Overriding NATS url with environment variable '%s'", c_ENVVAR_OVERRIDE_NATS_URL)
114+
}
115+
108116
mainConf.Nats.Log = natslog
109117
natsHandle, err := nats.Create(mainConf.Nats)
110118
if err != nil {

internal/app/app.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ const c_NATS_DELIM = common.NATS_DELIM
1414

1515
type Conf struct {
1616
Log common.Logger
17-
Debug bool `toml:"debug"`
18-
Address string `toml:"address"`
19-
Port string `toml:"port"`
17+
Debug bool `toml:"debug"`
2018
NatsHandle nats
2119
LibtapirHandle libtapir
2220
}
@@ -26,8 +24,6 @@ type appHandle struct {
2624
log common.Logger
2725
natsHandle nats
2826
libtapirHandle libtapir
29-
address string
30-
port string
3127
exitCh chan<- common.Exit
3228
pm
3329
}
@@ -67,17 +63,7 @@ func Create(conf Conf) (*appHandle, error) {
6763
return nil, common.ErrBadHandle
6864
}
6965

70-
if conf.Address == "" {
71-
return nil, common.ErrBadParam
72-
}
73-
74-
if conf.Port == "" {
75-
return nil, common.ErrBadParam
76-
}
77-
7866
a.log = conf.Log
79-
a.address = conf.Address
80-
a.port = conf.Port
8167
a.id = "main app"
8268
a.natsHandle = conf.NatsHandle
8369
a.libtapirHandle = conf.LibtapirHandle

0 commit comments

Comments
 (0)