Compare commits

..

No commits in common. "b11c8cbcbf65ed911e8d7bd7d2bf389c37668a2e" and "2255b1f158c4a736a2ae26eb7a966224492f934f" have entirely different histories.

2 changed files with 15 additions and 32 deletions

View File

@ -1,19 +0,0 @@
BINARY_DIR=bin
BINARY_NAME=webhook-listener
COVERAGE_DIR=coverage
lint:
golangci-lint run ./...
goreportcard:
goreportcard-cli -v
test:
go test ./...
test-coverage:
rm -rf ${COVERAGE_DIR}
mkdir ${COVERAGE_DIR}
go test -v -coverprofile ${COVERAGE_DIR}/cover.out ./...
go tool cover -html ${COVERAGE_DIR}/cover.out -o ${COVERAGE_DIR}/cover.html
build:
rm -rf ${BINARY_DIR}
mkdir ${BINARY_DIR}
env GOOS=linux CGO_ENABLED=0 GOARCH=amd64 go build -o ./${BINARY_DIR}/${BINARY_NAME} main.go

28
main.go
View File

@ -15,13 +15,9 @@ import (
)
func main() {
cfgFile := os.Getenv("CONFIG_FILE")
if cfgFile == "" {
// Get root path
_, filename, _, _ := runtime.Caller(0)
cfgFile = path.Join(path.Dir(filename), "configs", "app.yml")
}
cfg, err := config.LoadConfig(cfgFile)
// Get root path
_, filename, _, _ := runtime.Caller(0)
cfg, err := config.LoadConfig(path.Join(path.Dir(filename), "configs", "app.yml"))
if err != nil {
log.Fatalf("Error loading config: %v", err)
}
@ -40,6 +36,7 @@ func handlePayload(secret string, scripts map[string]config.ConfigScript) func(w
defer r.Body.Close()
authHeader := r.Header.Get("Authorization")
log.Println("authHeader", authHeader)
if authHeader != secret {
http.Error(w, "Signatures didn't match", http.StatusUnauthorized)
return
@ -56,17 +53,22 @@ func handlePayload(secret string, scripts map[string]config.ConfigScript) func(w
http.Error(w, "not found", http.StatusNotFound)
return
}
log.Println("body", body)
// Parse the JSON payload
var payload interface{}
if err = json.Unmarshal(body, &payload); err != nil {
err = json.Unmarshal(body, &payload)
if err != nil {
http.Error(w, "Failed to parse JSON payload", http.StatusBadRequest)
return
}
go func() {
if err := execute(scr.BinaryPath, scr.ScriptPath); err != nil {
log.Println(err)
}
}()
// TODO: Do something with the payload
fmt.Fprintf(w, "I got some JSON: %v", payload)
if err := execute(scr.BinaryPath, scr.ScriptPath); err != nil {
panic(err)
}
})
}