forked from shanbay/gobay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
172 lines (147 loc) · 3.51 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package gobay
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"github.com/spf13/viper"
"github.com/hashicorp/go-multierror"
)
// A Key represents a key for a Extension.
type Key string
// Extension like db, cache
type Extension interface {
Object() interface{}
Application() *Application
Init(app *Application) error
Close() error
}
// Application struct
type Application struct {
rootPath string
env string
config *viper.Viper
extensions map[Key]Extension
initialized bool
closed bool
mu sync.Mutex
}
// Get the extension at the specified key, return nil when the component doesn't exist
func (d *Application) Get(key Key) Extension {
ext, _ := d.GetOK(key)
return ext
}
// GetOK the extension at the specified key, return false when the component doesn't exist
func (d *Application) GetOK(key Key) (Extension, bool) {
ext, ok := d.extensions[key]
if !ok {
return nil, ok
}
return ext, ok
}
func (d *Application) Env() string {
return d.env
}
// Config returns the viper config for this application
func (d *Application) Config() *viper.Viper {
return d.config
}
// Init the application and its extensions with the config.
func (d *Application) Init() error {
d.mu.Lock()
defer d.mu.Unlock()
if d.initialized {
return nil
}
if err := d.initConfig(); err != nil {
return err
}
if err := d.initExtensions(); err != nil {
return err
}
d.initialized = true
return nil
}
func (d *Application) initConfig() error {
configfile := filepath.Join(d.rootPath, "config.yaml")
originConfig, err := ioutil.ReadFile(configfile)
if err != nil {
return err
}
renderedConfig := []byte(os.ExpandEnv(string(originConfig)))
config := viper.New()
config.SetConfigType("yaml")
if err := config.ReadConfig(bytes.NewBuffer(renderedConfig)); err != nil {
return err
}
config = config.Sub(d.env)
// add default config
config.SetDefault("debug", false)
config.SetDefault("testing", false)
config.SetDefault("timezone", "UTC")
config.SetDefault("grpc_listen_host", "localhost")
config.SetDefault("grpc_listen_port", 6000)
config.SetDefault("openapi_listen_host", "localhost")
config.SetDefault("openapi_listen_port", 3000)
d.config = config
return nil
}
func (d *Application) initExtensions() error {
var allerr error
for key, ext := range d.extensions {
if err := ext.Init(d); err != nil {
allerr = multierror.Append(allerr, errors.New(string(key)), err)
}
}
return allerr
}
// Close close app when exit
func (d *Application) Close() error {
d.mu.Lock()
defer d.mu.Unlock()
if d.closed {
return nil
}
if err := d.closeExtensions(); err != nil {
return err
}
d.closed = true
return nil
}
func (d *Application) closeExtensions() error {
for _, ext := range d.extensions {
if err := ext.Close(); err != nil {
return err
}
}
return nil
}
// CreateApp create an gobay Application
func CreateApp(rootPath string, env string, exts map[Key]Extension) (*Application, error) {
if rootPath == "" || env == "" {
return nil, fmt.Errorf("lack of rootPath or env")
}
app := &Application{rootPath: rootPath, env: env, extensions: exts}
if err := app.Init(); err != nil {
return nil, err
}
return app, nil
}
func GetConfigByPrefix(config *viper.Viper, prefix string, trimPrefix bool) *viper.Viper {
subConfig := viper.New()
for k, v := range config.AllSettings() {
if !strings.HasPrefix(k, prefix) {
continue
}
key := k
if trimPrefix {
key = k[len(prefix):]
}
subConfig.SetDefault(key, v)
}
return subConfig
}