-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice_provider.go
56 lines (46 loc) · 1.23 KB
/
service_provider.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
package http
import (
"fmt"
"github.com/goal-web/contracts"
"github.com/goal-web/supports/logs"
"github.com/goal-web/supports/utils"
"net/http"
)
type ServiceProvider struct {
app contracts.Application
RouteCollectors []interface{}
}
func (this *ServiceProvider) Stop() {
this.app.Call(func(dispatcher contracts.EventDispatcher, router contracts.Router) {
if err := router.Close(); err != nil {
logs.WithError(err).Info("Router 关闭报错")
}
dispatcher.Dispatch(&ServeClosed{})
})
}
func (this *ServiceProvider) Start() error {
for _, collector := range this.RouteCollectors {
this.app.Call(collector)
}
err := this.app.Call(func(router contracts.Router, config contracts.Config) error {
httpConfig := config.Get("http").(Config)
return router.Start(
utils.StringOr(
httpConfig.Address,
fmt.Sprintf("%s:%s", httpConfig.Host, utils.StringOr(httpConfig.Port, "8000")),
),
)
})[0].(error)
if err != nil && err != http.ErrServerClosed {
logs.WithError(err).Error("http 服务无法启动")
this.app.Stop()
return err
}
return nil
}
func (this *ServiceProvider) Register(app contracts.Application) {
this.app = app
app.Singleton("Router", func() contracts.Router {
return New(this.app)
})
}