Skip to content

Commit 614c6eb

Browse files
authored
chore: restructure config (#90)
1 parent 12363d8 commit 614c6eb

File tree

11 files changed

+146
-119
lines changed

11 files changed

+146
-119
lines changed

data/defaults.yml

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,39 @@ service:
44
logLevel: info
55

66
settings:
7-
dataAliases:
8-
"@message":
9-
[
10-
{ alias: msg, score: 100 },
11-
{ alias: content, score: 99 },
12-
{ alias: description, score: 98 },
13-
{ alias: text, score: 20 },
14-
{ alias: summary, score: 15 },
15-
{ alias: details, score: 14 },
7+
message:
8+
fieldMappings:
9+
"@message":
10+
[
11+
{ field: msg, score: 100 },
12+
{ field: content, score: 99 },
13+
{ field: description, score: 98 },
14+
{ field: text, score: 20 },
15+
{ field: summary, score: 15 },
16+
{ field: details, score: 14 },
1617

17-
{ alias: data.message, score: 10 },
18-
{ alias: data.content, score: 9 },
19-
{ alias: data.description, score: 8 },
20-
{ alias: data.text, score: 7 },
21-
{ alias: data.summary, score: 6 },
22-
{ alias: data.details, score: 5 },
18+
{ field: data.message, score: 10 },
19+
{ field: data.content, score: 9 },
20+
{ field: data.description, score: 8 },
21+
{ field: data.text, score: 7 },
22+
{ field: data.summary, score: 6 },
23+
{ field: data.details, score: 5 },
2324

24-
{ alias: body, score: 2 },
25-
{ alias: data, score: 1 },
26-
]
25+
{ field: body, score: 2 },
26+
{ field: data, score: 1 },
27+
]
2728

28-
variables:
29-
recipients: ${RECIPIENTS}
30-
number: ${NUMBER}
29+
variables:
30+
recipients: ${RECIPIENTS}
31+
number: ${NUMBER}
3132

32-
blockedEndpoints:
33-
- /v1/about
34-
- /v1/configuration
35-
- /v1/devices
36-
- /v1/register
37-
- /v1/unregister
38-
- /v1/qrcodelink
39-
- /v1/accounts
40-
- /v1/contacts
33+
access:
34+
endpoints:
35+
- !/v1/about
36+
- !/v1/configuration
37+
- !/v1/devices
38+
- !/v1/register
39+
- !/v1/unregister
40+
- !/v1/qrcodelink
41+
- !/v1/accounts
42+
- !/v1/contacts

internals/proxy/middlewares/common.go

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

66
"github.com/codeshelldev/secured-signal-api/utils/config"
7+
"github.com/codeshelldev/secured-signal-api/utils/config/structure"
78
)
89

910
type Context struct {
@@ -23,7 +24,7 @@ type contextKey string
2324

2425
const tokenKey contextKey = "token"
2526

26-
func getSettingsByReq(req *http.Request) *config.SETTING_ {
27+
func getSettingsByReq(req *http.Request) *structure.SETTINGS {
2728
token, ok := req.Context().Value(tokenKey).(string)
2829

2930
if !ok {
@@ -33,7 +34,7 @@ func getSettingsByReq(req *http.Request) *config.SETTING_ {
3334
return getSettings(token)
3435
}
3536

36-
func getSettings(token string) *config.SETTING_ {
37+
func getSettings(token string) *structure.SETTINGS {
3738
settings, exists := config.ENV.SETTINGS[token]
3839

3940
if !exists || settings == nil {

internals/proxy/middlewares/endpoints.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ func endpointsHandler(next http.Handler) http.Handler {
1717
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
1818
settings := getSettingsByReq(req)
1919

20-
blockedEndpoints := settings.BLOCKED_ENDPOINTS
21-
allowedEndpoints := settings.ALLOWED_ENDPOINTS
20+
endpoints := settings.ACCESS.ENDPOINTS
2221

23-
if blockedEndpoints == nil && allowedEndpoints == nil {
24-
blockedEndpoints = getSettings("*").BLOCKED_ENDPOINTS
22+
if endpoints == nil {
23+
endpoints = getSettings("*").ACCESS.ENDPOINTS
2524
}
2625

2726
reqPath := req.URL.Path
2827

29-
if isBlocked(reqPath, allowedEndpoints, blockedEndpoints) {
28+
if isBlocked(reqPath, endpoints) {
3029
log.Warn("User tried to access blocked endpoint: ", reqPath)
3130
http.Error(w, "Forbidden", http.StatusForbidden)
3231
return
@@ -36,15 +35,26 @@ func endpointsHandler(next http.Handler) http.Handler {
3635
})
3736
}
3837

39-
func isBlocked(endpoint string, allowed []string, blocked []string) bool {
40-
if blocked == nil {
41-
blocked = []string{}
42-
}
38+
func getEndpoints(endpoints []string) ([]string, []string) {
39+
blockedEndpoints := []string{}
40+
allowedEndpoints := []string{}
41+
42+
for _, endpoint := range endpoints {
43+
endpoint, block := strings.CutPrefix(endpoint, "!")
4344

44-
if allowed == nil {
45-
allowed = []string{}
45+
if block {
46+
blockedEndpoints = append(blockedEndpoints, endpoint)
47+
} else {
48+
allowedEndpoints = append(allowedEndpoints, endpoint)
49+
}
4650
}
4751

52+
return allowedEndpoints, blockedEndpoints
53+
}
54+
55+
func isBlocked(endpoint string, endpoints []string) bool {
56+
allowed, blocked := getEndpoints(endpoints)
57+
4858
isExplicitlyBlocked := slices.ContainsFunc(blocked, func(try string) bool {
4959
return strings.HasPrefix(endpoint, try)
5060
})

internals/proxy/middlewares/aliases.go renamed to internals/proxy/middlewares/mapping.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@ import (
66
"net/http"
77
"strconv"
88

9-
middlewareTypes "github.com/codeshelldev/secured-signal-api/internals/proxy/middlewares/types"
9+
"github.com/codeshelldev/secured-signal-api/utils/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"
1313
)
1414

15-
var Aliases Middleware = Middleware{
16-
Name: "Aliases",
17-
Use: aliasesHandler,
15+
var Mapping Middleware = Middleware{
16+
Name: "Mapping",
17+
Use: mappingHandler,
1818
}
1919

20-
func aliasesHandler(next http.Handler) http.Handler {
20+
func mappingHandler(next http.Handler) http.Handler {
2121
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
2222
settings := getSettingsByReq(req)
2323

24-
dataAliases := settings.DATA_ALIASES
24+
fieldMappings := settings.MESSAGE.FIELD_MAPPINGS
2525

26-
if dataAliases == nil {
27-
dataAliases = getSettings("*").DATA_ALIASES
26+
if fieldMappings == nil {
27+
fieldMappings = getSettings("*").MESSAGE.FIELD_MAPPINGS
2828
}
2929

30-
if settings.VARIABLES == nil {
31-
settings.VARIABLES = getSettings("*").VARIABLES
30+
if settings.MESSAGE.VARIABLES == nil {
31+
settings.MESSAGE.VARIABLES = getSettings("*").MESSAGE.VARIABLES
3232
}
3333

3434
body, err := request.GetReqBody(w, req)
@@ -43,7 +43,7 @@ func aliasesHandler(next http.Handler) http.Handler {
4343
if !body.Empty {
4444
bodyData = body.Data
4545

46-
aliasData := processDataAliases(dataAliases, bodyData)
46+
aliasData := processFieldMappings(fieldMappings, bodyData)
4747

4848
for key, value := range aliasData {
4949
prefix := key[:1]
@@ -55,7 +55,7 @@ func aliasesHandler(next http.Handler) http.Handler {
5555
bodyData[keyWithoutPrefix] = value
5656
modifiedBody = true
5757
case ".":
58-
settings.VARIABLES[keyWithoutPrefix] = value
58+
settings.MESSAGE.VARIABLES[keyWithoutPrefix] = value
5959
}
6060
}
6161
}
@@ -84,7 +84,7 @@ func aliasesHandler(next http.Handler) http.Handler {
8484
})
8585
}
8686

87-
func processDataAliases(aliases map[string][]middlewareTypes.DataAlias, data map[string]any) map[string]any {
87+
func processFieldMappings(aliases map[string][]structure.FieldMapping, data map[string]any) map[string]any {
8888
aliasData := map[string]any{}
8989

9090
for key, alias := range aliases {
@@ -98,27 +98,27 @@ func processDataAliases(aliases map[string][]middlewareTypes.DataAlias, data map
9898
return aliasData
9999
}
100100

101-
func getData(key string, aliases []middlewareTypes.DataAlias, data map[string]any) (string, any) {
101+
func getData(key string, aliases []structure.FieldMapping, data map[string]any) (string, any) {
102102
var best int
103103
var value any
104104

105105
for _, alias := range aliases {
106-
aliasValue, score, ok := processAlias(alias, data)
106+
aliasValue, score, ok := processFieldMapping(alias, data)
107107

108108
if ok {
109109
if score > best {
110110
value = aliasValue
111111
}
112112

113-
delete(data, alias.Alias)
113+
delete(data, alias.Field)
114114
}
115115
}
116116

117117
return key, value
118118
}
119119

120-
func processAlias(alias middlewareTypes.DataAlias, data map[string]any) (any, int, bool) {
121-
aliasKey := alias.Alias
120+
func processFieldMapping(alias structure.FieldMapping, data map[string]any) (any, int, bool) {
121+
aliasKey := alias.Field
122122

123123
value, ok := jsonutils.GetByPath(aliasKey, data)
124124

internals/proxy/middlewares/message.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ func messageHandler(next http.Handler) http.Handler {
1919
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
2020
settings := getSettingsByReq(req)
2121

22-
variables := settings.VARIABLES
23-
messageTemplate := settings.MESSAGE_TEMPLATE
22+
variables := settings.MESSAGE.VARIABLES
23+
messageTemplate := settings.MESSAGE.TEMPLATE
2424

2525
if variables == nil {
26-
variables = getSettings("*").VARIABLES
26+
variables = getSettings("*").MESSAGE.VARIABLES
2727
}
2828

2929
if messageTemplate == "" {
30-
messageTemplate = getSettings("*").MESSAGE_TEMPLATE
30+
messageTemplate = getSettings("*").MESSAGE.TEMPLATE
3131
}
3232

3333
body, err := request.GetReqBody(w, req)

internals/proxy/middlewares/template.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ var Template Middleware = Middleware{
2424

2525
func templateHandler(next http.Handler) http.Handler {
2626
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
27-
variables := getSettingsByReq(req).VARIABLES
27+
variables := getSettingsByReq(req).MESSAGE.VARIABLES
2828

2929
if variables == nil {
30-
variables = getSettings("*").VARIABLES
30+
variables = getSettings("*").MESSAGE.VARIABLES
3131
}
3232

3333
body, err := request.GetReqBody(w, req)

internals/proxy/middlewares/types/types.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import (
66

77
reverseProxy "github.com/codeshelldev/secured-signal-api/internals/proxy"
88
config "github.com/codeshelldev/secured-signal-api/utils/config"
9+
"github.com/codeshelldev/secured-signal-api/utils/config/structure"
910
docker "github.com/codeshelldev/secured-signal-api/utils/docker"
1011
log "github.com/codeshelldev/secured-signal-api/utils/logger"
1112
)
1213

1314
var proxy reverseProxy.Proxy
1415

15-
var ENV *config.ENV_
16+
var ENV *structure.ENV
1617

1718
func main() {
1819
logLevel := os.Getenv("LOG_LEVEL")

utils/config/loader.go

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,20 @@ import (
77
"strconv"
88
"strings"
99

10-
middlewareTypes "github.com/codeshelldev/secured-signal-api/internals/proxy/middlewares/types"
10+
"github.com/codeshelldev/secured-signal-api/utils/config/structure"
1111
jsonutils "github.com/codeshelldev/secured-signal-api/utils/jsonutils"
1212
log "github.com/codeshelldev/secured-signal-api/utils/logger"
1313

1414
"github.com/knadh/koanf/parsers/yaml"
1515
)
1616

17-
type ENV_ struct {
18-
CONFIG_PATH string
19-
DEFAULTS_PATH string
20-
FAVICON_PATH string
21-
TOKENS_DIR string
22-
LOG_LEVEL string
23-
PORT string
24-
API_URL string
25-
API_TOKENS []string
26-
SETTINGS map[string]*SETTING_
27-
INSECURE bool
28-
}
29-
30-
type SETTING_ struct {
31-
BLOCKED_ENDPOINTS []string `koanf:"blockedendpoints"`
32-
ALLOWED_ENDPOINTS []string `koanf:"allowedendpoints"`
33-
VARIABLES map[string]any `koanf:"variables"`
34-
DATA_ALIASES map[string][]middlewareTypes.DataAlias `koanf:"dataaliases"`
35-
MESSAGE_TEMPLATE string `koanf:"messagetemplate"`
36-
}
37-
38-
var ENV *ENV_ = &ENV_{
17+
var ENV *structure.ENV = &structure.ENV{
3918
CONFIG_PATH: os.Getenv("CONFIG_PATH"),
4019
DEFAULTS_PATH: os.Getenv("DEFAULTS_PATH"),
4120
TOKENS_DIR: os.Getenv("TOKENS_DIR"),
4221
FAVICON_PATH: os.Getenv("FAVICON_PATH"),
4322
API_TOKENS: []string{},
44-
SETTINGS: map[string]*SETTING_{},
23+
SETTINGS: map[string]*structure.SETTINGS{},
4524
INSECURE: false,
4625
}
4726

@@ -76,32 +55,36 @@ func InitEnv() {
7655

7756
ENV.API_URL = config.String("api.url")
7857

79-
var settings SETTING_
58+
var settings structure.SETTINGS
8059

81-
transformChildren(config, "settings.variables", transformVariables)
60+
transformChildren(config, "settings.message.variables", transformVariables)
8261

8362
config.Unmarshal("settings", &settings)
8463

8564
ENV.SETTINGS["*"] = &settings
8665
}
8766

8867
func LoadDefaults() {
89-
_, defErr := LoadFile(ENV.DEFAULTS_PATH, defaultsLayer, yaml.Parser())
68+
_, err := LoadFile(ENV.DEFAULTS_PATH, defaultsLayer, yaml.Parser())
9069

91-
if defErr != nil {
70+
if err != nil {
9271
log.Warn("Could not Load Defaults", ENV.DEFAULTS_PATH)
9372
}
9473
}
9574

9675
func LoadConfig() {
97-
_, conErr := LoadFile(ENV.CONFIG_PATH, userLayer, yaml.Parser())
76+
_, err := LoadFile(ENV.CONFIG_PATH, userLayer, yaml.Parser())
9877

99-
if conErr != nil {
100-
_, err := os.Stat(ENV.CONFIG_PATH)
78+
if err != nil {
79+
_, fsErr := os.Stat(ENV.CONFIG_PATH)
10180

102-
if !errors.Is(err, fs.ErrNotExist) {
103-
log.Error("Could not Load Config ", ENV.CONFIG_PATH, ": ", conErr.Error())
81+
// Config File doesn't exist
82+
// => User is using Environment
83+
if errors.Is(fsErr, fs.ErrNotExist) {
84+
return
10485
}
86+
87+
log.Error("Could not Load Config ", ENV.CONFIG_PATH, ": ", err.Error())
10588
}
10689
}
10790

0 commit comments

Comments
 (0)