2024-04-29 22:24:00 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-05-06 17:03:56 +02:00
|
|
|
"log"
|
2024-04-29 22:24:00 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-05-06 17:03:56 +02:00
|
|
|
"path"
|
|
|
|
"runtime"
|
2024-04-29 22:24:00 +02:00
|
|
|
|
|
|
|
"gitea.urkob.com/urko/gitea-webhook-listener/kit/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-05-06 17:19:58 +02:00
|
|
|
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)
|
2024-04-29 22:24:00 +02:00
|
|
|
if err != nil {
|
2024-05-06 17:03:56 +02:00
|
|
|
log.Fatalf("Error loading config: %v", err)
|
2024-04-29 22:24:00 +02:00
|
|
|
}
|
2024-05-06 17:03:56 +02:00
|
|
|
http.HandleFunc("/", handlePayload(cfg.Secret, cfg.Scripts))
|
2024-04-29 22:24:00 +02:00
|
|
|
http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil)
|
|
|
|
}
|
|
|
|
|
2024-05-06 17:03:56 +02:00
|
|
|
func handlePayload(secret string, scripts map[string]config.ConfigScript) func(w http.ResponseWriter, r *http.Request) {
|
2024-04-29 22:24:00 +02:00
|
|
|
return (func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Read the request body
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Failed to read request body", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
2024-05-06 17:03:56 +02:00
|
|
|
authHeader := r.Header.Get("Authorization")
|
|
|
|
log.Println("authHeader", authHeader)
|
|
|
|
if authHeader != secret {
|
2024-04-29 22:24:00 +02:00
|
|
|
http.Error(w, "Signatures didn't match", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-06 17:03:56 +02:00
|
|
|
if !r.URL.Query().Has("project") {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
project := r.URL.Query().Get("project")
|
|
|
|
scr, found := scripts[project]
|
|
|
|
if !found {
|
|
|
|
http.Error(w, "not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("body", body)
|
2024-04-29 22:24:00 +02:00
|
|
|
// Parse the JSON payload
|
|
|
|
var payload interface{}
|
|
|
|
err = json.Unmarshal(body, &payload)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Failed to parse JSON payload", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Do something with the payload
|
|
|
|
fmt.Fprintf(w, "I got some JSON: %v", payload)
|
|
|
|
|
2024-05-06 17:03:56 +02:00
|
|
|
if err := execute(scr.BinaryPath, scr.ScriptPath); err != nil {
|
2024-04-29 22:24:00 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func execute(binaryPath, scriptPath string) error {
|
|
|
|
cmd := exec.Command(binaryPath, scriptPath)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return fmt.Errorf("cmd.Run %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|