@@ -40,6 +40,14 @@ type (
4040 AllowedOrigins []string `env-required:"true" yaml:"allowed_origins" env:"HTTP_ALLOWED_ORIGINS"`
4141 AllowedHeaders []string `env-required:"true" yaml:"allowed_headers" env:"HTTP_ALLOWED_HEADERS"`
4242 WSCompression bool `yaml:"ws_compression" env:"WS_COMPRESSION"`
43+ TLS TLS `yaml:"tls"`
44+ }
45+
46+ // TLS -.
47+ TLS struct {
48+ Enabled bool `yaml:"enabled" env:"HTTP_TLS_ENABLED"`
49+ CertFile string `yaml:"certFile" env:"HTTP_TLS_CERT_FILE"`
50+ KeyFile string `yaml:"keyFile" env:"HTTP_TLS_KEY_FILE"`
4351 }
4452
4553 // Log -.
@@ -85,10 +93,9 @@ type (
8593 }
8694)
8795
88- // NewConfig returns app config.
89- func NewConfig () (* Config , error ) {
90- // set defaults
91- ConsoleConfig = & Config {
96+ // defaultConfig constructs the in-memory default configuration.
97+ func defaultConfig () * Config {
98+ return & Config {
9299 App : App {
93100 Name : "console" ,
94101 Repo : "device-management-toolkit/console" ,
@@ -102,6 +109,11 @@ func NewConfig() (*Config, error) {
102109 AllowedOrigins : []string {"*" },
103110 AllowedHeaders : []string {"*" },
104111 WSCompression : true ,
112+ TLS : TLS {
113+ Enabled : true ,
114+ CertFile : "" ,
115+ KeyFile : "" ,
116+ },
105117 },
106118 Log : Log {
107119 Level : "info" ,
@@ -135,59 +147,86 @@ func NewConfig() (*Config, error) {
135147 },
136148 },
137149 }
150+ }
138151
139- // Define a command line flag for the config path
140- var configPathFlag string
141- if flag . Lookup ( "config" ) == nil {
142- flag . StringVar ( & configPathFlag , "config" , "" , "path to config file" )
152+ // resolveConfigPath determines the effective config file path based on a flag value or default location.
153+ func resolveConfigPath ( configPathFlag string ) ( string , error ) {
154+ if configPathFlag != "" {
155+ return configPathFlag , nil
143156 }
144157
145- flag .Parse ()
158+ ex , err := os .Executable ()
159+ if err != nil {
160+ return "" , err
161+ }
146162
147- // Determine the config path
148- var configPath string
149- if configPathFlag != "" {
150- configPath = configPathFlag
151- } else {
152- ex , err := os .Executable ()
153- if err != nil {
154- panic (err )
155- }
163+ exPath := filepath .Dir (ex )
156164
157- exPath := filepath .Dir (ex )
165+ return filepath .Join (exPath , "config" , "config.yml" ), nil
166+ }
158167
159- configPath = filepath .Join (exPath , "config" , "config.yml" )
168+ // readOrInitConfig attempts to read the config file; if it doesn't exist, writes the provided cfg to disk.
169+ func readOrInitConfig (configPath string , cfg * Config ) error {
170+ err := cleanenv .ReadConfig (configPath , cfg )
171+ if err == nil {
172+ return nil
160173 }
161174
162- err := cleanenv .ReadConfig (configPath , ConsoleConfig )
163-
164175 var pathErr * os.PathError
165-
166176 if errors .As (err , & pathErr ) {
167177 // Write config file out to disk
168178 configDir := filepath .Dir (configPath )
169- if err := os .MkdirAll (configDir , os .ModePerm ); err != nil {
170- return nil , err
179+ if mkErr := os .MkdirAll (configDir , os .ModePerm ); mkErr != nil {
180+ return mkErr
171181 }
172182
173- file , err := os .Create (configPath )
174- if err != nil {
175- return nil , err
183+ file , cErr := os .Create (configPath )
184+ if cErr != nil {
185+ return cErr
176186 }
177187 defer file .Close ()
178188
179189 encoder := yaml .NewEncoder (file )
180190 defer encoder .Close ()
181191
182- if err := encoder .Encode (ConsoleConfig ); err != nil {
183- return nil , err
192+ if encErr := encoder .Encode (cfg ); encErr != nil {
193+ return encErr
184194 }
195+
196+ return nil
185197 }
186198
187- err = cleanenv .ReadEnv (ConsoleConfig )
199+ return err
200+ }
201+
202+ // NewConfig returns app config.
203+ func NewConfig () (* Config , error ) {
204+ // set defaults
205+ ConsoleConfig = defaultConfig ()
206+
207+ // Define a command line flag for the config path
208+ var configPathFlag string
209+ if flag .Lookup ("config" ) == nil {
210+ flag .StringVar (& configPathFlag , "config" , "" , "path to config file" )
211+ }
212+
213+ if ! flag .Parsed () {
214+ flag .Parse ()
215+ }
216+
217+ // Determine the config path
218+ configPath , err := resolveConfigPath (configPathFlag )
188219 if err != nil {
189220 return nil , err
190221 }
191222
223+ if err := readOrInitConfig (configPath , ConsoleConfig ); err != nil {
224+ return nil , err
225+ }
226+
227+ if err := cleanenv .ReadEnv (ConsoleConfig ); err != nil {
228+ return nil , err
229+ }
230+
192231 return ConsoleConfig , nil
193232}
0 commit comments