@@ -33,19 +33,26 @@ type Grape interface {
33
33
ExitFunc (call func (code int )) Grape
34
34
}
35
35
36
- type _grape struct {
37
- appName string
38
- configFilePath string
39
- pidFilePath string
40
- resolvers []config.Resolver
41
- configs Modules
42
- modules Modules
43
- packages container.TContainer
44
- logHandler * _log
45
- logger logx.Logger
46
- appContext xc.Context
47
- exitFunc func (code int )
48
- }
36
+ type (
37
+ _conf struct {
38
+ FileName string
39
+ Data string
40
+ DataExt string
41
+ }
42
+ _grape struct {
43
+ appName string
44
+ config _conf
45
+ pidFilePath string
46
+ resolvers []config.Resolver
47
+ configs Modules
48
+ modules Modules
49
+ packages container.TContainer
50
+ logHandler * _log
51
+ logger logx.Logger
52
+ appContext xc.Context
53
+ exitFunc func (code int )
54
+ }
55
+ )
49
56
50
57
// New create application
51
58
func New (appName string ) Grape {
@@ -82,7 +89,18 @@ func (a *_grape) Modules(modules ...interface{}) Grape {
82
89
83
90
// ConfigFile set config file path
84
91
func (a * _grape ) ConfigFile (filename string ) Grape {
85
- a .configFilePath = filename
92
+ a .config = _conf {
93
+ FileName : filename ,
94
+ }
95
+ return a
96
+ }
97
+
98
+ // ConfigData set config data
99
+ func (a * _grape ) ConfigData (data , ext string ) Grape {
100
+ a .config = _conf {
101
+ Data : data ,
102
+ DataExt : ext ,
103
+ }
86
104
return a
87
105
}
88
106
@@ -218,12 +236,15 @@ func (a *_grape) prepareConfig(interactive bool) {
218
236
219
237
// read config file
220
238
resolver := config .New (a .resolvers ... )
221
- if len (a .configFilePath ) > 0 {
222
- console .FatalIfErr (resolver .OpenFile (a .configFilePath ), "Open config file: %s" , a .configFilePath )
239
+ switch true {
240
+ case len (a .config .Data ) > 0 :
241
+ resolver .OpenBlob (a .config .Data , a .config .DataExt )
242
+ case len (a .config .FileName ) > 0 :
243
+ console .FatalIfErr (resolver .OpenFile (a .config .FileName ), "Open config file: %s" , a .config )
223
244
}
224
- console .FatalIfErr (resolver .Build (), "Prepare config file: %s" , a .configFilePath )
245
+ console .FatalIfErr (resolver .Build (), "Prepare config file: %s" , a .config )
225
246
if ! interactive {
226
- console .FatalIfErr (resolver .Decode (appConfig ), "Decode config file: %s" , a .configFilePath )
247
+ console .FatalIfErr (resolver .Decode (appConfig ), "Decode config file: %s" , a .config )
227
248
}
228
249
229
250
// init logger
@@ -242,7 +263,7 @@ func (a *_grape) prepareConfig(interactive bool) {
242
263
configs , err = reflect .TypingPtr (a .configs , func (c interface {}) error {
243
264
return resolver .Decode (c )
244
265
})
245
- console .FatalIfErr (err , "Decode config file: %s" , a .configFilePath )
266
+ console .FatalIfErr (err , "Decode config file: %s" , a .config )
246
267
a .modules = a .modules .Add (configs ... )
247
268
248
269
if ! interactive && len (a .pidFilePath ) > 0 {
0 commit comments