Skip to content

Commit e0a7b3e

Browse files
authored
chore: restructure and move (#147)
1 parent 7839e08 commit e0a7b3e

File tree

12 files changed

+91
-96
lines changed

12 files changed

+91
-96
lines changed

internals/config/loader.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"io/fs"
66
"os"
7-
"strconv"
87
"strings"
98

109
"github.com/codeshelldev/gotl/pkg/configutils"
@@ -20,11 +19,12 @@ var ENV *structure.ENV = &structure.ENV{
2019
DEFAULTS_PATH: os.Getenv("DEFAULTS_PATH"),
2120
TOKENS_DIR: os.Getenv("TOKENS_DIR"),
2221
FAVICON_PATH: os.Getenv("FAVICON_PATH"),
23-
API_TOKENS: []string{},
24-
SETTINGS: map[string]*structure.SETTINGS{},
22+
CONFIGS: map[string]*structure.CONFIG{},
2523
INSECURE: false,
2624
}
2725

26+
var DEFAULT *structure.CONFIG
27+
2828
var defaultsConf *configutils.Config
2929
var userConf *configutils.Config
3030
var tokenConf *configutils.Config
@@ -86,7 +86,7 @@ func LowercaseKeys(config *configutils.Config) {
8686
}
8787

8888
func NormalizeConfig(config *configutils.Config) {
89-
Normalize(config, "settings", &structure.SETTINGS{})
89+
Normalize(config, "", &structure.CONFIG{})
9090
}
9191

9292
func Normalize(config *configutils.Config, path string, structure any) {
@@ -127,17 +127,13 @@ func InitReload() {
127127
}
128128

129129
func InitEnv() {
130-
ENV.PORT = strconv.Itoa(mainConf.Layer.Int("service.port"))
131-
132-
ENV.LOG_LEVEL = strings.ToLower(mainConf.Layer.String("loglevel"))
133-
134-
ENV.API_URL = mainConf.Layer.String("api.url")
130+
var config structure.CONFIG
135131

136-
var settings structure.SETTINGS
132+
mainConf.Layer.Unmarshal("", &config)
137133

138-
mainConf.Layer.Unmarshal("settings", &settings)
134+
ENV.CONFIGS["*"] = &config
139135

140-
ENV.SETTINGS["*"] = &settings
136+
DEFAULT = ENV.CONFIGS["*"]
141137
}
142138

143139
func LoadDefaults() {
@@ -165,11 +161,3 @@ func LoadConfig() {
165161
log.Error("Could not Load Config ", ENV.CONFIG_PATH, ": ", err.Error())
166162
}
167163
}
168-
169-
func normalizeEnv(key string, value string) (string, any) {
170-
key = strings.ToLower(key)
171-
key = strings.ReplaceAll(key, "__", ".")
172-
key = strings.ReplaceAll(key, "_", "")
173-
174-
return key, stringutils.ToType(value)
175-
}

internals/config/parser.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
)
66

77
var transformFuncs = map[string]func(string, any) (string, any) {
8-
"default": defaultTransform,
8+
"default": lowercaseTransform,
99
"lower": lowercaseTransform,
1010
"upper": uppercaseTransform,
11+
"keep": keepTransform,
1112
}
1213

13-
func defaultTransform(key string, value any) (string, any) {
14+
func keepTransform(key string, value any) (string, any) {
1415
return key, value
1516
}
1617

internals/config/structure/structure.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,49 @@ type ENV struct {
55
DEFAULTS_PATH string
66
FAVICON_PATH string
77
TOKENS_DIR string
8-
LOG_LEVEL string
9-
PORT string
10-
API_URL string
11-
API_TOKENS []string
12-
SETTINGS map[string]*SETTINGS `koanf:"settings"`
138
INSECURE bool
9+
10+
CONFIGS map[string]*CONFIG
11+
}
12+
13+
type CONFIG struct {
14+
SERVICE SERVICE `koanf:"service"`
15+
API API `koanf:"api"`
16+
SETTINGS SETTINGS `koanf:"settings" aliases:"overrides"`
17+
}
18+
19+
type SERVICE struct {
20+
PORT string `koanf:"port"`
21+
LOG_LEVEL string `koanf:"loglevel"`
22+
}
23+
24+
type API struct {
25+
URL string `koanf:"url"`
26+
TOKENS []string `koanf:"tokens" aliases:"token"`
1427
}
1528

1629
type SETTINGS struct {
17-
ACCESS ACCESS_SETTINGS `koanf:"access" transform:"lower"`
18-
MESSAGE MESSAGE_SETTINGS `koanf:"message" transform:"lower"`
30+
ACCESS ACCESS_SETTINGS `koanf:"access"`
31+
MESSAGE MESSAGE_SETTINGS `koanf:"message"`
1932
}
2033

2134
type MESSAGE_SETTINGS struct {
22-
VARIABLES map[string]any `koanf:"variables" childtransform:"upper"`
23-
FIELD_MAPPINGS map[string][]FieldMapping `koanf:"fieldmappings" childtransform:"default"`
24-
TEMPLATE string `koanf:"template" transform:"lower"`
35+
VARIABLES map[string]any `koanf:"variables" childtransform:"upper"`
36+
FIELD_MAPPINGS map[string][]FieldMapping `koanf:"fieldmappings" childtransform:"default"`
37+
TEMPLATE string `koanf:"template"`
2538
}
2639

2740
type FieldMapping struct {
28-
Field string `koanf:"field" transform:"lower"`
29-
Score int `koanf:"score" transform:"lower"`
41+
Field string `koanf:"field"`
42+
Score int `koanf:"score"`
3043
}
3144

3245
type ACCESS_SETTINGS struct {
33-
ENDPOINTS []string `koanf:"endpoints" transform:"lower"`
34-
FIELD_POLICIES map[string]FieldPolicy `koanf:"fieldpolicies" transform:"lower" childtransform:"default"`
46+
ENDPOINTS []string `koanf:"endpoints"`
47+
FIELD_POLICIES map[string]FieldPolicy `koanf:"fieldpolicies" childtransform:"default"`
3548
}
3649

3750
type FieldPolicy struct {
38-
Value any `koanf:"value" transform:"lower"`
39-
Action string `koanf:"action" transform:"lower"`
51+
Value any `koanf:"value"`
52+
Action string `koanf:"action"`
4053
}

internals/config/tokens.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ import (
99
"github.com/knadh/koanf/parsers/yaml"
1010
)
1111

12-
type TOKEN_CONFIG struct {
13-
TOKENS []string `koanf:"tokens"`
14-
OVERRIDES structure.SETTINGS `koanf:"overrides"`
15-
}
16-
1712
func LoadTokens() {
1813
log.Debug("Loading Configs in ", ENV.TOKENS_DIR)
1914

@@ -45,16 +40,16 @@ func NormalizeTokens() {
4540
func InitTokens() {
4641
apiTokens := mainConf.Layer.Strings("api.tokens")
4742

48-
var tokenConfigs []TOKEN_CONFIG
43+
var tokenConfigs []structure.CONFIG
4944

5045
tokenConf.Layer.Unmarshal("tokenconfigs", &tokenConfigs)
5146

52-
overrides := parseTokenConfigs(tokenConfigs)
47+
config := parseTokenConfigs(tokenConfigs)
5348

54-
for token, override := range overrides {
49+
for token, config := range config {
5550
apiTokens = append(apiTokens, token)
5651

57-
ENV.SETTINGS[token] = &override
52+
ENV.CONFIGS[token] = &config
5853
}
5954

6055
if len(apiTokens) <= 0 {
@@ -71,19 +66,17 @@ func InitTokens() {
7166

7267
if len(apiTokens) > 0 {
7368
log.Debug("Registered " + strconv.Itoa(len(apiTokens)) + " Tokens")
74-
75-
ENV.API_TOKENS = apiTokens
7669
}
7770
}
7871

79-
func parseTokenConfigs(configs []TOKEN_CONFIG) map[string]structure.SETTINGS {
80-
settings := map[string]structure.SETTINGS{}
72+
func parseTokenConfigs(configArray []structure.CONFIG) map[string]structure.CONFIG {
73+
configs := map[string]structure.CONFIG{}
8174

82-
for _, config := range configs {
83-
for _, token := range config.TOKENS {
84-
settings[token] = config.OVERRIDES
75+
for _, config := range configArray {
76+
for _, token := range config.API.TOKENS {
77+
configs[token] = config
8578
}
8679
}
8780

88-
return settings
81+
return configs
8982
}

internals/proxy/middlewares/auth.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package middlewares
33
import (
44
"context"
55
"encoding/base64"
6+
"maps"
67
"net/http"
78
"slices"
89
"strings"
@@ -17,7 +18,12 @@ var Auth Middleware = Middleware{
1718
}
1819

1920
func authHandler(next http.Handler) http.Handler {
20-
tokens := config.ENV.API_TOKENS
21+
tokenKeys := maps.Keys(config.ENV.CONFIGS)
22+
tokens := slices.Collect(tokenKeys)
23+
24+
if tokens == nil {
25+
tokens = []string{}
26+
}
2127

2228
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
2329
if len(tokens) <= 0 {

internals/proxy/middlewares/common.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,18 @@ type contextKey string
2424

2525
const tokenKey contextKey = "token"
2626

27-
func getSettingsByReq(req *http.Request) *structure.SETTINGS {
28-
token, ok := req.Context().Value(tokenKey).(string)
27+
func getConfigByReq(req *http.Request) *structure.CONFIG {
28+
token := req.Context().Value(tokenKey).(string)
2929

30-
if !ok {
31-
token = "*"
32-
}
33-
34-
return getSettings(token)
30+
return getConfig(token)
3531
}
3632

37-
func getSettings(token string) *structure.SETTINGS {
38-
settings, exists := config.ENV.SETTINGS[token]
33+
func getConfig(token string) *structure.CONFIG {
34+
conf, exists := config.ENV.CONFIGS[token]
3935

40-
if !exists || settings == nil {
41-
settings = config.ENV.SETTINGS["*"]
36+
if !exists || conf == nil {
37+
conf = config.DEFAULT
4238
}
4339

44-
return settings
40+
return conf
4541
}

internals/proxy/middlewares/endpoints.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ var Endpoints Middleware = Middleware{
1616

1717
func endpointsHandler(next http.Handler) http.Handler {
1818
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
19-
settings := getSettingsByReq(req)
19+
conf := getConfigByReq(req)
2020

21-
endpoints := settings.ACCESS.ENDPOINTS
21+
endpoints := conf.SETTINGS.ACCESS.ENDPOINTS
2222

2323
if endpoints == nil {
24-
endpoints = getSettings("*").ACCESS.ENDPOINTS
24+
endpoints = getConfig("").SETTINGS.ACCESS.ENDPOINTS
2525
}
2626

2727
reqPath := req.URL.Path

internals/proxy/middlewares/mapping.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ var Mapping Middleware = Middleware{
1616

1717
func mappingHandler(next http.Handler) http.Handler {
1818
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
19-
settings := getSettingsByReq(req)
19+
conf := getConfigByReq(req)
2020

21-
fieldMappings := settings.MESSAGE.FIELD_MAPPINGS
21+
variables := conf.SETTINGS.MESSAGE.VARIABLES
22+
fieldMappings := conf.SETTINGS.MESSAGE.FIELD_MAPPINGS
2223

2324
if fieldMappings == nil {
24-
fieldMappings = getSettings("*").MESSAGE.FIELD_MAPPINGS
25+
fieldMappings = getConfig("").SETTINGS.MESSAGE.FIELD_MAPPINGS
2526
}
2627

27-
if settings.MESSAGE.VARIABLES == nil {
28-
settings.MESSAGE.VARIABLES = getSettings("*").MESSAGE.VARIABLES
28+
if variables == nil {
29+
variables = getConfig("").SETTINGS.MESSAGE.VARIABLES
2930
}
3031

3132
body, err := request.GetReqBody(req)
@@ -53,7 +54,7 @@ func mappingHandler(next http.Handler) http.Handler {
5354
bodyData[keyWithoutPrefix] = value
5455
modifiedBody = true
5556
case ".":
56-
settings.MESSAGE.VARIABLES[keyWithoutPrefix] = value
57+
variables[keyWithoutPrefix] = value
5758
}
5859
}
5960
}

internals/proxy/middlewares/message.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ var Message Middleware = Middleware{
1414

1515
func messageHandler(next http.Handler) http.Handler {
1616
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
17-
settings := getSettingsByReq(req)
17+
conf := getConfigByReq(req)
1818

19-
variables := settings.MESSAGE.VARIABLES
20-
messageTemplate := settings.MESSAGE.TEMPLATE
19+
variables := conf.SETTINGS.MESSAGE.VARIABLES
20+
messageTemplate := conf.SETTINGS.MESSAGE.TEMPLATE
2121

2222
if variables == nil {
23-
variables = getSettings("*").MESSAGE.VARIABLES
23+
variables = getConfig("").SETTINGS.MESSAGE.VARIABLES
2424
}
2525

2626
if messageTemplate == "" {
27-
messageTemplate = getSettings("*").MESSAGE.TEMPLATE
27+
messageTemplate = getConfig("").SETTINGS.MESSAGE.TEMPLATE
2828
}
2929

3030
body, err := request.GetReqBody(req)

internals/proxy/middlewares/policy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ var Policy Middleware = Middleware{
1818

1919
func policyHandler(next http.Handler) http.Handler {
2020
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
21-
settings := getSettingsByReq(req)
21+
conf := getConfigByReq(req)
2222

23-
policies := settings.ACCESS.FIELD_POLICIES
23+
policies := conf.SETTINGS.ACCESS.FIELD_POLICIES
2424

2525
if policies == nil {
26-
policies = getSettings("*").ACCESS.FIELD_POLICIES
26+
policies = getConfig("").SETTINGS.ACCESS.FIELD_POLICIES
2727
}
2828

2929
body, err := request.GetReqBody(req)

0 commit comments

Comments
 (0)