autogits/gitea-events-rabbitmq-publisher/main.go

62 lines
1.5 KiB
Go
Raw Normal View History

2024-08-13 16:42:20 +02:00
package main
import (
2024-08-14 17:43:56 +02:00
"fmt"
2024-08-13 16:42:20 +02:00
"log"
"net/http"
2024-08-14 17:43:56 +02:00
"os"
2024-08-13 16:42:20 +02:00
"src.opensuse.org/autogits/common"
)
const (
2024-08-14 17:43:56 +02:00
ListenAddr = "[::1]:8002"
2024-08-13 16:42:20 +02:00
RabbitForwarderPath = "rabbitmq-forwarder"
)
2024-08-14 17:43:56 +02:00
func connectToRabbitMQ() {
host := os.Getenv("RABBITMQ_HOST")
username := os.Getenv("RABBITMQ_USERNAME")
password := os.Getenv("RABBITMQ_PASSWORD")
if len(host) == 0 || len(username) == 0 || len(password) == 0 {
fmt.Printf("Missing RABBITMQ_HOST, RABBITMQ_USERNAME, RABBITMQ_PASSWORD")
os.Exit(1)
}
2024-08-14 17:58:22 +02:00
go ConnectToExchangeForPublish(host, username, password)
2024-08-14 17:43:56 +02:00
}
2024-08-13 16:42:20 +02:00
func main() {
common.RequireGiteaSecretToken()
2024-08-14 17:43:56 +02:00
connectToRabbitMQ()
2024-08-13 16:42:20 +02:00
2024-08-14 17:43:56 +02:00
http.HandleFunc("POST /rabbitmq-forwarder/{Org}", func(res http.ResponseWriter, req *http.Request) {
2024-08-13 16:42:20 +02:00
if len(req.Header.Get("Content-Type")) == 0 ||
req.Header["Content-Type"][0] != "application/json" ||
req.Method != "POST" {
res.WriteHeader(http.StatusInternalServerError)
return
}
hdr := req.Header[common.GiteaRequestHeader]
if len(hdr) != 1 {
// h.LogError("Unsupported number of %s headers: %d: %#v\n", GiteaRequestHeader, len(hdr), hdr)
// h.WriteError()
res.WriteHeader(http.StatusInternalServerError)
return
}
reqType := hdr[0]
2024-08-14 17:43:56 +02:00
err := PublicMessage(reqType, req.PathValue("Org"), req.Body)
2024-08-13 16:42:20 +02:00
if err != nil {
log.Printf("hook (%s) processing error: %v\n", reqType, err)
res.WriteHeader(http.StatusBadRequest)
} else {
res.WriteHeader(http.StatusOK)
}
})
log.Fatal(http.ListenAndServe(ListenAddr, nil))
}