forked from sublinks/sublinks-federation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-send.go
123 lines (106 loc) · 3.03 KB
/
test-send.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"context"
"fmt"
"os"
"time"
amqp "github.com/rabbitmq/amqp091-go"
"sublinks/sublinks-federation/internal/log"
"github.com/joho/godotenv"
)
func failOnError(err error, msg string) {
if err != nil {
logger := log.NewLogger("test-send")
logger.Panic().Err(err).Msg(msg)
}
}
func main() {
// bootstrap logger
logger := log.NewLogger("test-send")
// Load connection string from .env file
err := godotenv.Load()
if err != nil {
logger.Warn(fmt.Sprintf("failed to load env, %v", err))
}
// Get the connection string from the environment variable
amqpServerURL := os.Getenv("AMQP_SERVER_URL")
// Create a new RabbitMQ connection.
conn, err := amqp.Dial(amqpServerURL)
failOnError(err, "Failed to connect to RabbitMQ")
defer conn.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
sendMessage(conn, ctx, "actor.create", `
{
"actor_type": "Person",
"id": "https://sublinks.org/u/lazyguru",
"username": "lazyguru",
"name": "Lazyguru",
"bio": "I am a lazy guru",
"matrix_user_id": "@lazyguru:discuss.online",
"private_key": "some super secure string",
"public_key": "the public key"
}`)
sendMessage(conn, ctx, "actor.create", `
{
"actor_type": "Group",
"id": "https://sublinks.org/c/test_community",
"username": "test_community",
"name": "Test Community",
"private_key": "some super secure string",
"public_key": "the public key",
"sensitive": false
}`)
sendMessage(conn, ctx, "post.create", `
{
"id": "https://sublinks.org/post/test-post-1",
"title": "This is a post",
"content": "I am a lazy guru",
"published": "2021-08-01T12:34:59Z",
"community": "https://sublinks.org/c/test_community",
"author": "https://sublinks.org/u/lazyguru"
}`)
}
func sendMessage(conn *amqp.Connection, ctx context.Context, routingKey string, body string) {
// bootstrap logger
logger := log.NewLogger("test-send")
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer ch.Close()
err = ch.PublishWithContext(ctx,
"federation", // exchange
routingKey, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "application/json",
Timestamp: time.Now(),
Body: []byte(body),
})
failOnError(err, "Failed to publish a message")
logger.Info(fmt.Sprintf("[x] Sent %s", body))
ch2, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer ch2.Close()
body = `
{
"id": "test-post-1",
"title": "This is a post",
"content": "I am a lazy guru",
"published": "2021-08-01T12:34:59Z",
"community": "https://sublinks.org/c/test-community",
"author": "https://sublinks.org/u/lazyguru"
}`
err = ch2.PublishWithContext(ctx,
"federation", // exchange
"post.create", // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "application/json",
Timestamp: time.Now(),
Body: []byte(body),
})
failOnError(err, "Failed to publish a message")
logger.Info(fmt.Sprintf("[x] Sent %s", body))
}