/
config.go
48 lines (39 loc) · 980 Bytes
/
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
package common
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
type PostgresConfig struct {
Host string `json:"host"`
Port int `json:"port"`
User string `json:"user"`
Password string `json:"password"`
DBName string `json:"db_name"`
}
type SessionConfig struct {
Secret string `json:"secret"`
SessionKey string `json:"session_key"`
}
type Config struct {
Postgres PostgresConfig `json:"postgres_config"`
Session SessionConfig `json:"session_config"`
}
func NewConfigFromFile(fileName string) (*Config, error) {
jsonFile, err := os.Open(fileName)
if err != nil {
return nil, fmt.Errorf("failed open config file: %w", err)
}
defer jsonFile.Close()
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
return nil, fmt.Errorf("failed read config file: %w", err)
}
cfg := Config{}
err = json.Unmarshal(byteValue, &cfg)
if err != nil {
return nil, fmt.Errorf("failed unmarshal config: %w", err)
}
return &cfg, nil
}