-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
65 lines (54 loc) · 1.55 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
package hasura_api
import (
"github.com/gookit/config/v2"
"github.com/gookit/config/v2/yaml"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type HasuraConfig struct {
Version int `yaml:"version"`
Endpoint string `yaml:"endpoint"`
MetadataDirectory string `yaml:"metadata_directory"`
Actions Actions `yaml:"actions"`
}
type Actions struct {
Kind string `yaml:"kind"`
HandlerWebhookBaseurl string `yaml:"handler_webhook_baseurl"`
}
func defaultConfig() *HasuraConfig {
return &HasuraConfig{
Version: 3,
Endpoint: config.Getenv("HASURA_GRAPHQL_ENDPOINT", "http://localhost:8080"),
MetadataDirectory: config.Getenv("HASURA_METADATA_DIRECTORY", "metadata"),
Actions: Actions{
Kind: config.Getenv("HASURA_ACTIONS_KIND", "synchronous"),
HandlerWebhookBaseurl: config.Getenv("HASURA_ACTIONS_HANDLER_WEBHOOK_BASEURL", "http://localhost:3000"),
},
}
}
func ConfigFromFile(filepath ...string) (*HasuraConfig, error) {
config.WithOptions(config.ParseEnv)
config.AddDriver(yaml.Driver)
config.WithOptions(func(opt *config.Options) {
opt.TagName = "yaml"
})
if len(filepath) < 1 {
return defaultConfig(), nil
}
err := config.LoadFiles(filepath...)
if err == nil {
c := new(HasuraConfig)
return c, config.BindStruct("", c)
}
if err != nil {
logrus.Error(errors.WithStack(err))
}
return defaultConfig(), nil
}
func MustConfigFromFile(filepath string) *HasuraConfig {
c, err := ConfigFromFile(filepath)
if err != nil {
panic(err)
}
return c
}