@@ -25,6 +25,7 @@ type ENV_ struct {
2525 PORT string
2626 API_URL string
2727 API_TOKENS []string
28+ INSECURE bool
2829 BLOCKED_ENDPOINTS []string
2930 VARIABLES map [string ]any
3031 MESSAGE_ALIASES []middlewares.MessageAlias
@@ -34,11 +35,17 @@ var ENV ENV_ = ENV_{
3435 CONFIG_PATH : os .Getenv ("CONFIG_PATH" ),
3536 DEFAULTS_PATH : os .Getenv ("DEFAULTS_PATH" ),
3637 TOKENS_DIR : os .Getenv ("TOKENS_DIR" ),
38+ API_TOKENS : []string {},
39+ BLOCKED_ENDPOINTS : []string {},
3740 MESSAGE_ALIASES : []middlewares.MessageAlias {},
3841 VARIABLES : map [string ]any {},
42+ INSECURE : false ,
3943}
4044
41- var config = koanf .New ("." )
45+ var defaultsLayer = koanf .New ("." )
46+ var userLayer = koanf .New ("." )
47+
48+ var config * koanf.Koanf
4249
4350func InitEnv () {
4451 ENV .PORT = strconv .Itoa (config .Int ("server.port" ))
@@ -49,32 +56,47 @@ func InitEnv() {
4956
5057 if len (apiTokens ) <= 0 {
5158 apiTokens = config .Strings ("api.token" )
59+
60+ if len (apiTokens ) <= 0 {
61+ log .Warn ("No API TOKEN provided this is NOT recommended" )
62+
63+ log .Info ("Disabling Security Features due to incomplete Congfiguration" )
64+
65+ ENV .INSECURE = true
66+
67+ // Set Blocked Endpoints on Config to User Layer Value
68+ // => effectively ignoring Default Layer
69+ config .Set ("blockedendpoints" , userLayer .Strings ("blockeendpoints" ))
70+ }
5271 }
5372
54- ENV .API_TOKENS = apiTokens
73+ if len (apiTokens ) > 0 {
74+ log .Debug ("Registered " + strconv .Itoa (len (apiTokens )) + " Tokens" )
5575
56- ENV .BLOCKED_ENDPOINTS = config .Strings ("blockedendpoints" )
76+ ENV .API_TOKENS = apiTokens
77+ }
5778
5879 config .Unmarshal ("messagealiases" , & ENV .MESSAGE_ALIASES )
59-
6080 config .Unmarshal ("variables" , & ENV .VARIABLES )
6181
6282 ENV .VARIABLES ["NUMBER" ] = config .String ("number" )
6383 ENV .VARIABLES ["RECIPIENTS" ] = config .Strings ("recipients" )
84+
85+ ENV .BLOCKED_ENDPOINTS = config .Strings ("blockedendpoints" )
6486}
6587
6688func Load () {
6789 log .Debug ("Loading Config " , ENV .DEFAULTS_PATH )
6890
69- defErr := LoadFile (ENV .DEFAULTS_PATH , yaml .Parser ())
91+ defPro , defErr := LoadFile (ENV .DEFAULTS_PATH , defaultsLayer , yaml .Parser ())
7092
7193 if defErr != nil {
7294 log .Warn ("Could not Load Defaults" , ENV .DEFAULTS_PATH )
7395 }
7496
7597 log .Debug ("Loading Config " , ENV .CONFIG_PATH )
7698
77- conErr := LoadFile (ENV .CONFIG_PATH , yaml .Parser ())
99+ conPro , conErr := LoadFile (ENV .CONFIG_PATH , userLayer , yaml .Parser ())
78100
79101 if conErr != nil {
80102 _ , err := os .Stat (ENV .CONFIG_PATH )
@@ -85,22 +107,25 @@ func Load() {
85107 }
86108
87109 log .Debug ("Loading DotEnv" )
88- LoadEnv ()
89110
90- normalizeKeys ()
111+ envPro , _ := LoadEnv (userLayer )
112+
113+ config = mergeLayers (defPro , conPro , envPro )
114+
115+ normalizeKeys (config )
91116
92117 InitEnv ()
93118
94119 log .Info ("Finished Loading Configuration" )
95120}
96121
97- func LoadFile (path string , parser koanf.Parser ) error {
122+ func LoadFile (path string , config * koanf. Koanf , parser koanf.Parser ) (koanf. Provider , error ) {
98123 f := file .Provider (path )
99124
100125 err := config .Load (f , parser )
101126
102127 if err != nil {
103- return err
128+ return nil , err
104129 }
105130
106131 f .Watch (func (event any , err error ) {
@@ -113,10 +138,10 @@ func LoadFile(path string, parser koanf.Parser) error {
113138 Load ()
114139 })
115140
116- return err
141+ return f , err
117142}
118143
119- func LoadEnv () error {
144+ func LoadEnv (config * koanf. Koanf ) (koanf. Provider , error ) {
120145 e := env .Provider ("." , env.Opt {
121146 TransformFunc : normalizeEnv ,
122147 })
@@ -127,10 +152,19 @@ func LoadEnv() error {
127152 log .Fatal ("Error loading env: " , err .Error ())
128153 }
129154
130- return err
155+ return e , err
156+ }
157+
158+ func mergeLayers (defPro koanf.Provider , conPro koanf.Provider , envPro koanf.Provider ) * koanf.Koanf {
159+ final := koanf .New ("." )
160+ _ = final .Load (defPro , nil )
161+ _ = final .Load (conPro , nil )
162+ _ = final .Load (envPro , nil )
163+
164+ return final
131165}
132166
133- func normalizeKeys () {
167+ func normalizeKeys (config * koanf. Koanf ) {
134168 data := map [string ]any {}
135169
136170 for _ , key := range config .Keys () {
0 commit comments