forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_provider.go
59 lines (49 loc) · 1.48 KB
/
handler_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
57
58
59
package mbus
import (
"net/url"
"github.com/cloudfoundry/yagnats"
boshhandler "github.com/cloudfoundry/bosh-agent/handler"
boshmicro "github.com/cloudfoundry/bosh-agent/micro"
boshplatform "github.com/cloudfoundry/bosh-agent/platform"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
boshdir "github.com/cloudfoundry/bosh-agent/settings/directories"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type HandlerProvider struct {
settingsService boshsettings.Service
logger boshlog.Logger
handler boshhandler.Handler
}
func NewHandlerProvider(
settingsService boshsettings.Service,
logger boshlog.Logger,
) (p HandlerProvider) {
p.settingsService = settingsService
p.logger = logger
return
}
func (p HandlerProvider) Get(
platform boshplatform.Platform,
dirProvider boshdir.Provider,
) (handler boshhandler.Handler, err error) {
if p.handler != nil {
handler = p.handler
return
}
mbusURL, err := url.Parse(p.settingsService.GetSettings().Mbus)
if err != nil {
err = bosherr.WrapError(err, "Parsing handler URL")
return
}
switch mbusURL.Scheme {
case "nats":
handler = NewNatsHandler(p.settingsService, yagnats.NewClient(), p.logger, platform)
case "https":
handler = boshmicro.NewHTTPSHandler(mbusURL, p.logger, platform.GetFs(), dirProvider)
default:
err = bosherr.Errorf("Message Bus Handler with scheme %s could not be found", mbusURL.Scheme)
}
p.handler = handler
return
}