forked from steffen25/golang.zone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
52 lines (45 loc) · 1.06 KB
/
config.go
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
44
45
46
47
48
49
50
51
52
package config
import (
"encoding/json"
"log"
"os"
)
type MySQLConfig struct {
Username string `json:"username"`
Password string `json:"password"`
DatabaseName string `json:"database"`
Encoding string `json:"encoding"`
Host string `json:"host"`
Port string `json:"port"`
}
type RedisConfig struct {
Host string `json:"host"`
Post int `json:"port"`
}
type JWTConfig struct {
Secret string `json:"secret"`
PublicKeyPath string `json:"public_key_path"`
PrivateKeyPath string `json:"private_key_path"`
}
type Config struct {
Env string `json:"env"`
MySQL MySQLConfig `json:"mysql"`
Redis RedisConfig `json:"redis"`
JWT JWTConfig `json:"jwt"`
Port int `json:"port"`
}
// New creates a new config by reading a json file that matches the types above
func New(path string) (Config, error) {
file, err := os.Open(path)
defer file.Close()
if err != nil {
log.Fatal(err)
}
decoder := json.NewDecoder(file)
cfg := Config{}
err = decoder.Decode(&cfg)
if err != nil {
log.Fatal(err)
}
return cfg, nil
}