1
1
package httpclient
2
2
3
3
import (
4
- "bufio"
5
4
"encoding/json"
6
5
"fmt"
7
- "io"
8
6
"log"
9
7
"os"
10
8
"strconv"
@@ -24,56 +22,38 @@ const (
24
22
DefaultTimeout = 10 * time .Second
25
23
)
26
24
27
- // loadConfigFromFile loads configuration values from a JSON file into the ClientConfig struct.
28
- // It opens the specified configuration file, reads its content, and unmarshals the JSON data
29
- // into the ClientConfig struct. This function is crucial for initializing the client configuration
30
- // with values that may not be provided through environment variables or default values.
31
- // It uses Go's standard log package for logging, as the zap logger is not yet initialized when
32
- // this function is called.
33
- func (config * ClientConfig ) LoadConfigFromFile (filePath string ) error {
34
- // Open the configuration file
35
- file , err := os .Open (filePath )
25
+ // LoadConfigFromFile loads configuration values from a JSON file into the ClientConfig struct.
26
+ // This function opens the specified configuration file, reads its content, and unmarshals the JSON data
27
+ // into the ClientConfig struct. It's designed to initialize the client configuration with values
28
+ // from a file, complementing or overriding defaults and environment variable settings.
29
+ // LoadConfigFromFile loads configuration values from a JSON file into the ClientConfig struct.
30
+ func LoadConfigFromFile (filePath string ) (* ClientConfig , error ) {
31
+ // Read the entire file
32
+ fileBytes , err := os .ReadFile (filePath )
36
33
if err != nil {
37
- log .Printf ("Failed to open the configuration file: %s, error: %v" , filePath , err )
38
- return err
39
- }
40
- defer file .Close ()
41
-
42
- reader := bufio .NewReader (file )
43
- var builder strings.Builder
44
-
45
- // Read the file content
46
- for {
47
- part , _ , err := reader .ReadLine ()
48
- if err == io .EOF {
49
- break
50
- }
51
- if err != nil {
52
- log .Printf ("Failed to read the configuration file: %s, error: %v" , filePath , err )
53
- return err
54
- }
55
- builder .Write (part )
34
+ return nil , fmt .Errorf ("failed to read the configuration file: %s, error: %w" , filePath , err )
56
35
}
57
36
58
- // Unmarshal JSON content into the ClientConfig struct
59
- err = json .Unmarshal ([]byte (builder .String ()), config )
60
- if err != nil {
61
- log .Printf ("Failed to unmarshal the configuration file: %s, error: %v" , filePath , err )
62
- return err
37
+ // Initialize an instance of ClientConfig
38
+ var config ClientConfig
39
+
40
+ // Unmarshal the file content into the ClientConfig struct
41
+ if err := json .Unmarshal (fileBytes , & config ); err != nil {
42
+ return nil , fmt .Errorf ("failed to unmarshal the configuration file: %s, error: %w" , filePath , err )
63
43
}
64
44
65
45
log .Printf ("Configuration successfully loaded from file: %s" , filePath )
66
46
67
47
// Set default values if necessary
68
- setLoggerDefaultValues (config )
69
- setClientDefaultValues (config )
48
+ setLoggerDefaultValues (& config )
49
+ setClientDefaultValues (& config )
70
50
71
- // validate configuration
72
- if err := validateMandatoryConfiguration (config ); err != nil {
73
- return fmt .Errorf ("configuration validation failed: %w" , err )
51
+ // Validate mandatory configuration fields
52
+ if err := validateMandatoryConfiguration (& config ); err != nil {
53
+ return nil , fmt .Errorf ("configuration validation failed: %w" , err )
74
54
}
75
55
76
- return nil
56
+ return & config , nil
77
57
}
78
58
79
59
// LoadConfigFromEnv populates the ClientConfig structure with values from environment variables.
0 commit comments