-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (100 loc) · 3.12 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"flag"
"log"
"os"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/robfig/cron/v3"
)
type Config struct {
OrgName string
LogoURL string
FaviconURL string
SlackTeamID string
SlackAccessToken string
SlackAppToken string
SlackStatusChannelID string
SlackForwardChannelID string
SlackBotID string
SlackTruncation string
StatusNeutralColor string
StatusOKColor string
StatusOKEmoji string
StatusWarnColor string
StatusWarnEmoji string
StatusErrorColor string
StatusErrorEmoji string
NominalMessage string
NominalSentBy string
HelpMessage string
ReminderSchedule string
}
// Useful global variables
var config Config
func init() {
// Load environment variables one way or another
err := godotenv.Load()
if err != nil {
log.Println("Couldn't load .env file")
}
config.OrgName = os.Getenv("CSP_ORG_NAME")
config.LogoURL = os.Getenv("CSP_LOGO_URL")
config.FaviconURL = os.Getenv("CSP_FAVICON_URL")
config.SlackTeamID = os.Getenv("CSP_SLACK_TEAMID")
config.SlackAccessToken = os.Getenv("CSP_SLACK_ACCESS_TOKEN")
config.SlackAppToken = os.Getenv("CSP_SLACK_APP_TOKEN")
config.SlackStatusChannelID = os.Getenv("CSP_SLACK_STATUS_CHANNEL")
config.SlackForwardChannelID = os.Getenv("CSP_SLACK_FORWARD_CHANNEL")
config.SlackTruncation = os.Getenv("CSP_SLACK_TRUNCATION")
config.StatusNeutralColor = os.Getenv("CSP_CARD_NEUTRAL_COLOR")
config.StatusOKColor = os.Getenv("CSP_CARD_OK_COLOR")
config.StatusOKEmoji = os.Getenv("CSP_CARD_OK_EMOJI")
config.StatusWarnColor = os.Getenv("CSP_CARD_WARN_COLOR")
config.StatusWarnEmoji = os.Getenv("CSP_CARD_WARN_EMOJI")
config.StatusErrorColor = os.Getenv("CSP_CARD_ERROR_COLOR")
config.StatusErrorEmoji = os.Getenv("CSP_CARD_ERROR_EMOJI")
config.NominalMessage = os.Getenv("CSP_NOMINAL_MESSAGE")
config.NominalSentBy = os.Getenv("CSP_NOMINAL_SENT_BY")
config.HelpMessage = os.Getenv("CSP_HELP_LINK")
config.ReminderSchedule = os.Getenv("CSP_REMINDER_SCHEDULE")
}
func main() {
useSlack := flag.Bool("slack", true, "Launch an instance of CSP to connect to Slack")
pinReminders := flag.Bool("send-reminders", false, "Check for pinned items and send a reminder if it's been longer than a day.")
sendRemindersNow := flag.Bool("remind-now", false, "Send reminders right away.")
flag.Parse()
var csp CSPService
if *useSlack {
cspSlack, err := NewCSPSlack()
csp = &cspSlack
if err != nil {
log.Fatalf("Could not set up new CSPSlack service. %s", err)
}
}
if *sendRemindersNow {
err := csp.SendReminders(true)
if err != nil {
log.Printf("Could not send reminders: %s\n", err)
}
os.Exit(0)
}
if *pinReminders {
log.Printf("Setting up reminders. Schedule is %s\n", config.ReminderSchedule)
c := cron.New()
c.AddFunc(config.ReminderSchedule, func() {
err := csp.SendReminders(false)
if err != nil {
log.Printf("Cronjob returned error: %s\n", err)
}
})
c.Start()
}
go csp.Run()
web := gin.Default()
web.LoadHTMLGlob("templates/*")
web.Static("/static", "./static")
web.GET("/", csp.StatusPage)
web.GET("/health", health)
_ = web.Run()
}