blob: 135df951afbd114cccb9646de3a561508d2bb8be (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package config
import (
"log/slog"
"os"
"github.com/spf13/viper"
)
type Config struct {
ServerConfig ServerConfig `mapstructure:"SERVICE"`
BaseURL string `mapstructure:"BASE_URL"`
SessionLifetime int `mapstructure:"SESSION_LIFETIME_SECONDS"`
}
type ServerConfig struct {
Host string `mapstructure:"HOST"`
Port string `mapstructure:"PORT"`
}
// LoadConfig Load server configuration from the yaml file
func LoadConfig(viperConf *viper.Viper) Config {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
var config Config
err := viperConf.Unmarshal(&config)
if err != nil {
logger.Error("Unable to decode configuration file", "error", err)
}
return config
}
// OpenConfig open config file
func OpenConfig() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
viper.SetConfigType("yml")
viper.SetConfigName("config")
viper.SetEnvPrefix("CFG")
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
logger.Error("Unable to read configuration file", "error", err)
}
}
|