-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
91 lines (75 loc) · 2.05 KB
/
module.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
package fxslack
import (
"net/http"
"github.com/ankorstore/yokai/config"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slacktest"
"go.uber.org/fx"
)
// ModuleName is the module name.
const ModuleName = "slack"
// FxSlack is the [Fx] slack module.
//
// [Fx]: https://github.com/uber-go/fx
var FxSlackModule = fx.Module(
ModuleName,
fx.Provide(
NewSlackTestServer,
NewSlackClient,
),
)
// FxSlackTestServerParam allows injection of the required dependencies in [NewSlackTestServer].
type FxSlackTestServerParam struct {
fx.In
LifeCycle fx.Lifecycle
Config *config.Config
}
// NewSlackTestServer returns a [slacktest.Server].
func NewSlackTestServer(p FxSlackTestServerParam) *slacktest.Server {
if p.Config.IsTestEnv() {
return slacktest.NewTestServer()
}
return nil
}
// FxSlackClientParam allows injection of the required dependencies in [NewSlackClient].
type FxSlackClientParam struct {
fx.In
LifeCycle fx.Lifecycle
HttpRoundTripper http.RoundTripper
Config *config.Config
TestServer *slacktest.Server
}
// NewSlackClient returns a [slack.Client].
func NewSlackClient(p FxSlackClientParam) *slack.Client {
if p.Config.IsTestEnv() {
return createTestClient(p)
} else {
return createClient(p)
}
}
func createClient(p FxSlackClientParam) *slack.Client {
httpClient := &http.Client{
Transport: p.HttpRoundTripper,
}
client := slack.New(
p.Config.GetString("modules.slack.auth_token"),
slack.OptionHTTPClient(httpClient),
slack.OptionAppLevelToken(p.Config.GetString("modules.slack.app_level_token")),
slack.OptionDebug(p.Config.AppDebug()),
)
return client
}
func createTestClient(p FxSlackClientParam) *slack.Client {
server := p.TestServer
httpClient := &http.Client{
Transport: p.HttpRoundTripper,
}
client := slack.New(
p.Config.GetString("modules.slack.auth_token"),
slack.OptionHTTPClient(httpClient),
slack.OptionAppLevelToken(p.Config.GetString("modules.slack.app_level_token")),
slack.OptionAPIURL(server.GetAPIURL()),
slack.OptionDebug(p.Config.AppDebug()),
)
return client
}