-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
91 lines (70 loc) · 2.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"context"
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/worldline-go/igconfig"
"github.com/worldline-go/igconfig/loader"
)
// Config struct detailing all project parameters.
type Config struct {
AppPort int `cfg:"appport" default:"9057"`
// application specific vault
DBSchema string `cfg:"dbSchema" env:"DBSCHEMA" secret:"dbSchema,loggable" default:"transaction"`
DBDataSource string `cfg:"dbDataSource" env:"DBDATASOURCE" secret:"dbDataSource" loggable:"true"`
DBType string `cfg:"dbType" env:"DBTYPE" secret:"dbType,loggable" default:"pgx" loggable:"false"`
SuperSecret SuperSecret `cfg:"super_secret"`
Test time.Duration `cfg:"test-value" default:"2d"`
// generic vault secrets
Keycloack Keycloack
// env automatically compare uppercase
Migrate Migrate `cfg:"migrations" secret:"migrations,loggable" env:"migrations"`
Abc interface{}
LoadTest LoadTest `cfg:"loadtest" secret:"loadtest,loggable"`
}
type LoadTest struct {
Data string `cfg:"data" secret:"data,loggable"`
}
type SuperSecret struct {
Topsecret string `secret:"topSecret,loggable" default:""`
}
type Migrate struct {
DBDatasource string `env:"DBDATASOURCE" secret:"dbDataSource,loggable" default:""`
GetENV string `env:"TEST_ENV" loggable:"true" default:"X"`
}
type Keycloack struct {
SSOBaseURL string `cfg:"ssobaseurl" secret:"ssobaseurl,loggable"`
SSOPublicKeyID string `cfg:"ssopubkeyid" secret:"ssopubkeyid,loggable"`
SSORealm string `cfg:"ssorealm" secret:"ssorealm,loggable"`
CacheRetention string `cfg:"cacheretention" default:"30m"`
}
func main() {
// pretty logging
log.Logger = zerolog.New(
zerolog.ConsoleWriter{
Out: os.Stderr,
FormatTimestamp: func(i interface{}) string {
parse, _ := time.Parse(time.RFC3339, i.(string))
return parse.Format("2006-01-02 15:04:05")
},
}).With().Timestamp().Caller().Logger()
// run igconfig
// get logger context for config
logConfig := log.With().Str("component", "config").Logger()
ctx := logConfig.WithContext(context.Background())
// ctx := context.Background()
loader.VaultSecretAdditionalPaths = append(
loader.VaultSecretAdditionalPaths,
loader.AdditionalPath{Map: "loadtest", Name: "loadtest"},
)
var conf Config
if err := igconfig.LoadConfigWithContext(ctx, "test2", &conf); err != nil {
log.Ctx(ctx).Fatal().Err(err).Msg("unable to load configuration settings.")
}
// print values
log.Ctx(ctx).Info().
EmbedObject(igconfig.Printer{Value: conf}).Msg("loaded config")
log.Ctx(ctx).Info().Msg(conf.Test.String())
}