2022-01-17 06:02:13 +00:00
|
|
|
package envstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
|
|
|
)
|
|
|
|
|
2022-01-22 19:54:41 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2022-01-20 11:22:37 +00:00
|
|
|
// 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-01-17 06:02:13 +00:00
|
|
|
// EnvInMemoryStore struct
|
|
|
|
type EnvInMemoryStore struct {
|
|
|
|
mutex sync.Mutex
|
2022-01-20 11:22:37 +00:00
|
|
|
store *Store
|
2022-01-17 06:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EnvInMemoryStoreObj global variable for EnvInMemoryStore
|
|
|
|
var EnvInMemoryStoreObj = &EnvInMemoryStore{
|
2022-01-20 11:22:37 +00:00
|
|
|
store: &Store{
|
|
|
|
StringEnv: map[string]string{
|
|
|
|
constants.EnvKeyAdminCookieName: "authorizer-admin",
|
|
|
|
constants.EnvKeyJwtRoleClaim: "role",
|
|
|
|
constants.EnvKeyOrganizationName: "Authorizer",
|
|
|
|
constants.EnvKeyOrganizationLogo: "https://www.authorizer.io/images/logo.png",
|
|
|
|
},
|
|
|
|
BoolEnv: map[string]bool{
|
|
|
|
constants.EnvKeyDisableBasicAuthentication: false,
|
|
|
|
constants.EnvKeyDisableMagicLinkLogin: false,
|
|
|
|
constants.EnvKeyDisableEmailVerification: false,
|
|
|
|
constants.EnvKeyDisableLoginPage: false,
|
|
|
|
},
|
|
|
|
SliceEnv: map[string][]string{},
|
2022-01-17 06:02:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateEnvStore to update the whole env store object
|
2022-01-20 11:22:37 +00:00
|
|
|
func (e *EnvInMemoryStore) UpdateEnvStore(store Store) {
|
2022-01-17 06:02:13 +00:00
|
|
|
e.mutex.Lock()
|
|
|
|
defer e.mutex.Unlock()
|
|
|
|
// just override the keys + new keys
|
2022-01-20 11:22:37 +00:00
|
|
|
|
|
|
|
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-01-20 11:22:37 +00:00
|
|
|
func (e *EnvInMemoryStore) UpdateEnvVariable(storeIdentifier, key string, value interface{}) {
|
2022-01-17 06:02:13 +00:00
|
|
|
e.mutex.Lock()
|
|
|
|
defer e.mutex.Unlock()
|
2022-01-20 11:22:37 +00:00
|
|
|
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
|
|
|
|
func (e *EnvInMemoryStore) GetStringStoreEnvVariable(key string) string {
|
|
|
|
// e.mutex.Lock()
|
|
|
|
// defer e.mutex.Unlock()
|
|
|
|
return e.store.StringEnv[key]
|
2022-01-17 06:02:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 11:22:37 +00:00
|
|
|
// GetBoolStoreEnvVariable to get the env variable from bool store object
|
|
|
|
func (e *EnvInMemoryStore) GetBoolStoreEnvVariable(key string) bool {
|
2022-01-17 06:02:13 +00:00
|
|
|
// e.mutex.Lock()
|
|
|
|
// defer e.mutex.Unlock()
|
2022-01-20 11:22:37 +00:00
|
|
|
return e.store.BoolEnv[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSliceStoreEnvVariable to get the env variable from slice store object
|
|
|
|
func (e *EnvInMemoryStore) 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-01-20 11:22:37 +00:00
|
|
|
func (e *EnvInMemoryStore) GetEnvStoreClone() Store {
|
2022-01-17 06:02:13 +00:00
|
|
|
e.mutex.Lock()
|
|
|
|
defer e.mutex.Unlock()
|
|
|
|
|
2022-01-20 11:22:37 +00:00
|
|
|
result := *e.store
|
2022-01-17 06:02:13 +00:00
|
|
|
return result
|
|
|
|
}
|