fix: rename config -> env and handle env interface better
This commit is contained in:
@@ -18,7 +18,7 @@ import (
|
||||
func initArangodb() (arangoDriver.Database, error) {
|
||||
ctx := context.Background()
|
||||
conn, err := http.NewConnection(http.ConnectionConfig{
|
||||
Endpoints: []string{envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseURL).(string)},
|
||||
Endpoints: []string{envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseURL)},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -33,16 +33,16 @@ func initArangodb() (arangoDriver.Database, error) {
|
||||
|
||||
var arangodb driver.Database
|
||||
|
||||
arangodb_exists, err := arangoClient.DatabaseExists(nil, envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseName).(string))
|
||||
arangodb_exists, err := arangoClient.DatabaseExists(nil, envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseName))
|
||||
|
||||
if arangodb_exists {
|
||||
log.Println(envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseName).(string) + " db exists already")
|
||||
arangodb, err = arangoClient.Database(nil, envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseName).(string))
|
||||
log.Println(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseName) + " db exists already")
|
||||
arangodb, err = arangoClient.Database(nil, envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseName))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
arangodb, err = arangoClient.CreateDatabase(nil, envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseName).(string), nil)
|
||||
arangodb, err = arangoClient.CreateDatabase(nil, envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseName), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -101,13 +101,13 @@ func initArangodb() (arangoDriver.Database, error) {
|
||||
Sparse: true,
|
||||
})
|
||||
|
||||
configCollectionExists, err := arangodb.CollectionExists(ctx, Collections.Config)
|
||||
configCollectionExists, err := arangodb.CollectionExists(ctx, Collections.Env)
|
||||
if configCollectionExists {
|
||||
log.Println(Collections.Config + " collection exists already")
|
||||
log.Println(Collections.Env + " collection exists already")
|
||||
} else {
|
||||
_, err = arangodb.CreateCollection(ctx, Collections.Config, nil)
|
||||
_, err = arangodb.CreateCollection(ctx, Collections.Env, nil)
|
||||
if err != nil {
|
||||
log.Println("error creating collection("+Collections.Config+"):", err)
|
||||
log.Println("error creating collection("+Collections.Env+"):", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,161 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
arangoDriver "github.com/arangodb/go-driver"
|
||||
"github.com/google/uuid"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Key string `json:"_key,omitempty" bson:"_key"` // for arangodb
|
||||
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
|
||||
Config []byte `gorm:"type:text" json:"config" bson:"config"`
|
||||
Hash string `gorm:"type:hash" json:"hash" bson:"hash"`
|
||||
UpdatedAt int64 `gorm:"autoUpdateTime" json:"updated_at" bson:"updated_at"`
|
||||
CreatedAt int64 `gorm:"autoCreateTime" json:"created_at" bson:"created_at"`
|
||||
}
|
||||
|
||||
// AddConfig function to add config
|
||||
func (mgr *manager) AddConfig(config Config) (Config, error) {
|
||||
if config.ID == "" {
|
||||
config.ID = uuid.New().String()
|
||||
}
|
||||
|
||||
if IsORMSupported {
|
||||
// copy id as value for fields required for mongodb & arangodb
|
||||
config.Key = config.ID
|
||||
result := mgr.sqlDB.Create(&config)
|
||||
|
||||
if result.Error != nil {
|
||||
log.Println("error adding config:", result.Error)
|
||||
return config, result.Error
|
||||
}
|
||||
}
|
||||
|
||||
if IsArangoDB {
|
||||
config.CreatedAt = time.Now().Unix()
|
||||
config.UpdatedAt = time.Now().Unix()
|
||||
configCollection, _ := mgr.arangodb.Collection(nil, Collections.Config)
|
||||
meta, err := configCollection.CreateDocument(arangoDriver.WithOverwrite(nil), config)
|
||||
if err != nil {
|
||||
log.Println("error adding config:", err)
|
||||
return config, err
|
||||
}
|
||||
config.Key = meta.Key
|
||||
config.ID = meta.ID.String()
|
||||
}
|
||||
|
||||
if IsMongoDB {
|
||||
config.CreatedAt = time.Now().Unix()
|
||||
config.UpdatedAt = time.Now().Unix()
|
||||
config.Key = config.ID
|
||||
configCollection := mgr.mongodb.Collection(Collections.Config, options.Collection())
|
||||
_, err := configCollection.InsertOne(nil, config)
|
||||
if err != nil {
|
||||
log.Println("error adding config:", err)
|
||||
return config, err
|
||||
}
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// UpdateConfig function to update config
|
||||
func (mgr *manager) UpdateConfig(config Config) (Config, error) {
|
||||
config.UpdatedAt = time.Now().Unix()
|
||||
|
||||
if IsORMSupported {
|
||||
result := mgr.sqlDB.Save(&config)
|
||||
|
||||
if result.Error != nil {
|
||||
log.Println("error updating config:", result.Error)
|
||||
return config, result.Error
|
||||
}
|
||||
}
|
||||
|
||||
if IsArangoDB {
|
||||
collection, _ := mgr.arangodb.Collection(nil, Collections.Config)
|
||||
meta, err := collection.UpdateDocument(nil, config.Key, config)
|
||||
if err != nil {
|
||||
log.Println("error updating config:", err)
|
||||
return config, err
|
||||
}
|
||||
|
||||
config.Key = meta.Key
|
||||
config.ID = meta.ID.String()
|
||||
}
|
||||
|
||||
if IsMongoDB {
|
||||
configCollection := mgr.mongodb.Collection(Collections.Config, options.Collection())
|
||||
_, err := configCollection.UpdateOne(nil, bson.M{"_id": bson.M{"$eq": config.ID}}, bson.M{"$set": config}, options.MergeUpdateOptions())
|
||||
if err != nil {
|
||||
log.Println("error updating config:", err)
|
||||
return config, err
|
||||
}
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// GetConfig function to get config
|
||||
func (mgr *manager) GetConfig() (Config, error) {
|
||||
var config Config
|
||||
|
||||
if IsORMSupported {
|
||||
result := mgr.sqlDB.First(&config)
|
||||
|
||||
if result.Error != nil {
|
||||
return config, result.Error
|
||||
}
|
||||
}
|
||||
|
||||
if IsArangoDB {
|
||||
query := fmt.Sprintf("FOR d in %s RETURN d", Collections.Config)
|
||||
|
||||
cursor, err := mgr.arangodb.Query(nil, query, nil)
|
||||
if err != nil {
|
||||
return config, err
|
||||
}
|
||||
defer cursor.Close()
|
||||
|
||||
for {
|
||||
if !cursor.HasMore() {
|
||||
if config.Key == "" {
|
||||
return config, fmt.Errorf("config not found")
|
||||
}
|
||||
break
|
||||
}
|
||||
_, err := cursor.ReadDocument(nil, &config)
|
||||
if err != nil {
|
||||
return config, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if IsMongoDB {
|
||||
configCollection := mgr.mongodb.Collection(Collections.Config, options.Collection())
|
||||
cursor, err := configCollection.Find(nil, bson.M{}, options.Find())
|
||||
if err != nil {
|
||||
return config, err
|
||||
}
|
||||
defer cursor.Close(nil)
|
||||
|
||||
for cursor.Next(nil) {
|
||||
err := cursor.Decode(&config)
|
||||
if err != nil {
|
||||
return config, err
|
||||
}
|
||||
}
|
||||
|
||||
if config.ID == "" {
|
||||
return config, fmt.Errorf("config not found")
|
||||
}
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
@@ -29,9 +29,9 @@ type Manager interface {
|
||||
GetVerificationByEmail(email string, identifier string) (VerificationRequest, error)
|
||||
AddSession(session Session) error
|
||||
DeleteUserSession(userId string) error
|
||||
AddConfig(config Config) (Config, error)
|
||||
UpdateConfig(config Config) (Config, error)
|
||||
GetConfig() (Config, error)
|
||||
AddEnv(env Env) (Env, error)
|
||||
UpdateEnv(env Env) (Env, error)
|
||||
GetEnv() (Env, error)
|
||||
}
|
||||
|
||||
type manager struct {
|
||||
@@ -45,7 +45,7 @@ type CollectionList struct {
|
||||
User string
|
||||
VerificationRequest string
|
||||
Session string
|
||||
Config string
|
||||
Env string
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -58,7 +58,7 @@ var (
|
||||
User: Prefix + "users",
|
||||
VerificationRequest: Prefix + "verification_requests",
|
||||
Session: Prefix + "sessions",
|
||||
Config: Prefix + "config",
|
||||
Env: Prefix + "env",
|
||||
}
|
||||
)
|
||||
|
||||
@@ -66,9 +66,9 @@ func InitDB() {
|
||||
var sqlDB *gorm.DB
|
||||
var err error
|
||||
|
||||
IsORMSupported = envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseType).(string) != constants.DbTypeArangodb && envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseType).(string) != constants.DbTypeMongodb
|
||||
IsArangoDB = envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseType).(string) == constants.DbTypeArangodb
|
||||
IsMongoDB = envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseType).(string) == constants.DbTypeMongodb
|
||||
IsORMSupported = envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseType) != constants.DbTypeArangodb && envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseType) != constants.DbTypeMongodb
|
||||
IsArangoDB = envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseType) == constants.DbTypeArangodb
|
||||
IsMongoDB = envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseType) == constants.DbTypeMongodb
|
||||
|
||||
// sql db orm config
|
||||
ormConfig := &gorm.Config{
|
||||
@@ -77,20 +77,20 @@ func InitDB() {
|
||||
},
|
||||
}
|
||||
|
||||
log.Println("db type:", envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseType).(string))
|
||||
log.Println("db type:", envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseType))
|
||||
|
||||
switch envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseType).(string) {
|
||||
switch envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseType) {
|
||||
case constants.DbTypePostgres:
|
||||
sqlDB, err = gorm.Open(postgres.Open(envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseURL).(string)), ormConfig)
|
||||
sqlDB, err = gorm.Open(postgres.Open(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseURL)), ormConfig)
|
||||
break
|
||||
case constants.DbTypeSqlite:
|
||||
sqlDB, err = gorm.Open(sqlite.Open(envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseURL).(string)), ormConfig)
|
||||
sqlDB, err = gorm.Open(sqlite.Open(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseURL)), ormConfig)
|
||||
break
|
||||
case constants.DbTypeMysql:
|
||||
sqlDB, err = gorm.Open(mysql.Open(envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseURL).(string)), ormConfig)
|
||||
sqlDB, err = gorm.Open(mysql.Open(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseURL)), ormConfig)
|
||||
break
|
||||
case constants.DbTypeSqlserver:
|
||||
sqlDB, err = gorm.Open(sqlserver.Open(envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseURL).(string)), ormConfig)
|
||||
sqlDB, err = gorm.Open(sqlserver.Open(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseURL)), ormConfig)
|
||||
break
|
||||
case constants.DbTypeArangodb:
|
||||
arangodb, err := initArangodb()
|
||||
@@ -123,7 +123,7 @@ func InitDB() {
|
||||
if err != nil {
|
||||
log.Fatal("Failed to init sqlDB:", err)
|
||||
} else {
|
||||
sqlDB.AutoMigrate(&User{}, &VerificationRequest{}, &Session{}, &Config{})
|
||||
sqlDB.AutoMigrate(&User{}, &VerificationRequest{}, &Session{}, &Env{})
|
||||
}
|
||||
Mgr = &manager{
|
||||
sqlDB: sqlDB,
|
||||
|
161
server/db/env.go
Normal file
161
server/db/env.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
arangoDriver "github.com/arangodb/go-driver"
|
||||
"github.com/google/uuid"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type Env struct {
|
||||
Key string `json:"_key,omitempty" bson:"_key"` // for arangodb
|
||||
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
|
||||
EnvData []byte `gorm:"type:text" json:"env" bson:"env"`
|
||||
Hash string `gorm:"type:hash" json:"hash" bson:"hash"`
|
||||
UpdatedAt int64 `gorm:"autoUpdateTime" json:"updated_at" bson:"updated_at"`
|
||||
CreatedAt int64 `gorm:"autoCreateTime" json:"created_at" bson:"created_at"`
|
||||
}
|
||||
|
||||
// AddEnv function to add env to db
|
||||
func (mgr *manager) AddEnv(env Env) (Env, error) {
|
||||
if env.ID == "" {
|
||||
env.ID = uuid.New().String()
|
||||
}
|
||||
|
||||
if IsORMSupported {
|
||||
// copy id as value for fields required for mongodb & arangodb
|
||||
env.Key = env.ID
|
||||
result := mgr.sqlDB.Create(&env)
|
||||
|
||||
if result.Error != nil {
|
||||
log.Println("error adding config:", result.Error)
|
||||
return env, result.Error
|
||||
}
|
||||
}
|
||||
|
||||
if IsArangoDB {
|
||||
env.CreatedAt = time.Now().Unix()
|
||||
env.UpdatedAt = time.Now().Unix()
|
||||
configCollection, _ := mgr.arangodb.Collection(nil, Collections.Env)
|
||||
meta, err := configCollection.CreateDocument(arangoDriver.WithOverwrite(nil), env)
|
||||
if err != nil {
|
||||
log.Println("error adding config:", err)
|
||||
return env, err
|
||||
}
|
||||
env.Key = meta.Key
|
||||
env.ID = meta.ID.String()
|
||||
}
|
||||
|
||||
if IsMongoDB {
|
||||
env.CreatedAt = time.Now().Unix()
|
||||
env.UpdatedAt = time.Now().Unix()
|
||||
env.Key = env.ID
|
||||
configCollection := mgr.mongodb.Collection(Collections.Env, options.Collection())
|
||||
_, err := configCollection.InsertOne(nil, env)
|
||||
if err != nil {
|
||||
log.Println("error adding config:", err)
|
||||
return env, err
|
||||
}
|
||||
}
|
||||
|
||||
return env, nil
|
||||
}
|
||||
|
||||
// UpdateEnv function to update env in db
|
||||
func (mgr *manager) UpdateEnv(env Env) (Env, error) {
|
||||
env.UpdatedAt = time.Now().Unix()
|
||||
|
||||
if IsORMSupported {
|
||||
result := mgr.sqlDB.Save(&env)
|
||||
|
||||
if result.Error != nil {
|
||||
log.Println("error updating config:", result.Error)
|
||||
return env, result.Error
|
||||
}
|
||||
}
|
||||
|
||||
if IsArangoDB {
|
||||
collection, _ := mgr.arangodb.Collection(nil, Collections.Env)
|
||||
meta, err := collection.UpdateDocument(nil, env.Key, env)
|
||||
if err != nil {
|
||||
log.Println("error updating config:", err)
|
||||
return env, err
|
||||
}
|
||||
|
||||
env.Key = meta.Key
|
||||
env.ID = meta.ID.String()
|
||||
}
|
||||
|
||||
if IsMongoDB {
|
||||
configCollection := mgr.mongodb.Collection(Collections.Env, options.Collection())
|
||||
_, err := configCollection.UpdateOne(nil, bson.M{"_id": bson.M{"$eq": env.ID}}, bson.M{"$set": env}, options.MergeUpdateOptions())
|
||||
if err != nil {
|
||||
log.Println("error updating config:", err)
|
||||
return env, err
|
||||
}
|
||||
}
|
||||
|
||||
return env, nil
|
||||
}
|
||||
|
||||
// GetConfig function to get config
|
||||
func (mgr *manager) GetEnv() (Env, error) {
|
||||
var env Env
|
||||
|
||||
if IsORMSupported {
|
||||
result := mgr.sqlDB.First(&env)
|
||||
|
||||
if result.Error != nil {
|
||||
return env, result.Error
|
||||
}
|
||||
}
|
||||
|
||||
if IsArangoDB {
|
||||
query := fmt.Sprintf("FOR d in %s RETURN d", Collections.Env)
|
||||
|
||||
cursor, err := mgr.arangodb.Query(nil, query, nil)
|
||||
if err != nil {
|
||||
return env, err
|
||||
}
|
||||
defer cursor.Close()
|
||||
|
||||
for {
|
||||
if !cursor.HasMore() {
|
||||
if env.Key == "" {
|
||||
return env, fmt.Errorf("config not found")
|
||||
}
|
||||
break
|
||||
}
|
||||
_, err := cursor.ReadDocument(nil, &env)
|
||||
if err != nil {
|
||||
return env, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if IsMongoDB {
|
||||
configCollection := mgr.mongodb.Collection(Collections.Env, options.Collection())
|
||||
cursor, err := configCollection.Find(nil, bson.M{}, options.Find())
|
||||
if err != nil {
|
||||
return env, err
|
||||
}
|
||||
defer cursor.Close(nil)
|
||||
|
||||
for cursor.Next(nil) {
|
||||
err := cursor.Decode(&env)
|
||||
if err != nil {
|
||||
return env, err
|
||||
}
|
||||
}
|
||||
|
||||
if env.ID == "" {
|
||||
return env, fmt.Errorf("config not found")
|
||||
}
|
||||
}
|
||||
|
||||
return env, nil
|
||||
}
|
@@ -13,7 +13,7 @@ import (
|
||||
)
|
||||
|
||||
func initMongodb() (*mongo.Database, error) {
|
||||
mongodbOptions := options.Client().ApplyURI(envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseURL).(string))
|
||||
mongodbOptions := options.Client().ApplyURI(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseURL))
|
||||
maxWait := time.Duration(5 * time.Second)
|
||||
mongodbOptions.ConnectTimeout = &maxWait
|
||||
mongoClient, err := mongo.NewClient(mongodbOptions)
|
||||
@@ -31,7 +31,7 @@ func initMongodb() (*mongo.Database, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mongodb := mongoClient.Database(envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseName).(string), options.Database())
|
||||
mongodb := mongoClient.Database(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseName), options.Database())
|
||||
|
||||
mongodb.CreateCollection(ctx, Collections.User, options.CreateCollection())
|
||||
userCollection := mongodb.Collection(Collections.User, options.Collection())
|
||||
@@ -74,7 +74,7 @@ func initMongodb() (*mongo.Database, error) {
|
||||
},
|
||||
}, options.CreateIndexes())
|
||||
|
||||
mongodb.CreateCollection(ctx, Collections.Config, options.CreateCollection())
|
||||
mongodb.CreateCollection(ctx, Collections.Env, options.CreateCollection())
|
||||
|
||||
return mongodb, nil
|
||||
}
|
||||
|
@@ -45,7 +45,7 @@ func (mgr *manager) AddUser(user User) (User, error) {
|
||||
}
|
||||
|
||||
if user.Roles == "" {
|
||||
user.Roles = strings.Join(envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDefaultRoles).([]string), ",")
|
||||
user.Roles = strings.Join(envstore.EnvInMemoryStoreObj.GetSliceStoreEnvVariable(constants.EnvKeyDefaultRoles), ",")
|
||||
}
|
||||
|
||||
if IsORMSupported {
|
||||
|
Reference in New Issue
Block a user