-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoofy.go
197 lines (168 loc) · 4.43 KB
/
goofy.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package goofy
import (
stdContext "context"
"fmt"
"os"
"os/signal"
"path"
"path/filepath"
"reflect"
"runtime"
"strings"
"syscall"
"time"
"github.com/goava/di"
"github.com/gookit/event"
"github.com/gookit/gcli/v3"
"github.com/robfig/cron/v3"
"github.com/urionz/color"
"github.com/urionz/goofy/cache"
"github.com/urionz/goofy/cmds/dlv"
"github.com/urionz/goofy/cmds/repl"
"github.com/urionz/goofy/config"
"github.com/urionz/goofy/contracts"
"github.com/urionz/goofy/db"
"github.com/urionz/goofy/filesystem"
"github.com/urionz/goofy/log"
"github.com/urionz/goofy/redis"
"github.com/urionz/goofy/web"
)
var Default = New(SetWorkspace("./")).AddServices(
config.NewServiceProvider, log.NewServiceProvider,
redis.NewServiceProvider, db.NewServiceProvider,
cache.NewServiceProvider, filesystem.NewServiceProvider,
web.NewServiceProvider,
).AddCommanders(
contracts.FuncCommander(dlv.Command),
contracts.FuncCommander(repl.Command),
)
type Application struct {
di.Tags `name:"app"`
*cron.Cron
*event.Manager
*di.Container
*gcli.App
conf string
workspace string
services []interface{}
err error
}
var _ contracts.Application = (*Application)(nil)
func New(options ...Option) *Application {
var err error
app := &Application{
workspace: "./",
Cron: cron.New(),
App: gcli.NewApp(),
Manager: event.NewManager("goofy"),
}
if app.Container, err = di.New(di.ProvideValue(app, di.As(new(contracts.Application)))); err != nil {
panic(err)
}
app.App.GlobalFlags().StrOpt(&app.workspace, "workspace", "w", "./", "工作目录")
for _, option := range options {
option.apply(app)
}
go func() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGINT, os.Kill, syscall.SIGKILL, syscall.SIGTERM)
select {
case <-ch:
fmt.Println("shutdown...")
timeout := 10 * time.Second
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout)
defer cancel()
if err := app.Close(ctx); err != nil {
fmt.Println(err)
}
}
}()
return app
}
func (app *Application) Close(ctx stdContext.Context) error {
var closers []contracts.Closer
return app.Container.Iterate(&closers, func(tags di.Tags, loader di.ValueFunc) error {
i, _ := loader()
if provider, ok := i.(contracts.Closer); ok {
return provider.Close(ctx)
}
return nil
})
}
func (app *Application) DynamicConf(conf contracts.Config) error {
var services []contracts.DynamicConf
return app.Container.Iterate(&services, func(tags di.Tags, loader di.ValueFunc) error {
i, _ := loader()
return i.(contracts.DynamicConf).DynamicConf(app, conf)
})
}
func (app *Application) Storage() string {
return path.Join(app.Workspace(), "storage")
}
func (app *Application) Database() string {
return path.Join(app.Workspace(), "database")
}
func (app *Application) addError(err error) {
app.err = err
}
func (app *Application) Workspace() string {
return app.workspace
}
func (app *Application) Dir() string {
_, file, _, _ := runtime.Caller(1)
return filepath.Dir(file)
}
func (app *Application) Run() contracts.Application {
if err := recover(); err != nil {
color.Warnln(err)
}
if err := app.Container.Invoke(app.bootstrap); err != nil {
panic(err)
}
if len(os.Args) > 0 && !strings.HasSuffix(os.Args[0], "test") {
go func() {
if err := recover(); err != nil {
color.Warnln(err)
}
app.Cron.Run()
}()
app.App.Run(nil)
}
return app
}
func (app *Application) Call(name string, args ...string) error {
return app.App.Exec(name, args)
}
func (app *Application) Error() error {
return app.err
}
func (app *Application) bootstrap() error {
for _, service := range app.services {
outputs, err := app.resolveInputsFromDI(service)
if err != nil {
return err
}
if len(outputs) > 0 && !outputs[0].IsNil() {
return outputs[0].Interface().(error)
}
}
return nil
}
func (app *Application) resolveInputsFromDI(service interface{}) ([]reflect.Value, error) {
typeOf := reflect.TypeOf(service)
valueOf := reflect.ValueOf(service)
serviceArgNum := typeOf.NumIn()
inputs := make([]reflect.Value, serviceArgNum)
for i := 0; i < serviceArgNum; i++ {
newValue := reflect.New(typeOf.In(i))
ptr := newValue.Interface()
if err := app.Container.Resolve(ptr); err != nil {
return []reflect.Value{}, err
}
inputs[i] = newValue.Elem()
}
if len(os.Args) > 0 && !strings.HasSuffix(os.Args[0], "test") {
_ = app.App.GlobalFlags().Parse(os.Args[1:])
}
return valueOf.Call(inputs), nil
}