Skip to content

Commit 716ea5e

Browse files
committed
Added a new .env variable
1 parent 0d7b22d commit 716ea5e

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

Diff for: .env.example

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ DB_USER=postgres
44
DB_PASSWORD=postgres
55
DB_NAME=nuklaivm
66
DB_SSLMODE=require # Or "disable" if you don't want to use SSL
7+
DB_RESET=true # Set to "true" to reset the database on every restart
78
GRPC_WHITELISTED_BLOCKCHAIN_NODES="172.17.1.56" # "127.0.0.1,localhost,::1" is already included by default. You can even include something like myblockchain.aws.com

Diff for: config/config.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import (
1616
func GetDatabaseURL() string {
1717
// Retrieve env variable for DB_HOST and set default value if not present
1818

19-
host := getEnv("DB_HOST", "localhost")
20-
port := getEnv("DB_PORT", "5432")
21-
user := getEnv("DB_USER", "postgres")
22-
password := getEnv("DB_PASSWORD", "postgres")
23-
dbName := getEnv("DB_NAME", "nuklaivm")
24-
sslMode := getEnv("DB_SSL_MODE", "disable")
19+
host := GetEnv("DB_HOST", "localhost")
20+
port := GetEnv("DB_PORT", "5432")
21+
user := GetEnv("DB_USER", "postgres")
22+
password := GetEnv("DB_PASSWORD", "postgres")
23+
dbName := GetEnv("DB_NAME", "nuklaivm")
24+
sslMode := GetEnv("DB_SSL_MODE", "disable")
2525

2626
// Encode password to handle special characters
2727
encodedPassword := url.QueryEscape(password)
@@ -36,7 +36,7 @@ func GetDatabaseURL() string {
3636
// and resolves domain names to IPs.
3737
// GetWhitelistIPs retrieves the list of whitelisted IPs and CIDR ranges
3838
func GetWhitelistIPs() ([]string, []string) {
39-
ipList := getEnv("GRPC_WHITELISTED_BLOCKCHAIN_NODES", "127.0.0.1,localhost,::1")
39+
ipList := GetEnv("GRPC_WHITELISTED_BLOCKCHAIN_NODES", "127.0.0.1,localhost,::1")
4040
entries := strings.Split(ipList, ",")
4141

4242
whitelistIPs := []string{}
@@ -63,9 +63,9 @@ func GetWhitelistIPs() ([]string, []string) {
6363
return uniqueStrings(whitelistIPs), uniqueStrings(whitelistCIDRs)
6464
}
6565

66-
// getEnv retrieves the value of the environment variable named by the key.
66+
// GetEnv retrieves the value of the environment variable named by the key.
6767
// If the variable is not present, it returns the default value.
68-
func getEnv(key, defaultValue string) string {
68+
func GetEnv(key, defaultValue string) string {
6969
if value, exists := os.LookupEnv(key); exists {
7070
return value
7171
}

Diff for: db/db.go

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"log"
1010

1111
_ "github.com/lib/pq"
12+
"github.com/nuklai/nuklaivm-external-subscriber/config"
1213
)
1314

1415
// InitDB initializes the database connection and creates the schema if it doesn't exist
@@ -24,6 +25,18 @@ func InitDB(connStr string) (*sql.DB, error) {
2425

2526
log.Println("Database connection established")
2627

28+
reset := config.GetEnv("DB_RESET", "false") == "true"
29+
if reset {
30+
// Drop all existing tables
31+
log.Println("Resetting the database...")
32+
_, err := db.Exec(`
33+
DROP TABLE IF EXISTS blocks, transactions, actions, assets, genesis_data CASCADE;
34+
`)
35+
if err != nil {
36+
return nil, fmt.Errorf("error resetting the database: %w", err)
37+
}
38+
}
39+
2740
// Ensure schema is created
2841
if err := CreateSchema(db); err != nil {
2942
return nil, fmt.Errorf("error creating schema: %w", err)

Diff for: infra/aws/task-definition-subscriber.json

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@
6767
"name": "DB_PASSWORD",
6868
"valueFrom": "arn:aws:ssm:${AWS_REGION}:${AWS_ACCOUNT_ID}:parameter/${ENVIRONMENT}/${PRODUCT}/${APPLICATION}/db_password"
6969
},
70+
{
71+
"name": "DB_RESET",
72+
"valueFrom": "arn:aws:ssm:${AWS_REGION}:${AWS_ACCOUNT_ID}:parameter/${ENVIRONMENT}/${PRODUCT}/${APPLICATION}/db_reset"
73+
},
7074
{
7175
"name": "GRPC_WHITELISTED_BLOCKCHAIN_NODES",
7276
"valueFrom": "arn:aws:ssm:${AWS_REGION}:${AWS_ACCOUNT_ID}:parameter/${ENVIRONMENT}/${PRODUCT}/${APPLICATION}/grpc_whitelisted_blockchain_nodes"

0 commit comments

Comments
 (0)