Total Lines | 87 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package config |
||
2 | |||
3 | import ( |
||
4 | "github.com/memnix/memnix-rest/pkg/oauth" |
||
5 | "github.com/pkg/errors" |
||
6 | "github.com/spf13/viper" |
||
7 | ) |
||
8 | |||
9 | type Config struct { |
||
10 | Server ServerConfigStruct |
||
11 | Database DatabaseConfigStruct |
||
12 | Redis RedisConfigStruct |
||
13 | Log LogConfigStruct |
||
14 | Auth AuthConfigStruct |
||
15 | Sentry SentryConfigStruct |
||
16 | } |
||
17 | |||
18 | type SentryConfigStruct struct { |
||
19 | Debug bool |
||
20 | Environment string |
||
21 | Release string |
||
22 | TracesSampleRate float64 |
||
23 | ProfilesSampleRate float64 |
||
24 | DSN string |
||
25 | } |
||
26 | |||
27 | // ServerConfigStruct is the server configuration. |
||
28 | type ServerConfigStruct struct { |
||
29 | Port string |
||
30 | AppVersion string |
||
31 | JaegerURL string |
||
32 | Host string |
||
33 | FrontendURL string |
||
34 | } |
||
35 | |||
36 | // DatabaseConfigStruct is the database configuration. |
||
37 | type DatabaseConfigStruct struct { |
||
38 | DSN string |
||
39 | } |
||
40 | |||
41 | // RedisConfigStruct is the redis configuration. |
||
42 | type RedisConfigStruct struct { |
||
43 | Addr string |
||
44 | Password string |
||
45 | MinIdleConns int |
||
46 | PoolSize int |
||
47 | PoolTimeout int |
||
48 | } |
||
49 | |||
50 | // LogConfigStruct is the log configuration. |
||
51 | type LogConfigStruct struct { |
||
52 | Level string |
||
53 | } |
||
54 | |||
55 | // AuthConfigStruct is the auth configuration. |
||
56 | type AuthConfigStruct struct { |
||
57 | JWTSecret string |
||
58 | JWTHeaderLen int |
||
59 | JWTExpiration int |
||
60 | Discord oauth.DiscordConfig |
||
61 | Github oauth.GithubConfig |
||
62 | Bcryptcost int |
||
63 | } |
||
64 | |||
65 | // UseConfig loads a file from given path |
||
66 | func UseConfig(filename string) (*Config, error) { |
||
67 | v := viper.New() |
||
68 | |||
69 | v.SetConfigName(filename) |
||
70 | v.AddConfigPath(".") |
||
71 | v.AutomaticEnv() |
||
72 | if err := v.ReadInConfig(); err != nil { |
||
73 | var configFileNotFoundError viper.ConfigFileNotFoundError |
||
74 | if errors.As(err, &configFileNotFoundError) { |
||
75 | return nil, errors.New("config file not found") |
||
76 | } |
||
77 | return nil, err |
||
78 | } |
||
79 | |||
80 | var c Config |
||
81 | |||
82 | err := v.Unmarshal(&c) |
||
83 | if err != nil { |
||
84 | return nil, err |
||
85 | } |
||
86 | |||
87 | return &c, nil |
||
88 | } |
||
89 |