forked from gofiber/recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (65 loc) · 1.45 KB
/
main.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
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
amqp "github.com/rabbitmq/amqp091-go"
)
func main() {
// Create a new RabbitMQ connection.
connRabbitMQ, err := amqp.Dial("amqp://user:password@localhost:5672/")
if err != nil {
panic(err)
}
// Create a new Fiber instance.
app := fiber.New()
// Add middleware.
app.Use(
logger.New(), // add simple logger
)
// Add route.
app.Get("/send", func(c *fiber.Ctx) error {
// Checking, if query is empty.
if c.Query("msg") == "" {
log.Println("Missing 'msg' query parameter")
}
// Let's start by opening a channel to our RabbitMQ instance
// over the connection we have already established
ch, err := connRabbitMQ.Channel()
if err != nil {
return err
}
defer ch.Close()
// With this channel open, we can then start to interact.
// With the instance and declare Queues that we can publish and subscribe to.
_, err = ch.QueueDeclare(
"TestQueue",
true,
false,
false,
false,
nil,
)
// Handle any errors if we were unable to create the queue.
if err != nil {
return err
}
// Attempt to publish a message to the queue.
err = ch.Publish(
"",
"TestQueue",
false,
false,
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(c.Query("msg")),
},
)
if err != nil {
return err
}
return nil
})
// Start Fiber API server.
log.Fatal(app.Listen(":3000"))
}