Skip to content

Commit

Permalink
works
Browse files Browse the repository at this point in the history
  • Loading branch information
zrthstr committed Nov 23, 2024
1 parent 92b11f4 commit 6d8af9b
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions nr-push/relay2rabbit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pushToRabbitmq
25 changes: 25 additions & 0 deletions nr-push/relay2rabbit/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
BINARY_NAME=pushToRabbitmq
GO_MOD_PATH=$(shell go list -m)

build:
go build -o $(BINARY_NAME) main.go

run: build
export RABBITMQ_URL="amqp://user:password@localhost:5672";\
echo '{"foo:1}\n{"bar":2}\n' | ./$(BINARY_NAME) | tee /dev/null

install-deps:
go mod tidy

clean:
rm -f $(BINARY_NAME)

all: build run



deploy:
make clean
make build
scp pushToRabbitmq tr:/tmp # home/user/tr-ops/nostroots-server/data/strfry-data/plugins/pushToRabbitmq
ssh tr 'sudo cp /tmp/pushToRabbitmq /home/user/tr-ops/nostroots-server/data/strfry-data/plugins'
5 changes: 5 additions & 0 deletions nr-push/relay2rabbit/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module relay2rabbit

go 1.22.4

require github.com/rabbitmq/amqp091-go v1.10.0 // indirect
2 changes: 2 additions & 0 deletions nr-push/relay2rabbit/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
115 changes: 115 additions & 0 deletions nr-push/relay2rabbit/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package main

import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"strings"

"github.com/rabbitmq/amqp091-go"
)

func GetEnvFromProc1(varName string) (string, error) {
data, err := os.ReadFile("/proc/1/environ")
if err != nil {
return "", fmt.Errorf("failed to read /proc/1/environ: %w", err)
}

// Split by null byte and search for the desired variable
for _, env := range strings.Split(string(data), "\x00") {
if strings.HasPrefix(env, varName+"=") {
return strings.TrimPrefix(env, varName+"="), nil
}
}
return "", fmt.Errorf("environment variable %s not found", varName)
}

type Event struct {
Event struct {
ID string `json:"id"`
Content string `json:"content"`
// ... other fields if needed ...
} `json:"event"`
}

func main() {
//connStr := os.Getenv("RABBITMQ_URL")
connStr, err := GetEnvFromProc1("RABBITMQ_URL")

if err != nil {
log.Fatalf("failed to read RABBITMQ_URL from /proc/1/environ\n")
}

conn, err := amqp091.Dial(connStr)
if err != nil {
log.Fatalf("Failed to connect to RabbitMQ: %s\n", err)
}
defer conn.Close()

ch, err := conn.Channel()
if err != nil {
log.Fatalf("Failed to open a channel: %s\n", err)
}
defer ch.Close()

q, err := ch.QueueDeclare(
"myQueue", // Name of the queue
false, // Durable (false = not durable, i.e., will not survive server restarts)
false, // AutoDelete (false = queue will not be deleted automatically)
false, // Exclusive (false = other connections can access it)
false, // NoWait (false = the server will confirm the declaration)
nil, // Arguments (empty map here)
)
if err != nil {
log.Fatalf("Failed to declare a queue: %s\n", err)
}

scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
fmt.Fprintf(os.Stderr, "Received input: %s\n", line)

var event Event
err := json.Unmarshal([]byte(line), &event)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing JSON: %s\n", err)
continue
}
fmt.Fprintf(os.Stderr, "Parsed Event ID: '%s'\n", event.Event.ID)

err = ch.Publish(
"",
q.Name,
false,
false,
amqp091.Publishing{
ContentType: "application/json",
Body: []byte(line),
},
)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to publish a message: %s\n", err)
continue
}
fmt.Fprintf(os.Stderr, "Published to RabbitMQ: %s\n", line)

response := map[string]string{
"action": "accept",
"id": event.Event.ID,
}
responseJSON, err := json.Marshal(response)
if err != nil {
fmt.Fprintf(os.Stderr, "Error marshaling response JSON: %s\n", err)
continue
}

fmt.Println(string(responseJSON))
fmt.Fprintf(os.Stderr, "Wrote to stdout: %s\n", string(responseJSON))
}

if err := scanner.Err(); err != nil {
log.Printf("Error reading from stdin: %s\n", err)
}
}

0 comments on commit 6d8af9b

Please sign in to comment.