Skip to content

Commit 940e82a

Browse files
Fixed config bug for redis and completed config loader with proper error handling.
1 parent 62ffe71 commit 940e82a

File tree

7 files changed

+52
-20
lines changed

7 files changed

+52
-20
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Binary files
22
/bin/
33

4+
# Debug logs
5+
/logs/
6+
47
# for macOS dev environments
58
.DS_Store
69

cmd/laclm/main.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
"syscall"
1010
"time"
1111

12-
"go.uber.org/zap"
13-
"github.com/spf13/cobra"
1412
"github.com/MakeNowJust/heredoc"
13+
"github.com/spf13/cobra"
14+
"go.uber.org/zap"
1515

1616
"github.com/PythonHacker24/linux-acl-management-backend/api/routes"
1717
"github.com/PythonHacker24/linux-acl-management-backend/config"
@@ -40,9 +40,9 @@ func exec() error {
4040
`),
4141
Run: func(cmd *cobra.Command, args []string) {
4242
if configPath != "" {
43-
fmt.Printf("Using config file: %s\n", configPath)
43+
fmt.Printf("Using config file: %s\n\n", configPath)
4444
} else {
45-
fmt.Println("No config file provided.")
45+
fmt.Println("No config file provided.\n\n")
4646
}
4747
},
4848
}
@@ -61,13 +61,22 @@ func exec() error {
6161
load config file
6262
if there is an error in loading the config file, then it will exit with code 1
6363
*/
64-
config.LoadConfig(configPath)
64+
if err := config.LoadConfig(configPath); err != nil {
65+
fmt.Printf("Configuration Error in %s: %s",
66+
configPath,
67+
err.Error(),
68+
)
69+
/* since the configuration is invalid, don't proceed */
70+
os.Exit(1)
71+
}
6572

6673
/*
6774
load environment variables
6875
if there is an error or environment variables are not set, then it will exit with code 1
6976
*/
7077
config.LoadEnv()
78+
79+
fmt.Println("loaded config")
7180

7281
/*
7382
true for production, false for development mode

config.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
app:
55
name: laclm-dev
66
version: v1.1
7-
debug_mode: development
7+
debug_mode: true
88

99
# backend server deployment configs
1010
server:
@@ -14,13 +14,13 @@ server:
1414
# databases for operations
1515
database:
1616
transaction_logs_redis:
17-
address:
18-
password:
19-
db:
17+
address: localhost
18+
password: testingpassword
19+
db: 1
2020

2121
# logging configurations
2222
logging:
23-
file: "logs/app.log"
23+
file: logs/app.log
2424
max_size: 100
2525
max_backups: 5
2626
max_age: 30
@@ -29,9 +29,9 @@ logging:
2929
# filesystem server that needs management
3030
filesystem_servers:
3131
- path: /mnt/beegfs-1
32-
method: "remote"
32+
method: remote
3333
remote:
34-
host: "127.0.0.1"
34+
host: 127.0.0.1
3535
port: 4444
3636

3737
# authentication information

config/database.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package config
22

33
import (
44
"errors"
5+
"fmt"
56

67
"github.com/MakeNowJust/heredoc"
78
)
89

910
/* database parameters */
1011
type Database struct {
11-
TransactionLogRedis TransactionLogRedis `yaml:"transaction_log_redis"`
12+
TransactionLogRedis TransactionLogRedis `yaml:"transaction_logs_redis"`
1213
}
1314

1415
/* transaction log redis parameters */
@@ -31,6 +32,12 @@ func (r *TransactionLogRedis) Normalize() error {
3132
Please check the docs for more information:
3233
`))
3334
}
35+
36+
/* password can be empty */
37+
if r.Password == "" {
38+
/* just warn users to use password protected redis */
39+
fmt.Println("Prefer using password for redis for security purposes")
40+
}
3441

3542
if r.DB == "" {
3643
r.DB = "0"

config/loader.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
package config
22

33
import (
4+
"fmt"
45
"os"
56

67
"go.uber.org/zap"
78
"gopkg.in/yaml.v3"
9+
"github.com/davecgh/go-spew/spew"
810
)
911

1012
/*
1113
we need config normalization as well
12-
config normalization fixes all the fields that are not present in config file
14+
config normalization fixes all the fields that are not present in config file
1315
and sets it to default value
1416
*/
1517

1618
/* loads yaml config file from given file path */
17-
func LoadConfig(path string) {
19+
func LoadConfig(path string) error {
1820

1921
/* read the yaml config file */
2022
data, err := os.ReadFile(path)
2123
if err != nil {
22-
zap.L().Fatal("Failed to read config file",
23-
zap.Error(err),
24+
return fmt.Errorf("config loading error %w",
25+
err,
2426
)
27+
2528
}
2629

2730
/* unmarshal the yaml file to defined struct */
2831
err = yaml.Unmarshal(data, &BackendConfig)
2932
if err != nil {
30-
zap.L().Fatal("Failed to parse YAML config",
31-
zap.Error(err),
33+
return fmt.Errorf("config loading error %w",
34+
err,
3235
)
3336
}
37+
38+
if BackendConfig.AppInfo.DebugMode {
39+
fmt.Println("Contents of Config File (debug mode ON)")
40+
spew.Dump(BackendConfig)
41+
fmt.Println()
42+
}
3443

35-
BackendConfig.Normalize()
44+
/* normalize the complete backend config before proceeding */
45+
return BackendConfig.Normalize()
3646
}
3747

3848
/* loads environment variables */

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
77
github.com/MakeNowJust/heredoc v1.0.0 // indirect
88
github.com/bufbuild/buf v1.53.0 // indirect
9+
github.com/davecgh/go-spew v1.1.1 // indirect
910
github.com/go-asn1-ber/asn1-ber v1.5.8-0.20250403174932-29230038a667 // indirect
1011
github.com/go-ldap/ldap/v3 v3.4.11 // indirect
1112
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6
55
github.com/bufbuild/buf v1.53.0 h1:i0OgpDkzv8yLyCokXRCgbQao/SqTnqXYtERre0Jw6Do=
66
github.com/bufbuild/buf v1.53.0/go.mod h1:DylfLDMblZt5mX/SEFv2VzZMgdlePArhMGYVpk4M6N0=
77
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
8+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
9+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
810
github.com/go-asn1-ber/asn1-ber v1.5.8-0.20250403174932-29230038a667 h1:BP4M0CvQ4S3TGls2FvczZtj5Re/2ZzkV9VwqPHH/3Bo=
911
github.com/go-asn1-ber/asn1-ber v1.5.8-0.20250403174932-29230038a667/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
1012
github.com/go-ldap/ldap/v3 v3.4.11 h1:4k0Yxweg+a3OyBLjdYn5OKglv18JNvfDykSoI8bW0gU=

0 commit comments

Comments
 (0)