-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
119 lines (104 loc) · 3.18 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
package main
import (
"context"
"fmt"
"net/http"
"time"
"strconv"
"os"
"github.com/neufeldtech/secretmessage-go/pkg/secretmessage"
"github.com/neufeldtech/secretmessage-go/pkg/secretslack"
log "github.com/sirupsen/logrus"
_ "go.elastic.co/apm/module/apmgormv2"
postgres "go.elastic.co/apm/module/apmgormv2/driver/postgres"
"go.elastic.co/apm/module/apmhttp"
"golang.org/x/oauth2"
"gorm.io/gorm"
)
var (
defaultPort int64 = 8080
slackSigningSecretConfigKey = "slackSigningSecret"
slackClientIDConfigKey = "slackClientID"
slackClientSecretConfigKey = "slackClientSecret"
slackCallbackURLConfigKey = "slackCallbackURL"
legacyCryptoKeyConfigKey = "legacyCryptoKey"
appURLConfigKey = "appURL"
databaseURL = "databaseURL"
configMap = map[string]string{
slackSigningSecretConfigKey: os.Getenv("SLACK_SIGNING_SECRET"),
slackClientIDConfigKey: os.Getenv("SLACK_CLIENT_ID"),
slackClientSecretConfigKey: os.Getenv("SLACK_CLIENT_SECRET"),
slackCallbackURLConfigKey: os.Getenv("SLACK_CALLBACK_URL"),
legacyCryptoKeyConfigKey: os.Getenv("CRYPTO_KEY"),
appURLConfigKey: os.Getenv("APP_URL"),
databaseURL: os.Getenv("DATABASE_URL"),
}
)
func resolvePort() int64 {
portString := os.Getenv("PORT")
if portString == "" {
return defaultPort
}
port64, err := strconv.ParseInt(portString, 10, 64)
if err != nil {
return defaultPort
}
return port64
}
func main() {
tp, err := secretmessage.InitTracer(secretmessage.ServiceName)
if err != nil {
log.Fatal(err)
}
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
// Setup custom HTTP Client for calling Slack
secretslack.SetHTTPClient(apmhttp.WrapClient(
&http.Client{
Timeout: time.Second * 5,
},
))
for k, v := range configMap {
if v == "" {
log.Fatalf("error initializaing config. key %v was not set", k)
}
}
conf := secretmessage.Config{
Port: resolvePort(),
SlackToken: "",
SigningSecret: configMap[slackSigningSecretConfigKey],
AppURL: configMap[appURLConfigKey],
LegacyCryptoKey: configMap[legacyCryptoKeyConfigKey],
DatabaseURL: configMap[databaseURL],
OauthConfig: &oauth2.Config{
ClientID: configMap[slackClientIDConfigKey],
ClientSecret: configMap[slackClientSecretConfigKey],
RedirectURL: configMap[slackCallbackURLConfigKey],
Scopes: []string{"chat:write", "commands", "workflow.steps:execute"},
Endpoint: oauth2.Endpoint{
AuthURL: "https://slack.com/oauth/v2/authorize",
TokenURL: "https://slack.com/api/oauth.v2.access",
},
},
}
db, err := gorm.Open(postgres.Open(conf.DatabaseURL), &gorm.Config{})
if err != nil {
log.Fatal(err)
}
d, _ := db.DB()
d.SetMaxIdleConns(10)
d.SetMaxOpenConns(10)
db.AutoMigrate(secretmessage.Secret{})
db.AutoMigrate(secretmessage.Team{})
controller := secretmessage.NewController(
conf,
db,
)
go secretmessage.StayAwake(conf)
r := controller.ConfigureRoutes()
log.Infof("Booted and listening on port %v", conf.Port)
r.Run(fmt.Sprintf("0.0.0.0:%v", conf.Port))
}