-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (93 loc) · 3.05 KB
/
main.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
package main
import (
"net/http"
"os"
"time"
"github.com/kelseyhightower/envconfig"
"github.com/nlopes/slack"
)
type (
configuration struct {
Port string `envconfig:"PORT" default:"3000"`
ChannelID string `envconfig:"CHANNEL_ID" required:"true"`
BotID string `envconfig:"BOT_ID" required:"true"`
BotToken string `envconfig:"BOT_TOKEN" required:"true"`
BotUsageURL string `envconfig:"BOT_USAGE_URL"`
VerificationToken string `envconfig:"VERIFICATION_TOKEN" required:"true"`
AllowEmailDomains []string `envconfig:"ALLOW_EMAIL_DOMAINS"`
EsaToken string `envconfig:"ESA_TOKEN" required:"true"`
EsaTeamName string `envconfig:"ESA_TEAM_NAME" required:"true"`
AdminIDs []string `envconfig:"ADMIN_IDS" required:"true"`
AdminGroupID string `envconfig:"ADMIN_GROUP_ID"`
AccountExpireMonth int `envconfig:"ACCOUNT_EXPIRE_MONTH" default:"6"`
Organizations []string `envconfig:"ORGANIZATIONS"`
}
)
const (
envPrefix = ""
)
var (
timeZone = time.FixedZone("JST", 9*60*60)
)
func main() {
// parse config
var conf configuration
if err := envconfig.Process(envPrefix, &conf); err != nil {
logger.Errorf("Failed to process env var: %s", err)
os.Exit(1)
}
accountExpireMonth := conf.AccountExpireMonth
if accountExpireMonth < 1 {
accountExpireMonth = 1
}
// setup
logger.Infof("Start slack event listening")
slackClient := slack.New(conf.BotToken)
bot, err := slackClient.GetUserInfo(conf.BotID)
if err != nil {
logger.Errorf("Failed to get bot profile: %s", err)
os.Exit(1)
}
esaClient, err := NewEsaClient(conf.EsaTeamName, conf.EsaToken)
if err != nil {
logger.Errorf("Failed to create esa client: %s", err)
os.Exit(1)
}
repository, err := NewRepository(slackClient, conf.AdminIDs, conf.AllowEmailDomains, conf.Organizations)
if err != nil {
logger.Errorf("Failed to create repository: %s", err)
os.Exit(1)
}
// listening slack event and response
listener := &MessageListener{
esaClient: esaClient,
slackClient: slackClient,
repository: repository,
channelID: conf.ChannelID,
botID: conf.BotID,
botName: bot.Name,
botUsageURL: conf.BotUsageURL,
accountExpireMonth: accountExpireMonth,
}
go listener.Run()
// register handler to receive interactive message responses from slack (kicked by user action)
auxMux := http.NewServeMux()
auxMux.Handle("/interaction", InteractionHandler{
esaClient: esaClient,
slackClient: slackClient,
repository: repository,
channelID: conf.ChannelID,
verificationToken: conf.VerificationToken,
adminIDs: conf.AdminIDs,
adminGroupID: conf.AdminGroupID,
})
auxMux.HandleFunc("/alive", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
logger.Infof("Server listening on :%s", conf.Port)
if err := http.ListenAndServe(":"+conf.Port, auxMux); err != nil {
logger.Errorf("Failed to server listening: %s", err)
os.Exit(1)
}
os.Exit(0)
}