-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (72 loc) · 1.73 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
package main
import (
"drops/pkg/utils"
"fmt"
"io"
"net/http"
"os"
"strconv"
"time"
"github.com/gorilla/mux"
)
func main() {
fmt.Print("service listen on port:3000")
r := mux.NewRouter()
r.HandleFunc("/", onWebhook)
r.Use(webhookMsgMiddleware)
srv := &http.Server{
Addr: ":3000",
// Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: r, // Pass our instance of gorilla/mux in.
}
if err := srv.ListenAndServe(); err != nil {
panic(err)
}
}
func onWebhook(w http.ResponseWriter, req *http.Request) {
webHookBody, err := io.ReadAll(req.Body)
if err != nil {
fmt.Print(err)
return
}
fmt.Print(string(webHookBody))
secret := os.Getenv("SECRET")
signatureHeader := req.Header.Get("X-Hub-Signature-256")
if !utils.ValidateSignature(webHookBody, signatureHeader, secret) {
fmt.Print("event signature is not valid")
}
appID := os.Getenv("APP_ID")
appid, err := strconv.Atoi(appID)
if err != nil {
return
}
installtionID := os.Getenv("INSTALLTION_ID")
iid, err := strconv.Atoi(installtionID)
if err != nil {
return
}
pemFilePath := os.Getenv("PRIVATE_KEY_PATH")
ittt, resp, err := utils.GetAppInstallationToken(pemFilePath, int64(appid), int64(iid))
if err != nil {
return
}
if resp.StatusCode >= 300 {
return
}
url := "https://api.github.com/repos/kumulo-nimbus/drops/issues"
token := .GetToken()
resource, err := utils.AccessResourceViaRest(url, token)
if err != nil {
return
}
fmt.Print(resource)
}
func webhookMsgMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// validate hook
next.ServeHTTP(w, r)
})
}