forked from novacloudcz/graphql-orm
/
config.go
67 lines (58 loc) · 1.51 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package model
import (
"io/ioutil"
"path"
"time"
"github.com/ghodss/yaml"
)
// Config struct
type Config struct {
Package string `json:"package"`
Connection *struct {
MaxIdleConnections *uint `json:"maxIdleConnections"`
ConnMaxLifetime *string `json:"connMaxLifetime"`
MaxOpenConnections *uint `json:"maxOpenConnections"`
} `json:"connection,omitempty"`
}
// LoadConfig loads config from path
func LoadConfig() (c Config, err error) {
return LoadConfigFromPath(".")
}
// LoadConfigFromPath ...
func LoadConfigFromPath(p string) (c Config, err error) {
configSource, err := ioutil.ReadFile(path.Join(p, "graphql-orm.yml"))
if err != nil {
return
}
err = yaml.Unmarshal(configSource, &c)
if err != nil {
return
}
return
}
// MaxIdleConnections ...
func (c *Config) MaxIdleConnections() uint {
if c.Connection != nil && (*c.Connection).MaxIdleConnections != nil {
return *(*c.Connection).MaxIdleConnections
}
return 5
}
// MaxOpenConnections ...
func (c *Config) MaxOpenConnections() uint {
if c.Connection != nil && (*c.Connection).MaxOpenConnections != nil {
return *(*c.Connection).MaxOpenConnections
}
return 10
}
// ConnMaxLifetime ...
func (c *Config) ConnMaxLifetime() float64 {
if c.Connection != nil && (*c.Connection).ConnMaxLifetime != nil {
val := *(*c.Connection).ConnMaxLifetime
dur, err := time.ParseDuration(val)
if err != nil {
panic("failed to parse config connMaxLifetime duration, error: " + err.Error())
}
return dur.Seconds()
}
return time.Minute.Seconds()
}