Skip to content

Commit e8913e9

Browse files
committed
added yml configs (via koanf) + env overrides + defaults.yml file (TESTING)
1 parent 1c46123 commit e8913e9

File tree

12 files changed

+256
-139
lines changed

12 files changed

+256
-139
lines changed

Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
FROM alpine:latest
22
RUN apk --no-cache add ca-certificates
33

4-
ENV PORT=8880
4+
ENV SERVER_PORT=8880
5+
ENV CONFIG_PATH=/config/config.yml
6+
ENV DEFAULTS_PATH=/app/utils/config/defaults.yml
7+
ENV TOKENS_DIR=/config/tokens
58

69
ARG TARGETOS
710
ARG TARGETARCH

docker-compose.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ services:
2020
aliases:
2121
- secured-signal-api
2222
environment:
23-
SIGNAL_API_URL: http://signal-api:8080
24-
DEFAULT_RECIPIENTS: '[ "000", "001", "002" ]'
23+
API__URL: http://signal-api:8080
24+
RECIPIENTS: 000,001,002
2525
NUMBER: 123456789
26-
API_TOKEN: LOOOOOONG_STRING
26+
API__TOKENS: LOOOOOONG_STRING
2727
ports:
2828
- "8880:8880"
2929
restart: unless-stopped

examples/traefik.docker-compose.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ services:
88
aliases:
99
- secured-signal-api
1010
environment:
11-
SIGNAL_API_URL: http://signal-api:8080
12-
DEFAULT_RECIPIENTS: '[ "000", "001", "002" ]'
11+
API__URL: http://signal-api:8080
12+
DEFAULT_RECIPIENTS: 000,001,002
1313
NUMBER: 123456789
14-
API_TOKEN: LOOOOOONG_STRING
14+
API__TOKENS: LOOOOOONG_STRING
1515
labels:
1616
- traefik.enable=true
1717
- traefik.http.routers.signal-api.rule=Host(`signal-api.mydomain.com`)

go.mod

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@ module github.com/codeshelldev/secured-signal-api
22

33
go 1.25.1
44

5-
require go.uber.org/zap v1.27.0
5+
require (
6+
go.uber.org/zap v1.27.0
7+
gopkg.in/yaml.v3 v3.0.1
8+
)
69

7-
require go.uber.org/multierr v1.11.0 // indirect
10+
require (
11+
github.com/fsnotify/fsnotify v1.9.0 // indirect
12+
github.com/go-viper/mapstructure/v2 v2.3.0 // indirect
13+
github.com/joho/godotenv v1.5.1 // indirect
14+
github.com/knadh/koanf/maps v0.1.2 // indirect
15+
github.com/knadh/koanf/parsers/dotenv v1.1.0 // indirect
16+
github.com/knadh/koanf/parsers/yaml v1.1.0 // indirect
17+
github.com/knadh/koanf/providers/confmap v1.0.0
18+
github.com/knadh/koanf/providers/env v1.1.0 // indirect
19+
github.com/knadh/koanf/providers/file v1.2.0 // indirect
20+
github.com/knadh/koanf/v2 v2.2.2 // indirect
21+
github.com/mitchellh/copystructure v1.2.0 // indirect
22+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
23+
go.uber.org/multierr v1.11.0 // indirect
24+
go.yaml.in/yaml/v3 v3.0.4 // indirect
25+
golang.org/x/sys v0.36.0 // indirect
26+
)

go.sum

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
4+
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
5+
github.com/go-viper/mapstructure/v2 v2.3.0 h1:27XbWsHIqhbdR5TIC911OfYvgSaW93HM+dX7970Q7jk=
6+
github.com/go-viper/mapstructure/v2 v2.3.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
7+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
8+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
9+
github.com/knadh/koanf/maps v0.1.2 h1:RBfmAW5CnZT+PJ1CVc1QSJKf4Xu9kxfQgYVQSu8hpbo=
10+
github.com/knadh/koanf/maps v0.1.2/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
11+
github.com/knadh/koanf/parsers/dotenv v1.1.0 h1:dQaM0Jw54zRsqDcaJ27pciNExuKfOXagCJW3K1h0hj0=
12+
github.com/knadh/koanf/parsers/dotenv v1.1.0/go.mod h1:P3BQjxaIc2+SZ3n9BUceqYl95pz3qaGqYTZX0j0d/DI=
13+
github.com/knadh/koanf/parsers/yaml v1.1.0 h1:3ltfm9ljprAHt4jxgeYLlFPmUaunuCgu1yILuTXRdM4=
14+
github.com/knadh/koanf/parsers/yaml v1.1.0/go.mod h1:HHmcHXUrp9cOPcuC+2wrr44GTUB0EC+PyfN3HZD9tFg=
15+
github.com/knadh/koanf/providers/confmap v1.0.0 h1:mHKLJTE7iXEys6deO5p6olAiZdG5zwp8Aebir+/EaRE=
16+
github.com/knadh/koanf/providers/confmap v1.0.0/go.mod h1:txHYHiI2hAtF0/0sCmcuol4IDcuQbKTybiB1nOcUo1A=
17+
github.com/knadh/koanf/providers/env v1.1.0 h1:U2VXPY0f+CsNDkvdsG8GcsnK4ah85WwWyJgef9oQMSc=
18+
github.com/knadh/koanf/providers/env v1.1.0/go.mod h1:QhHHHZ87h9JxJAn2czdEl6pdkNnDh/JS1Vtsyt65hTY=
19+
github.com/knadh/koanf/providers/file v1.2.0 h1:hrUJ6Y9YOA49aNu/RSYzOTFlqzXSCpmYIDXI7OJU6+U=
20+
github.com/knadh/koanf/providers/file v1.2.0/go.mod h1:bp1PM5f83Q+TOUu10J/0ApLBd9uIzg+n9UgthfY+nRA=
21+
github.com/knadh/koanf/v2 v2.2.2 h1:ghbduIkpFui3L587wavneC9e3WIliCgiCgdxYO/wd7A=
22+
github.com/knadh/koanf/v2 v2.2.2/go.mod h1:abWQc0cBXLSF/PSOMCB/SK+T13NXDsPvOksbpi5e/9Q=
23+
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
24+
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
25+
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
26+
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
327
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
428
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
529
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
630
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
31+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
732
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
833
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
934
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
1035
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
1136
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
1237
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
38+
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
39+
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
40+
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
41+
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
42+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
43+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
44+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
1345
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1446
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internals/proxy/middlewares/body.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func getMessage(aliases []MessageAlias, data map[string]interface{}) (string, ma
9090
func processAlias(alias MessageAlias, data map[string]interface{}) (string, int, bool) {
9191
aliasKey := alias.Alias
9292

93-
value, ok := utils.GetJsonByPath(aliasKey, data)
93+
value, ok := utils.GetByPath(aliasKey, data)
9494

9595
aliasValue, isStr := value.(string)
9696

main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ import (
77

88
proxy "github.com/codeshelldev/secured-signal-api/internals/proxy"
99
middlewares "github.com/codeshelldev/secured-signal-api/internals/proxy/middlewares"
10+
config "github.com/codeshelldev/secured-signal-api/utils/config"
1011
docker "github.com/codeshelldev/secured-signal-api/utils/docker"
11-
env "github.com/codeshelldev/secured-signal-api/utils/env"
1212
log "github.com/codeshelldev/secured-signal-api/utils/logger"
1313
)
1414

1515
var initHandler *httputil.ReverseProxy
1616

17-
var ENV env.ENV_
17+
var ENV config.ENV_
1818

1919
func main() {
2020
logLevel := os.Getenv("LOG_LEVEL")
2121

2222
log.Init(logLevel)
2323

24-
env.Load()
24+
config.Load()
2525

26-
ENV = env.ENV
26+
ENV = config.ENV
2727

2828
initHandler = proxy.Create(ENV.API_URL)
2929

tests/json_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestJsonPath(t *testing.T) {
107107
key := c.key
108108
expected := c.expected
109109

110-
got, ok := utils.GetJsonByPath(key, data)
110+
got, ok := utils.GetByPath(key, data)
111111

112112
if !ok || got.(string) != expected {
113113
t.Error("Expected: ", key, " == ", expected, "; Got: ", got)

utils/config/config.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"strconv"
6+
"strings"
7+
8+
middlewares "github.com/codeshelldev/secured-signal-api/internals/proxy/middlewares"
9+
utils "github.com/codeshelldev/secured-signal-api/utils"
10+
log "github.com/codeshelldev/secured-signal-api/utils/logger"
11+
12+
"github.com/knadh/koanf/parsers/dotenv"
13+
"github.com/knadh/koanf/parsers/yaml"
14+
"github.com/knadh/koanf/providers/confmap"
15+
"github.com/knadh/koanf/providers/env"
16+
"github.com/knadh/koanf/providers/file"
17+
"github.com/knadh/koanf/v2"
18+
)
19+
20+
type ENV_ struct {
21+
CONFIG_PATH string
22+
DEFAULTS_PATH string
23+
TOKENS_DIR string
24+
PORT string
25+
API_URL string
26+
API_TOKENS []string
27+
BLOCKED_ENDPOINTS []string
28+
VARIABLES map[string]any
29+
MESSAGE_ALIASES []middlewares.MessageAlias
30+
}
31+
32+
var ENV ENV_ = ENV_{
33+
CONFIG_PATH: os.Getenv("CONFIG_PATH"),
34+
DEFAULTS_PATH: os.Getenv("DEFAULTS_PATH"),
35+
TOKENS_DIR: os.Getenv("TOKENS_DIR"),
36+
}
37+
38+
var config = koanf.New(".")
39+
40+
func LoadIntoENV() {
41+
ENV.PORT = strconv.Itoa(config.Int("server.port"))
42+
43+
ENV.API_URL = config.String("api.url")
44+
ENV.API_TOKENS = config.Strings("api.tokens")
45+
46+
ENV.BLOCKED_ENDPOINTS = config.Strings("blockedendpoints")
47+
48+
ENV.VARIABLES = config.Get("variables").(map[string]any)
49+
ENV.MESSAGE_ALIASES = config.Get("messagealiases").([]middlewares.MessageAlias)
50+
51+
ENV.VARIABLES["NUMBER"] = config.String("number")
52+
ENV.VARIABLES["RECIPIENTS"] = config.Strings("recipients")
53+
}
54+
55+
func Load() {
56+
LoadFile(ENV.DEFAULTS_PATH, yaml.Parser())
57+
LoadFile(ENV.CONFIG_PATH, yaml.Parser())
58+
59+
LoadDotEnv()
60+
61+
normalizeKeys()
62+
63+
LoadIntoENV()
64+
}
65+
66+
func LoadFile(path string, parser koanf.Parser) (*file.File) {
67+
f := file.Provider(path)
68+
69+
err := config.Load(f, parser)
70+
71+
if err != nil {
72+
log.Fatal("Error loading ", path, ": ", err.Error())
73+
}
74+
75+
f.Watch(func(event interface{}, err error) {
76+
if err != nil {
77+
return
78+
}
79+
80+
log.Info("Config changed, Reloading...")
81+
82+
Load()
83+
})
84+
85+
return f
86+
}
87+
88+
func LoadDotEnv() (*env.Env) {
89+
e := env.ProviderWithValue("", ".", normalizeEnv)
90+
91+
err := config.Load(e, dotenv.Parser())
92+
93+
if err != nil {
94+
log.Fatal("Error loading env: ", err.Error())
95+
}
96+
97+
return e
98+
}
99+
100+
func normalizeKeys() {
101+
data := map[string]any{}
102+
103+
for _, key := range config.Keys() {
104+
lower := strings.ToLower(key)
105+
106+
data[lower] = config.Get(key)
107+
}
108+
config.Load(confmap.Provider(data, "."), nil)
109+
}
110+
111+
func normalizeEnv(key string, value string) (string, any) {
112+
key = strings.ToLower(strings.ReplaceAll(key, "__", "."))
113+
114+
if strings.HasPrefix(value, "{") || strings.HasPrefix(value, "[") {
115+
data, err := utils.GetJsonSafe[any](value)
116+
117+
if data != nil && err == nil {
118+
return key, data
119+
}
120+
}
121+
122+
if strings.Contains(value, ",") {
123+
items := utils.StringToArray(value)
124+
125+
return key, items
126+
}
127+
128+
intValue, intErr := strconv.Atoi(value)
129+
130+
if intErr == nil {
131+
return key, intValue
132+
}
133+
134+
return key, value
135+
}

utils/config/defaults.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
server:
2+
port: 8880
3+
4+
api:
5+
tokens:
6+
url:
7+
8+
messageAliases:
9+
[
10+
{ alias: msg, score: 100 },
11+
{ alias: content, score: 99 },
12+
{ alias: description, score: 98 },
13+
{ alias: text, score: 20 },
14+
{ alias: body, score: 15 },
15+
{ alias: summary, score: 10 },
16+
{ alias: details, score: 9 },
17+
{ alias: payload, score: 2 },
18+
{ alias: data, score: 1 },
19+
]
20+
21+
blockedEndpoints:
22+
- /v1/about
23+
- /v1/configuration
24+
- /v1/devices
25+
- /v1/register
26+
- /v1/unregister
27+
- /v1/qrcodelink
28+
- /v1/accounts
29+
- /v1/contacts

0 commit comments

Comments
 (0)