Skip to content

Commit fbaa8a4

Browse files
authored
chore: move config to internals (#102)
1 parent 1d9a314 commit fbaa8a4

File tree

10 files changed

+104
-87
lines changed

10 files changed

+104
-87
lines changed
Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
"strconv"
88
"strings"
99

10-
"github.com/codeshelldev/secured-signal-api/utils/config/structure"
10+
"github.com/codeshelldev/secured-signal-api/internals/config/structure"
11+
"github.com/codeshelldev/secured-signal-api/utils/configutils"
1112
jsonutils "github.com/codeshelldev/secured-signal-api/utils/jsonutils"
1213
log "github.com/codeshelldev/secured-signal-api/utils/logger"
1314

@@ -24,56 +25,70 @@ var ENV *structure.ENV = &structure.ENV{
2425
INSECURE: false,
2526
}
2627

28+
var defaultsConf = configutils.New()
29+
var userConf = configutils.New()
30+
var tokenConf = configutils.New()
31+
32+
var config = configutils.New()
33+
2734
func Load() {
35+
InitReload()
36+
2837
LoadDefaults()
2938

3039
LoadConfig()
3140

3241
LoadTokens()
3342

34-
LoadEnv(userLayer)
43+
userConf.LoadEnv()
3544

36-
config = mergeLayers()
45+
config.MergeLayers(defaultsConf.Layer, userConf.Layer)
3746

38-
normalizeKeys(config)
39-
templateConfig(config)
47+
config.NormalizeKeys()
48+
config.TemplateConfig()
4049

4150
InitTokens()
4251

4352
InitEnv()
4453

4554
log.Info("Finished Loading Configuration")
4655

47-
log.Dev("Loaded Config:\n" + jsonutils.ToJson(config.All()))
48-
log.Dev("Loaded Token Configs:\n" + jsonutils.ToJson(tokensLayer.All()))
56+
log.Dev("Loaded Config:\n" + jsonutils.ToJson(config.Layer.All()))
57+
log.Dev("Loaded Token Configs:\n" + jsonutils.ToJson(tokenConf.Layer.All()))
58+
}
59+
60+
func InitReload() {
61+
defaultsConf.OnLoad(Load)
62+
userConf.OnLoad(Load)
63+
tokenConf.OnLoad(Load)
4964
}
5065

5166
func InitEnv() {
52-
ENV.PORT = strconv.Itoa(config.Int("service.port"))
67+
ENV.PORT = strconv.Itoa(config.Layer.Int("service.port"))
5368

54-
ENV.LOG_LEVEL = strings.ToLower(config.String("loglevel"))
69+
ENV.LOG_LEVEL = strings.ToLower(config.Layer.String("loglevel"))
5570

56-
ENV.API_URL = config.String("api.url")
71+
ENV.API_URL = config.Layer.String("api.url")
5772

5873
var settings structure.SETTINGS
5974

60-
transformChildren(config, "settings.message.variables", transformVariables)
75+
config.TransformChildren("settings.message.variables", transformVariables)
6176

62-
config.Unmarshal("settings", &settings)
77+
config.Layer.Unmarshal("settings", &settings)
6378

6479
ENV.SETTINGS["*"] = &settings
6580
}
6681

6782
func LoadDefaults() {
68-
_, err := LoadFile(ENV.DEFAULTS_PATH, defaultsLayer, yaml.Parser())
83+
_, err := defaultsConf.LoadFile(ENV.DEFAULTS_PATH, yaml.Parser())
6984

7085
if err != nil {
7186
log.Warn("Could not Load Defaults", ENV.DEFAULTS_PATH)
7287
}
7388
}
7489

7590
func LoadConfig() {
76-
_, err := LoadFile(ENV.CONFIG_PATH, userLayer, yaml.Parser())
91+
_, err := userConf.LoadFile(ENV.CONFIG_PATH, yaml.Parser())
7792

7893
if err != nil {
7994
_, fsErr := os.Stat(ENV.CONFIG_PATH)
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package config
33
import (
44
"strconv"
55

6-
"github.com/codeshelldev/secured-signal-api/utils/config/structure"
6+
"github.com/codeshelldev/secured-signal-api/internals/config/structure"
77
log "github.com/codeshelldev/secured-signal-api/utils/logger"
88
"github.com/knadh/koanf/parsers/yaml"
99
)
@@ -16,25 +16,25 @@ type TOKEN_CONFIG_ struct {
1616
func LoadTokens() {
1717
log.Debug("Loading Configs in ", ENV.TOKENS_DIR)
1818

19-
err := LoadDir("tokenconfigs", ENV.TOKENS_DIR, tokensLayer, yaml.Parser())
19+
err := tokenConf.LoadDir("tokenconfigs", ENV.TOKENS_DIR, ".yml", yaml.Parser())
2020

2121
if err != nil {
2222
log.Error("Could not Load Configs in ", ENV.TOKENS_DIR, ": ", err.Error())
2323
}
2424

25-
normalizeKeys(tokensLayer)
25+
tokenConf.NormalizeKeys()
2626

27-
templateConfig(tokensLayer)
27+
tokenConf.TemplateConfig()
2828
}
2929

3030
func InitTokens() {
31-
apiTokens := config.Strings("api.tokens")
31+
apiTokens := config.Layer.Strings("api.tokens")
3232

3333
var tokenConfigs []TOKEN_CONFIG_
3434

35-
transformChildrenUnderArray(tokensLayer, "tokenconfigs", "overrides.message.variables", transformVariables)
35+
tokenConf.TransformChildrenUnderArray("tokenconfigs", "overrides.message.variables", transformVariables)
3636

37-
tokensLayer.Unmarshal("tokenconfigs", &tokenConfigs)
37+
tokenConf.Layer.Unmarshal("tokenconfigs", &tokenConfigs)
3838

3939
overrides := parseTokenConfigs(tokenConfigs)
4040

@@ -53,7 +53,7 @@ func InitTokens() {
5353

5454
// Set Blocked Endpoints on Config to User Layer Value
5555
// => effectively ignoring Default Layer
56-
config.Set("settings.access.endpoints", userLayer.Strings("settings.access.endpoints"))
56+
config.Layer.Set("settings.access.endpoints", userConf.Layer.Strings("settings.access.endpoints"))
5757
}
5858

5959
if len(apiTokens) > 0 {

internals/proxy/middlewares/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"slices"
88
"strings"
99

10-
"github.com/codeshelldev/secured-signal-api/utils/config"
10+
"github.com/codeshelldev/secured-signal-api/internals/config"
1111
log "github.com/codeshelldev/secured-signal-api/utils/logger"
1212
)
1313

internals/proxy/middlewares/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package middlewares
33
import (
44
"net/http"
55

6-
"github.com/codeshelldev/secured-signal-api/utils/config"
7-
"github.com/codeshelldev/secured-signal-api/utils/config/structure"
6+
"github.com/codeshelldev/secured-signal-api/internals/config"
7+
"github.com/codeshelldev/secured-signal-api/internals/config/structure"
88
)
99

1010
type Context struct {

internals/proxy/middlewares/mapping.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/http"
77
"strconv"
88

9-
"github.com/codeshelldev/secured-signal-api/utils/config/structure"
9+
"github.com/codeshelldev/secured-signal-api/internals/config/structure"
1010
jsonutils "github.com/codeshelldev/secured-signal-api/utils/jsonutils"
1111
log "github.com/codeshelldev/secured-signal-api/utils/logger"
1212
request "github.com/codeshelldev/secured-signal-api/utils/request"

internals/proxy/middlewares/policy.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import (
55
"net/http"
66
"strings"
77

8-
"github.com/codeshelldev/secured-signal-api/utils/config/structure"
9-
"github.com/codeshelldev/secured-signal-api/utils/jsonutils"
8+
"github.com/codeshelldev/secured-signal-api/internals/config/structure"
109
log "github.com/codeshelldev/secured-signal-api/utils/logger"
1110
request "github.com/codeshelldev/secured-signal-api/utils/request"
1211
)
@@ -103,9 +102,6 @@ func doBlock(body map[string]any, headers map[string]any, policies map[string]st
103102
for field, policy := range allowed {
104103
value, err := getField(field, body, headers)
105104

106-
log.Dev("Checking ", field, "...")
107-
log.Dev("Got Value of ", jsonutils.ToJson(value))
108-
109105
if value == policy.Value && err == nil {
110106
isExplictlyAllowed = true
111107
cause = field
@@ -116,9 +112,6 @@ func doBlock(body map[string]any, headers map[string]any, policies map[string]st
116112
for field, policy := range blocked {
117113
value, err := getField(field, body, headers)
118114

119-
log.Dev("Checking ", field, "...")
120-
log.Dev("Got Value of ", jsonutils.ToJson(value))
121-
122115
if value == policy.Value && err == nil {
123116
isExplicitlyBlocked = true
124117
cause = field

internals/proxy/middlewares/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package middlewares
33
import (
44
"net/http"
55

6-
"github.com/codeshelldev/secured-signal-api/utils/config"
6+
"github.com/codeshelldev/secured-signal-api/internals/config"
77
)
88

99
var Server Middleware = Middleware{

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"net/http"
55
"os"
66

7+
config "github.com/codeshelldev/secured-signal-api/internals/config"
8+
"github.com/codeshelldev/secured-signal-api/internals/config/structure"
79
reverseProxy "github.com/codeshelldev/secured-signal-api/internals/proxy"
8-
config "github.com/codeshelldev/secured-signal-api/utils/config"
9-
"github.com/codeshelldev/secured-signal-api/utils/config/structure"
1010
docker "github.com/codeshelldev/secured-signal-api/utils/docker"
1111
log "github.com/codeshelldev/secured-signal-api/utils/logger"
1212
)

0 commit comments

Comments
 (0)