Better logging, comments

This commit is contained in:
Akbar Rahman 2021-08-06 14:53:22 +01:00
parent cf65488907
commit 4c8cb33c59

26
main.go
View File

@ -46,21 +46,26 @@ func main() {
} }
func webhookHandler(w http.ResponseWriter, r *http.Request) { func webhookHandler(w http.ResponseWriter, r *http.Request) {
// Check what service is specified in URL (/webhooks/{service}) and if it exists
serviceName := string(mux.Vars(r)["service"])
service, ok := c.Services[serviceName]
if !ok {
writeResponse(w, 404, "Service Not Found")
fmt.Printf("Service not found: %v\n", serviceName)
return
}
fmt.Printf("Got webhook for: %v\n", serviceName)
// Read payload or return 500 if that doesn't work out
payload := "" payload := ""
if p, err := ioutil.ReadAll(r.Body); err != nil { if p, err := ioutil.ReadAll(r.Body); err != nil {
writeResponse(w, 500, "Internal Server Error: Could not read payload") writeResponse(w, 500, "Internal Server Error: Could not read payload")
fmt.Println("Error: Could not read payload")
return return
} else { } else {
payload = string(p) payload = string(p)
} }
// check what service is specified in URL (/webhooks/{service}) and if it exists
service, ok := c.Services[string(mux.Vars(r)["service"])]
if !ok {
writeResponse(w, 404, "Service Not Found")
return
}
// Verify that signature provided matches signature calculated using secretsss // Verify that signature provided matches signature calculated using secretsss
signature := r.Header.Get(service.SignatureHeader) signature := r.Header.Get(service.SignatureHeader)
calculatedSignature := fmt.Sprintf( calculatedSignature := fmt.Sprintf(
@ -72,17 +77,16 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("calcuatedSignature = %v\n", calculatedSignature) fmt.Printf("calcuatedSignature = %v\n", calculatedSignature)
if signature != calculatedSignature && checkSignature { if signature != calculatedSignature && checkSignature {
writeResponse(w, 400, "Bad Request: Signatures do not match") writeResponse(w, 400, "Bad Request: Signatures do not match")
fmt.Println("Signatures do not match!")
return return
} }
// run test and script in parralel to prevent timing out // Run tests and script as goroutine to prevent timing out
go func(){ go func(){
// Run tests, immediately stop if one fails // Run tests, immediately stop if one fails
for _, test := range service.Tests { for _, test := range service.Tests {
if _, err := test.Execute(payload); err != nil { if _, err := test.Execute(payload); err != nil {
writeResponse(w, 409, fmt.Printf("Test failed(%v) for service %v\n", test, serviceName)
fmt.Sprintf("Conflict: Test failed: %v", err.Error()),
)
return return
} }
} }