Skip to content

Commit 98cae08

Browse files
committed
Use toml conf format. Set debug printouts via config file or flag
1 parent 20bcefd commit 98cae08

File tree

6 files changed

+38
-20
lines changed

6 files changed

+38
-20
lines changed

cmd/observation-encoder/main.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
"encoding/json"
65
"flag"
76
"fmt"
87
"os"
@@ -11,21 +10,23 @@ import (
1110
"syscall"
1211
"time"
1312

13+
//"github.com/nats-io/nats.go"
14+
"github.com/pelletier/go-toml/v2"
15+
1416
"github.com/dnstapir/observation-encoder/internal/api"
1517
"github.com/dnstapir/observation-encoder/internal/app"
1618
"github.com/dnstapir/observation-encoder/internal/cert"
1719
"github.com/dnstapir/observation-encoder/internal/common"
1820
"github.com/dnstapir/observation-encoder/internal/logger"
1921
)
2022

21-
/* Rewritten if building with make */
22-
var version = "BAD-BUILD"
2323
var commit = "BAD-BUILD"
2424

2525
type conf struct {
2626
app.Conf
27-
Api api.Conf
28-
Cert cert.Conf `json:"cert"`
27+
Debug bool `toml:"debug"`
28+
Api api.Conf `toml:"api"`
29+
Cert cert.Conf `toml:"cert"`
2930
}
3031

3132
func main() {
@@ -41,7 +42,7 @@ func main() {
4142
)
4243
flag.StringVar(&configFile,
4344
"config",
44-
"",
45+
"config.toml",
4546
"Configuration file to use",
4647
)
4748
flag.BoolVar(&debugFlag,
@@ -59,13 +60,11 @@ func main() {
5960
panic(fmt.Sprintf("Could not create logger, err: '%s'", err))
6061
}
6162

62-
log.Info("observation-encoder version: '%s', commit: '%s'", version, commit)
63+
log.Info("observation-encoder, commit: '%s'", commit)
6364
if runVersionCmd {
6465
os.Exit(0)
6566
}
6667

67-
log.Debug("Debug logging enabled")
68-
6968
if configFile == "" {
7069
log.Error("No config file specified, exiting...")
7170
os.Exit(-1)
@@ -78,7 +77,7 @@ func main() {
7877
}
7978
defer file.Close()
8079

81-
confDecoder := json.NewDecoder(file)
80+
confDecoder := toml.NewDecoder(file)
8281
if confDecoder == nil {
8382
log.Error("Problem decoding config file '%s', exiting...", configFile)
8483
os.Exit(-1)
@@ -88,21 +87,32 @@ func main() {
8887
confDecoder.Decode(&mainConf)
8988
file.Close() // TODO okay to close here while also using defer above?
9089

91-
mainConf.Log = log
90+
// TODO create different loggers with different debug settings
91+
applog, err := logger.Create(
92+
logger.Conf{
93+
Debug: debugFlag || mainConf.Debug,
94+
})
95+
if err != nil {
96+
log.Error("Error creating app log: %s", err)
97+
} else {
98+
applog.Debug("Debug logging enabled")
99+
}
100+
101+
mainConf.Log = applog
92102
appHandle, err := app.Create(mainConf.Conf)
93103
if err != nil {
94104
log.Error("Error creating application: '%s'", err)
95105
os.Exit(-1)
96106
}
97107

98-
mainConf.Cert.Log = log
108+
mainConf.Cert.Log = applog
99109
certHandle, err := cert.Create(mainConf.Cert)
100110
if err != nil {
101111
log.Error("Error creating cert manager: '%s'", err)
102112
os.Exit(-1)
103113
}
104114

105-
mainConf.Api.Log = log
115+
mainConf.Api.Log = applog
106116
mainConf.Api.App = appHandle
107117
mainConf.Api.Certs = certHandle
108118
apiHandle, err := api.Create(mainConf.Api)
@@ -119,11 +129,15 @@ func main() {
119129
ctx, cancel := context.WithCancel(context.Background())
120130
exitCh := make(chan common.Exit)
121131

132+
log.Info("Starting threads...")
133+
122134
go appHandle.Run(ctx, exitCh)
123135

124136
go apiHandle.Run(ctx, exitCh)
125137
go certHandle.Run(ctx, exitCh)
126138

139+
log.Info("Threads started!")
140+
127141
exitLoop := false
128142
var wg sync.WaitGroup
129143
wg.Add(1)

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/dnstapir/observation-encoder
22

33
go 1.24.6
4+
5+
require github.com/pelletier/go-toml/v2 v2.2.4

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
2+
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=

internal/api/api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
)
1515

1616
type Conf struct {
17-
Active bool `json:"active"`
18-
Address string `json:"address"`
19-
Port string `json:"port"`
17+
Active bool `toml:"active"`
18+
Address string `toml:"address"`
19+
Port string `toml:"port"`
2020
Log common.Logger
2121
App appHandle
2222
Certs certHandle

internal/app/app.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const c_N_HANDLERS = 3
1313

1414
type Conf struct {
1515
Log common.Logger
16-
Address string `json:"address"`
17-
Port string `json:"port"`
16+
Address string `toml:"address"`
17+
Port string `toml:"port"`
1818
}
1919

2020
type appHandle struct {

internal/cert/cert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717

1818
type Conf struct {
1919
Log common.Logger
20-
Interval int `json:"interval"`
21-
CertDir string `json:"cert_dir"`
20+
Interval int `toml:"interval"`
21+
CertDir string `toml:"cert_dir"`
2222
}
2323

2424
type certHandle struct {

0 commit comments

Comments
 (0)