authorizer/server/envstore/store.go

121 lines
3.1 KiB
Go
Raw Normal View History

2022-01-17 06:02:13 +00:00
package envstore
import (
"sync"
"github.com/authorizerdev/authorizer/server/constants"
)
var (
// ARG_DB_URL is the cli arg variable for the database url
ARG_DB_URL *string
// ARG_DB_TYPE is the cli arg variable for the database type
ARG_DB_TYPE *string
// ARG_ENV_FILE is the cli arg variable for the env file
ARG_ENV_FILE *string
)
// Store data structure
type Store struct {
StringEnv map[string]string `json:"string_env"`
BoolEnv map[string]bool `json:"bool_env"`
SliceEnv map[string][]string `json:"slice_env"`
}
2022-02-28 02:25:01 +00:00
// EnvStore struct
type EnvStore struct {
2022-01-17 06:02:13 +00:00
mutex sync.Mutex
store *Store
2022-01-17 06:02:13 +00:00
}
2022-02-28 02:25:01 +00:00
var defaultStore = &EnvStore{
store: &Store{
StringEnv: map[string]string{
constants.EnvKeyAdminCookieName: "authorizer-admin",
constants.EnvKeyJwtRoleClaim: "role",
constants.EnvKeyOrganizationName: "Authorizer",
2022-01-27 04:25:05 +00:00
constants.EnvKeyOrganizationLogo: "https://www.authorizer.dev/images/logo.png",
},
BoolEnv: map[string]bool{
constants.EnvKeyDisableBasicAuthentication: false,
constants.EnvKeyDisableMagicLinkLogin: false,
constants.EnvKeyDisableEmailVerification: false,
constants.EnvKeyDisableLoginPage: false,
2022-03-16 17:19:18 +00:00
constants.EnvKeyDisableSignUp: false,
},
SliceEnv: map[string][]string{},
2022-01-17 06:02:13 +00:00
},
}
2022-02-28 02:25:01 +00:00
// EnvStoreObj.GetBoolStoreEnvVariable global variable for EnvStore
var EnvStoreObj = defaultStore
2022-01-17 06:02:13 +00:00
// UpdateEnvStore to update the whole env store object
2022-02-28 02:25:01 +00:00
func (e *EnvStore) UpdateEnvStore(store Store) {
2022-01-17 06:02:13 +00:00
e.mutex.Lock()
defer e.mutex.Unlock()
// just override the keys + new keys
for key, value := range store.StringEnv {
e.store.StringEnv[key] = value
}
for key, value := range store.BoolEnv {
e.store.BoolEnv[key] = value
}
for key, value := range store.SliceEnv {
e.store.SliceEnv[key] = value
2022-01-17 06:02:13 +00:00
}
}
// UpdateEnvVariable to update the particular env variable
2022-02-28 02:25:01 +00:00
func (e *EnvStore) UpdateEnvVariable(storeIdentifier, key string, value interface{}) {
2022-01-17 06:02:13 +00:00
e.mutex.Lock()
defer e.mutex.Unlock()
switch storeIdentifier {
case constants.StringStoreIdentifier:
e.store.StringEnv[key] = value.(string)
case constants.BoolStoreIdentifier:
e.store.BoolEnv[key] = value.(bool)
case constants.SliceStoreIdentifier:
e.store.SliceEnv[key] = value.([]string)
}
}
// GetStringStoreEnvVariable to get the env variable from string store object
2022-02-28 02:25:01 +00:00
func (e *EnvStore) GetStringStoreEnvVariable(key string) string {
// e.mutex.Lock()
// defer e.mutex.Unlock()
return e.store.StringEnv[key]
2022-01-17 06:02:13 +00:00
}
// GetBoolStoreEnvVariable to get the env variable from bool store object
2022-02-28 02:25:01 +00:00
func (e *EnvStore) GetBoolStoreEnvVariable(key string) bool {
2022-01-17 06:02:13 +00:00
// e.mutex.Lock()
// defer e.mutex.Unlock()
return e.store.BoolEnv[key]
}
// GetSliceStoreEnvVariable to get the env variable from slice store object
2022-02-28 02:25:01 +00:00
func (e *EnvStore) GetSliceStoreEnvVariable(key string) []string {
// e.mutex.Lock()
// defer e.mutex.Unlock()
return e.store.SliceEnv[key]
2022-01-17 06:02:13 +00:00
}
// GetEnvStoreClone to get clone of current env store object
2022-02-28 02:25:01 +00:00
func (e *EnvStore) GetEnvStoreClone() Store {
2022-01-17 06:02:13 +00:00
e.mutex.Lock()
defer e.mutex.Unlock()
result := *e.store
2022-01-17 06:02:13 +00:00
return result
}
2022-02-28 02:25:01 +00:00
func (e *EnvStore) ResetStore() {
e.mutex.Lock()
defer e.mutex.Unlock()
e.store = defaultStore.store
}