Skip to content

Commit c04459b

Browse files
Completed complete PostgreSQL user configuration for config.yaml with validation function
1 parent 8f830a5 commit c04459b

File tree

1 file changed

+61
-2
lines changed

1 file changed

+61
-2
lines changed

config/database.go

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
/* database parameters */
1111
type Database struct {
1212
TransactionLogRedis TransactionLogRedis `yaml:"transaction_log_redis,omitempty"`
13+
ArchivalPQ ArchivalPQ `yaml:"archival_postgres,omitempty"`
1314
}
1415

1516
/* transaction log redis parameters */
@@ -19,9 +20,31 @@ type TransactionLogRedis struct {
1920
DB int `yaml:"db,omitempty"`
2021
}
2122

22-
/* normalization function */
23+
/* archival PostgreSQL parameters */
24+
type ArchivalPQ struct {
25+
Host string `yaml:"host,omitempty"`
26+
Port int `yaml:"port,omitempty"`
27+
User string `yaml:"user,omitempty"`
28+
Password string `yaml:"password,omitempty"`
29+
DBName string `yaml:"dbname,omitempty"`
30+
SSLMode string `yaml:"sslmode,omitempty"`
31+
}
32+
33+
/* normalization function for database */
2334
func (d *Database) Normalize() error {
24-
return d.TransactionLogRedis.Normalize()
35+
/* check if Redis parameters are valid */
36+
err := d.TransactionLogRedis.Normalize()
37+
if err != nil {
38+
return err
39+
}
40+
41+
/* check if PostgreSQL parameters are valid */
42+
err = d.ArchivalPQ.Normalize()
43+
if err != nil {
44+
return err
45+
}
46+
47+
return nil
2548
}
2649

2750
/* transaction log redis normalization function */
@@ -44,3 +67,39 @@ func (r *TransactionLogRedis) Normalize() error {
4467

4568
return nil
4669
}
70+
71+
/* archival PostgreSQL parameters */
72+
func (a *ArchivalPQ) Normalize() error {
73+
74+
/* return localhost if empty */
75+
if a.Host == "" {
76+
a.Host = "localhost"
77+
}
78+
79+
/* return default port if empty */
80+
if a.Port == 0 {
81+
a.Port = 5432
82+
}
83+
84+
/* username is mandatory */
85+
if a.User == "" {
86+
return errors.New("Database username is not set in the configuration.")
87+
}
88+
89+
/* dbname is mandatory */
90+
if a.DBName == "" {
91+
return errors.New("Database name (dbname) is not set in the configuration.")
92+
}
93+
94+
/* sslmode is disabled by default */
95+
if a.SSLMode == "" {
96+
a.SSLMode = "disable"
97+
}
98+
99+
/* empty password but give a warning */
100+
if a.Password == "" {
101+
fmt.Printf("Warning: Connecting to PostgreSQL without a password. Consider using one for security.\n\n")
102+
}
103+
104+
return nil
105+
}

0 commit comments

Comments
 (0)