2022-08-22 18:51:46 -06:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/analogj/go-util/utils"
|
2022-08-22 19:11:00 -06:00
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/errors"
|
2022-08-22 18:51:46 -06:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// When initializing this class the following methods must be called:
|
|
|
|
// Config.New
|
|
|
|
// Config.Init
|
|
|
|
// This is done automatically when created via the Factory.
|
|
|
|
type configuration struct {
|
|
|
|
*viper.Viper
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *configuration) Init() error {
|
|
|
|
c.Viper = viper.New()
|
|
|
|
//set defaults
|
|
|
|
c.SetDefault("web.listen.port", "8080")
|
|
|
|
c.SetDefault("web.listen.host", "0.0.0.0")
|
|
|
|
c.SetDefault("web.listen.basepath", "")
|
2022-12-21 20:51:02 -07:00
|
|
|
c.SetDefault("web.allow_unsafe_endpoints", false)
|
|
|
|
|
2022-08-22 19:11:00 -06:00
|
|
|
c.SetDefault("web.src.frontend.path", "/opt/fasten/web")
|
2023-07-08 08:43:30 -06:00
|
|
|
c.SetDefault("dashboard.location", []string{})
|
2022-08-22 18:51:46 -06:00
|
|
|
|
2023-01-26 04:10:36 -07:00
|
|
|
c.SetDefault("database.location", "/opt/fasten/db/fasten.db")
|
2022-10-08 20:40:33 -06:00
|
|
|
|
2022-11-02 01:12:54 -06:00
|
|
|
c.SetDefault("jwt.issuer.key", "thisismysupersecuressessionsecretlength")
|
|
|
|
|
2022-08-22 18:51:46 -06:00
|
|
|
c.SetDefault("log.level", "INFO")
|
|
|
|
c.SetDefault("log.file", "")
|
|
|
|
|
|
|
|
//set the default system config file search path.
|
|
|
|
//if you want to load a non-standard location system config file (~/capsule.yml), use ReadConfig
|
|
|
|
//if you want to load a repo specific config file, use ReadConfig
|
|
|
|
c.SetConfigType("yaml")
|
|
|
|
c.SetConfigName("template")
|
|
|
|
c.AddConfigPath("$HOME/")
|
|
|
|
|
|
|
|
//configure env variable parsing.
|
2022-08-22 19:11:00 -06:00
|
|
|
c.SetEnvPrefix("FASTEN")
|
2022-08-22 18:51:46 -06:00
|
|
|
c.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
|
|
|
|
c.AutomaticEnv()
|
|
|
|
//CLI options will be added via the `Set()` function
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *configuration) ReadConfig(configFilePath string) error {
|
|
|
|
|
|
|
|
if !utils.FileExists(configFilePath) {
|
|
|
|
message := fmt.Sprintf("The configuration file (%s) could not be found. Skipping", configFilePath)
|
|
|
|
log.Printf(message)
|
|
|
|
return errors.ConfigFileMissingError("The configuration file could not be found.")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Loading configuration file: %s", configFilePath)
|
|
|
|
|
|
|
|
config_data, err := os.Open(configFilePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error reading configuration file: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = c.MergeConfig(config_data)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error merging config file: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
2022-09-14 09:19:28 -06:00
|
|
|
return c.ValidateConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function ensures that required configuration keys (that must be manually set) are present
|
|
|
|
func (c *configuration) ValidateConfig() error {
|
|
|
|
|
2022-08-22 18:51:46 -06:00
|
|
|
return nil
|
|
|
|
}
|