about summary refs log tree commit diff
path: root/src/rest-api
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2024-05-02 12:47:26 +0200
committerBaitinq <manuelpalenzuelamerino@gmail.com>2024-05-02 15:39:09 +0200
commita0f2eab19883bee5e80343409764d0a79c6adeb4 (patch)
treeb73dcac49afe016a38819c9acb24663c3789f207 /src/rest-api
parentrest-api: Create specific payload endpoint (diff)
downloadfs-tracer-backend-a0f2eab19883bee5e80343409764d0a79c6adeb4.tar.gz
fs-tracer-backend-a0f2eab19883bee5e80343409764d0a79c6adeb4.tar.bz2
fs-tracer-backend-a0f2eab19883bee5e80343409764d0a79c6adeb4.zip
Start setting up rabbitmq connection
Diffstat (limited to 'src/rest-api')
-rw-r--r--src/rest-api/cmd/BUILD.bazel1
-rw-r--r--src/rest-api/cmd/main.go44
2 files changed, 45 insertions, 0 deletions
diff --git a/src/rest-api/cmd/BUILD.bazel b/src/rest-api/cmd/BUILD.bazel
index f72cc99..d91e054 100644
--- a/src/rest-api/cmd/BUILD.bazel
+++ b/src/rest-api/cmd/BUILD.bazel
@@ -7,6 +7,7 @@ go_library(
     srcs = ["main.go"],
     importpath = "github.com/Baitinq/rest-api/src/rest-api/cmd",
     visibility = ["//visibility:private"],
+    deps = ["@com_github_rabbitmq_amqp091_go//:go_default_library"],
 )
 
 go_binary(
diff --git a/src/rest-api/cmd/main.go b/src/rest-api/cmd/main.go
index 2fcd1ec..37182c1 100644
--- a/src/rest-api/cmd/main.go
+++ b/src/rest-api/cmd/main.go
@@ -1,13 +1,57 @@
 package main
 
 import (
+	"context"
 	"fmt"
 	"io"
 	"log"
 	"net/http"
+	"os"
+	"time"
+
+	amqp "github.com/rabbitmq/amqp091-go"
 )
 
 func main() {
+	rabbitmq_password, ok := os.LookupEnv("RABBITMQ_PASSWORD")
+	if !ok {
+		panic("RABBITMQ_PASSWORD not set")
+	}
+	log.Println("RabbitMQ password", rabbitmq_password)
+	conn, err := amqp.Dial(fmt.Sprintf("amqp://user:%s@rabbitmq:5672/", rabbitmq_password))
+	if err != nil {
+		panic(err)
+	}
+	defer conn.Close()
+
+	ch, err := conn.Channel()
+	if err != nil {
+		panic(err)
+	}
+	defer ch.Close()
+
+	q, err := ch.QueueDeclare(
+		"hello", // name
+		false,   // durable
+		false,   // delete when unused
+		false,   // exclusive
+		false,   // no-wait
+		nil,     // arguments
+	)
+	if err != nil {
+		panic(err)
+	}
+	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+	defer cancel()
+
+	body := "Hello World!"
+	ch.PublishWithContext(ctx, "", q.Name, false, false, amqp.Publishing{
+		ContentType: "text/plain",
+		Body:        []byte(body),
+	})
+
+	log.Println(" [x] Sent", body)
+
 	mux := http.NewServeMux()
 	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 		fmt.Fprint(w, "Hello folks!")