From 937664dfba2f4d9f5d8a060d656f197db227c356a5a8b0204246df7819455e0c Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Wed, 11 Sep 2024 18:56:48 +0200 Subject: [PATCH] enable debug-only rabbitmq webhook hander --- gitea-events-rabbitmq-publisher/main.go | 27 ++++++++++++++++--------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/gitea-events-rabbitmq-publisher/main.go b/gitea-events-rabbitmq-publisher/main.go index b12d6fe..f433125 100644 --- a/gitea-events-rabbitmq-publisher/main.go +++ b/gitea-events-rabbitmq-publisher/main.go @@ -325,19 +325,24 @@ func parseRequestJSONOrg(reqType string, data []byte) (org *common.Organization, func main() { var listenAddr string var reqBearerToken string + var doPublish bool flag.BoolVar(&DebugMode, "debug", false, "enables debugging messages") flag.StringVar(&listenAddr, "listen", ListenAddrDef, "HTTP listen socket address for webhook events") flag.StringVar(&topicScope, "topic-domain", DefTopicDomain, "Default domain for RabbitMQ topics") flag.StringVar(&reqBearerToken, "token", "", "HTTP Bearer token to match") + flag.BoolVar(&doPublish, "publish", true, "Publish to RabbitMQ. Disable for testing.") flag.Parse() log.Println("Starting....") log.Printf(" * Debugging: %t\n", DebugMode) log.Printf(" * Listening: %s\n", listenAddr) log.Printf(" * Bearer token: %t\n", len(reqBearerToken) > 0) + log.Printf(" * Publish to RabbitMQ: %t\n", doPublish) - connectToRabbitMQ() + if doPublish { + connectToRabbitMQ() + } http.HandleFunc("POST /rabbitmq-forwarder", func(res http.ResponseWriter, req *http.Request) { if len(req.Header.Get("Content-Type")) == 0 || @@ -406,17 +411,19 @@ func main() { return } - err = PublishMessage(org.Username, reqType, extraAction, data) - if err != nil { - errorStr := fmt.Sprintf("hook (%s) processing error: %v\n", reqType, err) - res.Header().Add("Content-Type", "plain/text") - res.Write([]byte(errorStr)) - res.WriteHeader(http.StatusBadRequest) + if doPublish { + err = PublishMessage(org.Username, reqType, extraAction, data) + if err != nil { + errorStr := fmt.Sprintf("hook (%s) processing error: %v\n", reqType, err) + res.Header().Add("Content-Type", "plain/text") + res.Write([]byte(errorStr)) + res.WriteHeader(http.StatusBadRequest) - if DebugMode { - log.Println(errorStr) + if DebugMode { + log.Println(errorStr) + } + return } - return } res.WriteHeader(http.StatusOK) })