Total Lines | 69 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |