Skip to content

Commit 62ffe71

Browse files
Added normalization completed to all the config values and adjusted logger initilization in main.go
1 parent c2cc768 commit 62ffe71

File tree

10 files changed

+183
-15
lines changed

10 files changed

+183
-15
lines changed

cmd/laclm/main.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ func exec() error {
2828

2929
/* exec() wraps run() protecting it with user interrupts */
3030

31-
utils.InitLogger(true)
32-
33-
/* zap.L() can be used all over the code for global level logging */
34-
35-
zap.L().Info("Logger Initiated ...")
36-
3731
/* setting up cobra for cli interactions */
3832
var(
3933
configPath string
@@ -59,9 +53,7 @@ func exec() error {
5953

6054
/* Execute the command */
6155
if err := rootCmd.Execute(); err != nil {
62-
zap.L().Error("arguements error",
63-
zap.Error(err),
64-
)
56+
fmt.Printf("arguements error: %s", err.Error())
6557
os.Exit(1)
6658
}
6759

@@ -76,8 +68,16 @@ func exec() error {
7668
if there is an error or environment variables are not set, then it will exit with code 1
7769
*/
7870
config.LoadEnv()
71+
72+
/*
73+
true for production, false for development mode
74+
logger is only for http server and core components (after this step)
75+
using logger for cli issues doesn't make sense
76+
*/
77+
utils.InitLogger(!config.BackendConfig.AppInfo.DebugMode)
7978

80-
/* true for production, false for development mode */
79+
/* zap.L() can be used all over the code for global level logging */
80+
zap.L().Info("Logger Initiated ...")
8181

8282
ctx, cancel := context.WithCancel(context.Background())
8383
defer cancel()

config.yaml

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

99
# backend server deployment configs
1010
server:

config/app.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,25 @@ package config
22

33
/* app parameters */
44
type App struct {
5-
Name string `yaml:"name"`
6-
Version string `yaml:"version"`
7-
Environment string `yaml:"environment"`
5+
Name string `yaml:"name"`
6+
Version string `yaml:"version"`
7+
DebugMode bool `yaml:"debug_mode"`
8+
}
9+
10+
/* normalization function */
11+
func (a *App) Normalize() error {
12+
if a.Name == "" {
13+
a.Name = "laclm"
14+
}
15+
16+
if a.Version == "" {
17+
a.Name = "v1.1"
18+
}
19+
20+
/*
21+
if debug_mode is not provided, it's false
22+
we want production to be true
23+
*/
24+
25+
return nil
826
}

config/backend_security.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,12 @@ package config
44
type BackendSecurity struct {
55
JWTExpiry int `yaml:"jwt_expiry"`
66
}
7+
8+
/* normalization function */
9+
func (b *BackendSecurity) Normalize() error {
10+
if b.JWTExpiry == 0 {
11+
b.JWTExpiry = 1
12+
}
13+
14+
return nil
15+
}

config/config.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package config
22

3+
import "fmt"
4+
35
/* globally accessible yaml config */
46
var BackendConfig Config
57

@@ -20,3 +22,34 @@ type Config struct {
2022
type EnvironmentConfig struct {
2123
JWTSecret string
2224
}
25+
26+
/* complete config normalizer function */
27+
func (c *Config) Normalize() error {
28+
if err := c.AppInfo.Normalize(); err != nil {
29+
return fmt.Errorf("app configuration error: %w", err)
30+
}
31+
32+
if err := c.Server.Normalize(); err != nil {
33+
return fmt.Errorf("server configuration error: %w", err)
34+
}
35+
36+
if err := c.Database.Normalize(); err != nil {
37+
return fmt.Errorf("database configuration error: %w", err)
38+
}
39+
40+
if err := c.Logging.Normalize(); err != nil {
41+
return fmt.Errorf("logging configuration error: %w", err)
42+
}
43+
44+
for i := range c.FileSystemServers {
45+
if err := c.FileSystemServers[i].Normalize(); err != nil {
46+
return fmt.Errorf("file system server [%d] error: %w", i, err)
47+
}
48+
}
49+
50+
if err := c.BackendSecurity.Normalize(); err != nil {
51+
return fmt.Errorf("backend security configuration error: %w", err)
52+
}
53+
54+
return nil
55+
}

config/database.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package config
22

3+
import (
4+
"errors"
5+
6+
"github.com/MakeNowJust/heredoc"
7+
)
8+
39
/* database parameters */
410
type Database struct {
511
TransactionLogRedis TransactionLogRedis `yaml:"transaction_log_redis"`
@@ -11,3 +17,24 @@ type TransactionLogRedis struct {
1117
Password string `yaml:"password"`
1218
DB string `yaml:"db"`
1319
}
20+
21+
/* normalization function */
22+
func (d *Database) Normalize() error {
23+
return d.TransactionLogRedis.Normalize()
24+
}
25+
26+
func (r *TransactionLogRedis) Normalize() error {
27+
if r.Address == "" {
28+
return errors.New(heredoc.Doc(`
29+
Transaction Log Redis Address is not specified in the configuration file.
30+
31+
Please check the docs for more information:
32+
`))
33+
}
34+
35+
if r.DB == "" {
36+
r.DB = "0"
37+
}
38+
39+
return nil
40+
}

config/filesystem.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,61 @@
11
package config
22

3+
import (
4+
"errors"
5+
6+
"github.com/MakeNowJust/heredoc"
7+
)
8+
39
/* file system server parameters */
410
type FileSystemServers struct {
5-
Remote *Remote `yaml:"remote,omitempty"`
611
Path string `yaml:"path"`
712
Method string `yaml:"method"`
13+
Remote *Remote `yaml:"remote,omitempty"`
814
}
915

1016
/* remote parameters for file system server with laclm daemons installed */
1117
type Remote struct {
1218
Host string `yaml:"host"`
1319
Port int `yaml:"port"`
1420
}
21+
22+
/* normalization function */
23+
func (f *FileSystemServers) Normalize() error {
24+
if f.Path == "" {
25+
return errors.New(heredoc.Doc(`
26+
Remote server file path not specified in the configuration file.
27+
28+
Please check the docs for more information:
29+
`))
30+
}
31+
32+
if f.Method == "" {
33+
f.Method = "local"
34+
}
35+
36+
if f.Method == "remote" {
37+
if f.Remote == nil {
38+
return errors.New(heredoc.Doc(`
39+
40+
`))
41+
}
42+
43+
if f.Remote.Host == "" {
44+
return errors.New(heredoc.Doc(`
45+
Address not provided for remote file server
46+
47+
Please check the docs for more information:
48+
`))
49+
}
50+
51+
if f.Remote.Port == 0 {
52+
return errors.New(heredoc.Doc(`
53+
Port not provided for remote file server
54+
55+
Please check the docs for more information:
56+
`))
57+
}
58+
}
59+
60+
return nil
61+
}

config/loader.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ func LoadConfig(path string) {
3131
zap.Error(err),
3232
)
3333
}
34+
35+
BackendConfig.Normalize()
3436
}
3537

3638
/* loads environment variables */

config/logging.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,22 @@ type Logging struct {
88
MaxAge int `yaml:"max_age"`
99
Compress bool `yaml:"compress"`
1010
}
11+
12+
/* normalization function */
13+
func (l *Logging) Normalize() error {
14+
if l.File == "" {
15+
l.File = "log/app.log"
16+
}
17+
18+
if l.MaxSize == 0 {
19+
l.MaxSize = 100
20+
}
21+
22+
if l.MaxBackups == 0 {
23+
l.MaxBackups = 3
24+
}
25+
26+
/* let compression remain false by default */
27+
28+
return nil
29+
}

config/server.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@ type Server struct {
55
Host string `yaml:"host"`
66
Port int `yaml:"port"`
77
}
8+
9+
/* normalization function */
10+
func (s *Server) Normalize() error {
11+
if s.Host == "" {
12+
s.Host = "localhost"
13+
}
14+
15+
if s.Port == 0 {
16+
s.Port = 8080
17+
}
18+
19+
return nil
20+
}

0 commit comments

Comments
 (0)