|
| 1 | +package cassandradb |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/authorizerdev/authorizer/server/constants" |
| 8 | + "github.com/authorizerdev/authorizer/server/db/models" |
| 9 | + "github.com/authorizerdev/authorizer/server/envstore" |
| 10 | + cansandraDriver "github.com/gocql/gocql" |
| 11 | +) |
| 12 | + |
| 13 | +type provider struct { |
| 14 | + db *cansandraDriver.Session |
| 15 | +} |
| 16 | + |
| 17 | +// NewProvider to initialize arangodb connection |
| 18 | +func NewProvider() (*provider, error) { |
| 19 | + dbURL := envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseURL) |
| 20 | + keySpace := envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyDatabaseName) |
| 21 | + cassandraClient := cansandraDriver.NewCluster(dbURL) |
| 22 | + // cassandraClient.Keyspace = keySpace |
| 23 | + cassandraClient.RetryPolicy = &cansandraDriver.SimpleRetryPolicy{ |
| 24 | + NumRetries: 3, |
| 25 | + } |
| 26 | + cassandraClient.Consistency = cansandraDriver.Quorum |
| 27 | + |
| 28 | + session, err := cassandraClient.CreateSession() |
| 29 | + if err != nil { |
| 30 | + log.Println("Error while creating connection to cassandra db", err) |
| 31 | + return nil, err |
| 32 | + } |
| 33 | + |
| 34 | + keyspaceQuery := fmt.Sprintf("CREATE KEYSPACE IF NOT EXISTS %s WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor':1}", |
| 35 | + keySpace) |
| 36 | + err = session.Query(keyspaceQuery).Exec() |
| 37 | + if err != nil { |
| 38 | + log.Println("Unable to create keyspace:", err) |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + // make sure collections are present |
| 43 | + envCollectionQuery := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s (id text, env text, hash text, updated_at bigint, created_at bigint, PRIMARY KEY (id))", |
| 44 | + keySpace, models.Collections.Env) |
| 45 | + err = session.Query(envCollectionQuery).Exec() |
| 46 | + if err != nil { |
| 47 | + log.Println("Unable to create env collection:", err) |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 51 | + sessionCollectionQuery := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s (id text, user_id text, user_agent text, ip text, updated_at bigint, created_at bigint, PRIMARY KEY (id))", keySpace, models.Collections.Session) |
| 52 | + err = session.Query(sessionCollectionQuery).Exec() |
| 53 | + if err != nil { |
| 54 | + log.Println("Unable to create session collection:", err) |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + userCollectionQuery := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s (id text, email text, email_verified_at bigint, password text, signup_methods text, given_name text, family_name text, middle_name text, nick_name text, gender text, birthdate text, phone_number text, phone_number_verified_at bigint, picture text, roles text, updated_at bigint, created_at bigint, revoked_timestamp bigint, PRIMARY KEY (id, email))", keySpace, models.Collections.User) |
| 59 | + err = session.Query(userCollectionQuery).Exec() |
| 60 | + if err != nil { |
| 61 | + log.Println("Unable to create user collection:", err) |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + |
| 65 | + // token is reserved keyword in cassandra, hence we need to use jwt_token |
| 66 | + verificationRequestCollectionQuery := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s (id text, jwt_token text, identifier text, expires_at bigint, email text, nonce text, redirect_uri text, created_at bigint, updated_at bigint, PRIMARY KEY (id, identifier, email))", keySpace, models.Collections.VerificationRequest) |
| 67 | + err = session.Query(verificationRequestCollectionQuery).Exec() |
| 68 | + if err != nil { |
| 69 | + log.Println("Unable to create verification request collection:", err) |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + return &provider{ |
| 74 | + db: session, |
| 75 | + }, err |
| 76 | +} |
0 commit comments