-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
119 lines (86 loc) · 2.56 KB
/
main.go
File metadata and controls
119 lines (86 loc) · 2.56 KB
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 (
"errors"
"github.com/gin-gonic/gin"
"os"
"strconv"
)
func main() {
// Initialize the context.
context := InitializeContext()
// Set up the router.
router := gin.Default()
router.GET("/redirect-link", context.RedirectToSlack)
router.GET("/authorization", context.ReceiveAuthorizationCode)
router.POST("/event", context.ReceiveSlackEvent)
// Start the server.
// If a certificate and key are not provided, the server will start in HTTP mode.
certificatePath := os.Getenv("EVENT_LISTENER_CERTIFICATE_PATH")
keyPath := os.Getenv("EVENT_LISTENER_KEY_PATH")
if certificatePath != "" && keyPath != "" {
router.RunTLS(":18075", certificatePath, keyPath)
} else {
router.Run(":18075")
}
}
func (context *EventListenerContext) RedirectToSlack(c *gin.Context) {
var accountId int
var clientId int
var err error
accountId, err = GetIntegerFromQuery(c, "account_id")
clientId, err = GetIntegerFromQuery(c, "client_id")
if accountId == 0 || clientId == 0 {
c.JSON(400, gin.H{
"error": "The account_id and client_id query parameters are required.",
})
}
slackLink, err := GetSlackLink(context, accountId, clientId)
if err != nil {
c.JSON(500, gin.H{
"error": err.Error(),
})
}
c.Redirect(302, slackLink)
}
func (context *EventListenerContext) ReceiveAuthorizationCode(c *gin.Context) {
code := c.Query("code")
state := c.Query("state")
_, err := ExchangeCodeForToken(context, code, state)
if err != nil {
c.JSON(500, gin.H{
"error": err.Error(),
})
return
}
c.JSON(204, gin.H{})
}
func (context *EventListenerContext) ReceiveSlackEvent(c *gin.Context) {
var body []byte
var response interface{}
var err error
body, err = c.GetRawData()
if err != nil {
c.JSON(500, gin.H{
"error": "Could not read the request body.",
})
}
response, err = ProcessSlackEvent(context, body)
if err != nil {
c.JSON(500, gin.H{
"error": err.Error(),
})
}
c.JSON(200, response)
}
func GetIntegerFromQuery(c *gin.Context, queryParam string) (int, error) {
queryValue := c.Query(queryParam)
if queryValue == "" {
errorMessage := "The query parameter " + queryParam + " is required."
return 0, errors.New(errorMessage)
}
intValue, err := strconv.Atoi(queryValue)
if err != nil {
return 0, err
}
return intValue, nil
}