Total Lines | 100 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package config |
||
2 | |||
3 | import ( |
||
4 | "log/slog" |
||
5 | |||
6 | "github.com/memnix/memnix-rest/pkg/oauth" |
||
7 | "github.com/spf13/viper" |
||
8 | ) |
||
9 | |||
10 | // Config holds the configuration for the application. |
||
11 | type Config struct { |
||
12 | Server ServerConfig |
||
13 | Database DatabaseConfig |
||
14 | Redis RedisConfig |
||
15 | Log LogConfig |
||
16 | Auth AuthConfig |
||
17 | Sentry SentryConfig |
||
18 | } |
||
19 | |||
20 | // SentryConfig holds the configuration for the sentry client. |
||
21 | type SentryConfig struct { |
||
22 | Debug bool |
||
23 | Environment string |
||
24 | Release string |
||
25 | TracesSampleRate float64 |
||
26 | ProfilesSampleRate float64 |
||
27 | DSN string |
||
28 | } |
||
29 | |||
30 | // ServerConfig holds the configuration for the server. |
||
31 | type ServerConfig struct { |
||
32 | Port string |
||
33 | AppVersion string |
||
34 | JaegerURL string |
||
35 | Host string |
||
36 | FrontendURL string |
||
37 | LogLevel string |
||
38 | } |
||
39 | |||
40 | // DatabaseConfig holds the configuration for the database. |
||
41 | type DatabaseConfig struct { |
||
42 | DSN string |
||
43 | } |
||
44 | |||
45 | // RedisConfig holds the configuration for the redis client. |
||
46 | type RedisConfig struct { |
||
47 | Addr string |
||
48 | Password string |
||
49 | MinIdleConns int |
||
50 | PoolSize int |
||
51 | PoolTimeout int |
||
52 | } |
||
53 | |||
54 | // LogConfig holds the configuration for the logger. |
||
55 | type LogConfig struct { |
||
56 | Level string |
||
57 | } |
||
58 | |||
59 | func (logConfig *LogConfig) GetSlogLevel() slog.Level { |
||
60 | switch logConfig.Level { |
||
61 | case "debug": |
||
62 | return slog.LevelDebug |
||
63 | case "info": |
||
64 | return slog.LevelInfo |
||
65 | case "warn": |
||
66 | return slog.LevelWarn |
||
67 | case "error": |
||
68 | return slog.LevelError |
||
69 | default: |
||
70 | return slog.LevelInfo |
||
71 | } |
||
72 | } |
||
73 | |||
74 | // AuthConfig holds the configuration for the authentication. |
||
75 | type AuthConfig struct { |
||
76 | JWTSecret string |
||
77 | JWTHeaderLen int |
||
78 | JWTExpiration int |
||
79 | Discord oauth.DiscordConfig |
||
80 | Github oauth.GithubConfig |
||
81 | Bcryptcost int |
||
82 | } |
||
83 | |||
84 | // LoadConfig loads the configuration from a file. |
||
85 | func LoadConfig(filename string) (*Config, error) { |
||
86 | v := viper.New() |
||
87 | v.SetConfigName(filename) |
||
88 | v.AddConfigPath(".") |
||
89 | v.AutomaticEnv() |
||
90 | |||
91 | if err := v.ReadInConfig(); err != nil { |
||
92 | return nil, err |
||
93 | } |
||
94 | |||
95 | var c Config |
||
96 | if err := v.Unmarshal(&c); err != nil { |
||
97 | return nil, err |
||
98 | } |
||
99 | |||
100 | return &c, nil |
||
101 | } |
||
102 |