diff --git a/database/routines.go b/database/routines.go index c92d5116..0445c15c 100644 --- a/database/routines.go +++ b/database/routines.go @@ -17,8 +17,8 @@ var ( // Maintenance will automatically delete old records from 'failures' and 'hits' // this function is currently set to delete records 7+ days old every 60 minutes func Maintenance() { - dur := utils.GetenvAs("REMOVE_AFTER", "2160h").Duration() - interval := utils.GetenvAs("CLEANUP_INTERVAL", "1h").Duration() + dur := utils.Params.GetDuration("REMOVE_AFTER") + interval := utils.Params.GetDuration("CLEANUP_INTERVAL") log.Infof("Database Cleanup runs every %s and will remove records older than %s", interval.String(), dur.String()) ticker := interval diff --git a/handlers/prometheus.go b/handlers/prometheus.go index 9fcd62da..7c29e130 100644 --- a/handlers/prometheus.go +++ b/handlers/prometheus.go @@ -40,7 +40,7 @@ func hex2int(hexStr string) uint64 { func prometheusHandler(w http.ResponseWriter, r *http.Request) { promValues = []string{} - prefix = utils.Getenv("PREFIX", "").(string) + prefix = utils.Params.GetString("PREFIX") if prefix != "" { prefix = prefix + "_" } diff --git a/source/source.go b/source/source.go index 0c8f50df..cfea245d 100644 --- a/source/source.go +++ b/source/source.go @@ -75,7 +75,7 @@ func UsingAssets(folder string) bool { if _, err := os.Stat(folder + "/assets"); err == nil { return true } else { - useAssets := utils.Getenv("USE_ASSETS", false).(bool) + useAssets := utils.Params.GetBool("USE_ASSETS") if useAssets { log.Infoln("Environment variable USE_ASSETS was found.") diff --git a/types/configs/connection.go b/types/configs/connection.go index 84ae3bb3..e638ea39 100644 --- a/types/configs/connection.go +++ b/types/configs/connection.go @@ -39,8 +39,8 @@ func Connect(configs *DbConfig, retry bool) error { } } - apiKey := utils.Getenv("API_KEY", utils.RandomString(24)).(string) - apiSecret := utils.Getenv("API_SECRET", utils.RandomString(24)).(string) + apiKey := utils.Params.GetString("API_KEY") + apiSecret := utils.Params.GetString("API_SECRET") configs.ApiKey = apiKey configs.ApiSecret = apiSecret @@ -85,8 +85,8 @@ func CreateAdminUser(configs *DbConfig) error { log.Infoln(fmt.Sprintf("Core database does not exist, creating now!")) if configs.Username == "" && configs.Password == "" { - configs.Username = utils.Getenv("ADMIN_USER", "admin").(string) - configs.Password = utils.Getenv("ADMIN_PASSWORD", "admin").(string) + configs.Username = utils.Params.GetString("ADMIN_USER") + configs.Password = utils.Params.GetString("ADMIN_PASSWORD") } admin := &users.User{ diff --git a/types/core/samples.go b/types/core/samples.go index 6837eccf..34350ff3 100644 --- a/types/core/samples.go +++ b/types/core/samples.go @@ -6,14 +6,19 @@ import ( ) func Samples() error { - apiKey := utils.Getenv("API_KEY", utils.RandomString(16)) - apiSecret := utils.Getenv("API_SECRET", utils.RandomString(16)) + apiKey := utils.Params.GetString("API_KEY") + apiSecret := utils.Params.GetString("API_SECRET") + + if apiKey == "" || apiSecret == "" { + apiKey = utils.RandomString(32) + apiSecret = utils.RandomString(32) + } core := &Core{ Name: "Statping Sample Data", Description: "This data is only used to testing", - ApiKey: apiKey.(string), - ApiSecret: apiSecret.(string), + ApiKey: apiKey, + ApiSecret: apiSecret, Domain: "http://localhost:8080", CreatedAt: utils.Now(), UseCdn: null.NewNullBool(false), diff --git a/types/services/env.go b/types/services/env.go index 65c694f4..fdd34b76 100644 --- a/types/services/env.go +++ b/types/services/env.go @@ -19,7 +19,7 @@ func findServiceByHash(hash string) *Service { } func ServicesFromEnvFile() error { - servicesEnv := utils.Getenv("SERVICES_FILE", "").(string) + servicesEnv := utils.Params.GetString("SERVICES_FILE") if servicesEnv == "" { return nil } diff --git a/utils/configs.go b/utils/configs.go index c6415cc8..b3bfc5ba 100644 --- a/utils/configs.go +++ b/utils/configs.go @@ -42,7 +42,10 @@ func setDefaults() { Params.SetDefault("STATPING_DIR", Directory) Params.SetDefault("GO_ENV", "") Params.SetDefault("DISABLE_LOGS", false) + Params.SetDefault("USE_ASSETS", false) Params.SetDefault("BASE_PATH", "") + Params.SetDefault("ADMIN_USER", "admin") + Params.SetDefault("ADMIN_PASSWORD", "admin") Params.SetDefault("MAX_OPEN_CONN", 25) Params.SetDefault("MAX_IDLE_CONN", 25) Params.SetDefault("MAX_LIFE_CONN", 5*time.Minute) @@ -51,6 +54,8 @@ func setDefaults() { Params.SetDefault("ALLOW_REPORTS", false) Params.SetDefault("POSTGRES_SSLMODE", "disable") Params.SetDefault("SASS", "sass") + Params.SetDefault("REMOVE_AFTER", 2160*time.Hour) + Params.SetDefault("CLEANUP_INTERVAL", 1*time.Hour) dbConn := Params.GetString("DB_CONN") dbInt := Params.GetInt("DB_PORT") diff --git a/utils/log.go b/utils/log.go index 2b10d370..3c18d469 100644 --- a/utils/log.go +++ b/utils/log.go @@ -38,9 +38,9 @@ func SentryInit(v *string, allow bool) { } version = *v } - goEnv := Getenv("GO_ENV", "production").(string) - allowReports := Getenv("ALLOW_REPORTS", false).(bool) - if allowReports || allow { + goEnv := Params.GetString("GO_ENV") + allowReports := Params.GetBool("ALLOW_REPORTS") + if allowReports || allow || goEnv == "test" { if err := sentry.Init(sentry.ClientOptions{ Dsn: errorReporter, Environment: goEnv, diff --git a/utils/utils.go b/utils/utils.go index ba479825..2e0ef5ce 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -47,12 +47,6 @@ func NotNumber(val string) bool { return err != nil } -func GetenvAs(key string, defaultValue interface{}) *env { - return &env{ - data: Getenv(key, defaultValue), - } -} - func (e *env) Duration() time.Duration { t, err := time.ParseDuration(e.data.(string)) if err != nil { @@ -61,26 +55,6 @@ func (e *env) Duration() time.Duration { return t } -func Getenv(key string, defaultValue interface{}) interface{} { - if defaultValue != nil { - Params.SetDefault(key, defaultValue) - } - val := Params.Get(key) - if val != nil { - switch val.(type) { - case int, int64: - return Params.GetInt(key) - case time.Duration: - return Params.GetDuration(key) - case bool: - return Params.GetBool(key) - default: - return val - } - } - return defaultValue -} - // ToInt converts a int to a string func ToInt(s interface{}) int64 { switch v := s.(type) {