Updating Config to a more protected access paradigm
This commit is contained in:
@@ -19,7 +19,11 @@ type StackConfiguration struct {
|
|||||||
|
|
||||||
var Environment = os.Getenv("STACK_ENVIRONMENT")
|
var Environment = os.Getenv("STACK_ENVIRONMENT")
|
||||||
|
|
||||||
var Config = StackConfiguration{}
|
var configInternal = StackConfiguration{}
|
||||||
|
|
||||||
|
func Config() StackConfiguration {
|
||||||
|
return configInternal
|
||||||
|
}
|
||||||
|
|
||||||
func GetConfigPath(filename string) string {
|
func GetConfigPath(filename string) string {
|
||||||
if Environment == "" {
|
if Environment == "" {
|
||||||
@@ -35,11 +39,11 @@ func LoadConfig() {
|
|||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
dec := json.NewDecoder(file)
|
dec := json.NewDecoder(file)
|
||||||
if err := dec.Decode(&Config); err != nil {
|
if err := dec.Decode(&configInternal); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
Config.ConfigLoaded = true
|
configInternal.ConfigLoaded = true
|
||||||
|
|
||||||
log.Printf("Loaded Config for stack " + Environment)
|
log.Printf("Loaded Config for stack " + Environment)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,52 +16,52 @@ func TestAllConfigs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SingleStackTest(t *testing.T, stack string, expected StackConfiguration) {
|
func SingleStackTest(t *testing.T, stack string, expected StackConfiguration) {
|
||||||
Config = StackConfiguration{}
|
configInternal = StackConfiguration{}
|
||||||
|
|
||||||
if Config.ConfigLoaded {
|
if Config().ConfigLoaded {
|
||||||
t.Errorf("Config.ConfigLoaded should be false before any processing")
|
t.Errorf("Config.ConfigLoaded should be false before any processing")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(Config.AdminEmails) > 0 ||
|
if len(Config().AdminEmails) > 0 ||
|
||||||
Config.AdminHmacEnv != "" ||
|
Config().AdminHmacEnv != "" ||
|
||||||
Config.UserHmacEnv != "" ||
|
Config().UserHmacEnv != "" ||
|
||||||
Config.AllowFreshAdminGeneration ||
|
Config().AllowFreshAdminGeneration ||
|
||||||
Config.AuthedRateLimitConfig != "" ||
|
Config().AuthedRateLimitConfig != "" ||
|
||||||
Config.UnauthedRateLimitConfig != "" { // Extend this IF for any other config values
|
Config().UnauthedRateLimitConfig != "" { // Extend this IF for any other config values
|
||||||
t.Errorf("Config already has values before loading")
|
t.Errorf("Config already has values before loading")
|
||||||
}
|
}
|
||||||
|
|
||||||
Environment = stack
|
Environment = stack
|
||||||
LoadConfig()
|
LoadConfig()
|
||||||
|
|
||||||
if !Config.ConfigLoaded {
|
if !Config().ConfigLoaded {
|
||||||
t.Errorf("Config was not set to loaded")
|
t.Errorf("Config was not set to loaded")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally test values
|
// Finally test values
|
||||||
if Config.AllowFreshAdminGeneration != expected.AllowFreshAdminGeneration {
|
if Config().AllowFreshAdminGeneration != expected.AllowFreshAdminGeneration {
|
||||||
t.Errorf("AllowFreshAdminGeneration value not set properly")
|
t.Errorf("AllowFreshAdminGeneration value not set properly")
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, email := range Config.AdminEmails {
|
for i, email := range Config().AdminEmails {
|
||||||
if expected.AdminEmails[i] != email {
|
if expected.AdminEmails[i] != email {
|
||||||
t.Errorf("AdminEmails value not set properly, expected %v at %v, was %v", expected.AdminEmails[i], i, email)
|
t.Errorf("AdminEmails value not set properly, expected %v at %v, was %v", expected.AdminEmails[i], i, email)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if Config.AdminHmacEnv != expected.AdminHmacEnv {
|
if Config().AdminHmacEnv != expected.AdminHmacEnv {
|
||||||
t.Errorf("AdminHmacEnv value not set properly")
|
t.Errorf("AdminHmacEnv value not set properly")
|
||||||
}
|
}
|
||||||
|
|
||||||
if Config.UserHmacEnv != expected.UserHmacEnv {
|
if Config().UserHmacEnv != expected.UserHmacEnv {
|
||||||
t.Errorf("UserHmacEnv value not set properly")
|
t.Errorf("UserHmacEnv value not set properly")
|
||||||
}
|
}
|
||||||
|
|
||||||
if Config.AuthedRateLimitConfig != expected.AuthedRateLimitConfig {
|
if Config().AuthedRateLimitConfig != expected.AuthedRateLimitConfig {
|
||||||
t.Errorf("AuthedRateLimitConfig value not set properly")
|
t.Errorf("AuthedRateLimitConfig value not set properly")
|
||||||
}
|
}
|
||||||
|
|
||||||
if Config.UnauthedRateLimitConfig != expected.UnauthedRateLimitConfig {
|
if Config().UnauthedRateLimitConfig != expected.UnauthedRateLimitConfig {
|
||||||
t.Errorf("UnauthedRateLimitConfig value not set properly")
|
t.Errorf("UnauthedRateLimitConfig value not set properly")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -493,7 +493,7 @@ func StarterAdmin() gin.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go util.SendEmail("Admin Created", "A new admin, "+a.Email+", has been created", config.Config.AdminEmails)
|
go util.SendEmail("Admin Created", "A new admin, "+a.Email+", has been created", config.Config().AdminEmails)
|
||||||
|
|
||||||
c.JSON(http.StatusOK, util.NextMsg{Next: "db verify"})
|
c.JSON(http.StatusOK, util.NextMsg{Next: "db verify"})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ var unauthLoaded = false
|
|||||||
func UnauthRateLimit() gin.HandlerFunc {
|
func UnauthRateLimit() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
if !unauthLoaded {
|
if !unauthLoaded {
|
||||||
unauthed.loadFromConfig(config.GetConfigPath(config.Config.UnauthedRateLimitConfig))
|
unauthed.loadFromConfig(config.GetConfigPath(config.Config().UnauthedRateLimitConfig))
|
||||||
unauthLoaded = true
|
unauthLoaded = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,7 +145,7 @@ var authLoaded = false
|
|||||||
func AuthedRateLimit() gin.HandlerFunc {
|
func AuthedRateLimit() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
if !authLoaded {
|
if !authLoaded {
|
||||||
authed.loadFromConfig(config.GetConfigPath(config.Config.AuthedRateLimitConfig))
|
authed.loadFromConfig(config.GetConfigPath(config.Config().AuthedRateLimitConfig))
|
||||||
authLoaded = true
|
authLoaded = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -44,7 +44,7 @@ func main() {
|
|||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
// Fresh admin functionality
|
// Fresh admin functionality
|
||||||
if config.Config.AllowFreshAdminGeneration {
|
if config.Config().AllowFreshAdminGeneration {
|
||||||
var adminCount int64
|
var adminCount int64
|
||||||
database.Db.Model(models.Admin{}).Count(&adminCount)
|
database.Db.Model(models.Admin{}).Count(&adminCount)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user