2024-08-13 16:42:20 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
rabbitmq "github.com/rabbitmq/amqp091-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
func failOnError(err error, msg string) {
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("%s: %s", err, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
Topic string
|
2024-08-14 17:43:56 +02:00
|
|
|
Body []byte
|
2024-08-13 16:42:20 +02:00
|
|
|
}
|
|
|
|
|
2024-08-14 17:43:56 +02:00
|
|
|
var messageQueue chan Message
|
|
|
|
|
|
|
|
func PublicMessage(giteaWebhookType, giteaOrg string, msgBody io.Reader) error {
|
|
|
|
if messageQueue == nil {
|
|
|
|
return fmt.Errorf("Queue not initialized")
|
|
|
|
}
|
2024-08-13 16:42:20 +02:00
|
|
|
|
|
|
|
data, err := io.ReadAll(msgBody)
|
2024-08-14 17:43:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading JSON data. Err: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-08-13 16:42:20 +02:00
|
|
|
if !json.Valid(data) {
|
|
|
|
return fmt.Errorf("Invalid JSON in request")
|
|
|
|
}
|
|
|
|
|
2024-08-14 17:43:56 +02:00
|
|
|
messageQueue <- Message{
|
|
|
|
Topic: fmt.Sprintf("opensuse.gitea.%s.%s", giteaOrg, giteaWebhookType),
|
|
|
|
Body: data,
|
2024-08-13 16:42:20 +02:00
|
|
|
}
|
2024-08-14 17:43:56 +02:00
|
|
|
return nil
|
2024-08-13 16:42:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func ConnectToExchangeForPublish(host, username, password string) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
log.Print("recovering... reconnecting...\n")
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
go ConnectToExchangeForPublish(host, username, password)
|
|
|
|
}
|
|
|
|
}()
|
2024-08-14 17:43:56 +02:00
|
|
|
|
2024-08-13 16:42:20 +02:00
|
|
|
if messageQueue == nil {
|
|
|
|
messageQueue = make(chan Message, 10240)
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := ""
|
|
|
|
if len(username) > 0 && len(password) > 0 {
|
|
|
|
auth = username + ":" + password + "@"
|
|
|
|
}
|
|
|
|
|
|
|
|
connection, err := rabbitmq.DialTLS("amqps://"+auth+host, &tls.Config{
|
|
|
|
ServerName: host,
|
|
|
|
})
|
|
|
|
failOnError(err, "Cannot connect to rabbit.opensuse.org")
|
|
|
|
defer connection.Close()
|
|
|
|
|
|
|
|
ch, err := connection.Channel()
|
|
|
|
failOnError(err, "Cannot create a channel")
|
|
|
|
defer ch.Close()
|
|
|
|
|
|
|
|
err = ch.ExchangeDeclarePassive("pubsub", "topic", true, false, false, false, nil)
|
|
|
|
failOnError(err, "Cannot find pubsub exchange")
|
|
|
|
|
|
|
|
for {
|
|
|
|
msg, ok := <-messageQueue
|
|
|
|
if !ok {
|
2024-08-14 17:43:56 +02:00
|
|
|
log.Printf("Shutdown ... \n")
|
|
|
|
return
|
|
|
|
}
|
2024-08-13 16:42:20 +02:00
|
|
|
|
2024-08-14 17:43:56 +02:00
|
|
|
err = ch.Publish("pubsub", msg.Topic, false, false, rabbitmq.Publishing{
|
|
|
|
ContentType: "application/json",
|
|
|
|
AppId: RabbitForwarderPath,
|
|
|
|
Body: []byte(msg.Body),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2024-08-13 16:42:20 +02:00
|
|
|
if connection.IsClosed() {
|
2024-08-14 17:43:56 +02:00
|
|
|
select {
|
|
|
|
case messageQueue <- msg:
|
|
|
|
log.Printf("requed ...")
|
|
|
|
default:
|
|
|
|
log.Printf("queue full... message lost")
|
|
|
|
}
|
2024-08-13 16:42:20 +02:00
|
|
|
// reconnect
|
|
|
|
log.Printf("reconnecting...")
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
go ConnectToExchangeForPublish(host, username, password)
|
2024-08-14 17:43:56 +02:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
log.Printf("Error sending request. %v", err)
|
2024-08-13 16:42:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|