Passed
Pull Request — main (#166)
by Yume
02:21
created

cmd/v2/config/viper.go   A

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 47
dl 0
loc 69
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B config.*LogConfig.GetSlogLevel 0 12 6
A config.LoadConfig 0 16 3
1
package config
2
3
import (
4
	"log/slog"
5
6
	v2 "github.com/memnix/memnix-rest/app/v2"
7
	"github.com/memnix/memnix-rest/infrastructures"
8
	"github.com/memnix/memnix-rest/pkg/oauth"
9
	"github.com/spf13/viper"
10
)
11
12
// Config holds the configuration for the application.
13
type Config struct {
14
	Server    v2.ServerConfig
15
	Log       LogConfig
16
	Auth      AuthConfig
17
	Sentry    infrastructures.SentryConfig
18
	Database  infrastructures.DatabaseConfig
19
	Redis     infrastructures.RedisConfig
20
	Ristretto infrastructures.RistrettoConfig
21
}
22
23
// LogConfig holds the configuration for the logger.
24
type LogConfig struct {
25
	Level string
26
}
27
28
func (logConfig *LogConfig) GetSlogLevel() slog.Level {
29
	switch logConfig.Level {
30
	case "debug":
31
		return slog.LevelDebug
32
	case "info":
33
		return slog.LevelInfo
34
	case "warn":
35
		return slog.LevelWarn
36
	case "error":
37
		return slog.LevelError
38
	default:
39
		return slog.LevelInfo
40
	}
41
}
42
43
// AuthConfig holds the configuration for the authentication.
44
type AuthConfig struct {
45
	Discord       oauth.DiscordConfig
46
	Github        oauth.GithubConfig
47
	JWTSecret     string
48
	JWTHeaderLen  int
49
	JWTExpiration int
50
	Bcryptcost    int
51
}
52
53
// LoadConfig loads the configuration from a file.
54
func LoadConfig(filename string) (*Config, error) {
55
	v := viper.New()
56
	v.SetConfigName(filename)
57
	v.AddConfigPath(".")
58
	v.AutomaticEnv()
59
60
	if err := v.ReadInConfig(); err != nil {
61
		return nil, err
62
	}
63
64
	var c Config
65
	if err := v.Unmarshal(&c); err != nil {
66
		return nil, err
67
	}
68
69
	return &c, nil
70
}
71