diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2024-05-02 12:47:26 +0200 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2024-05-02 15:39:09 +0200 |
commit | a0f2eab19883bee5e80343409764d0a79c6adeb4 (patch) | |
tree | b73dcac49afe016a38819c9acb24663c3789f207 | |
parent | rest-api: Create specific payload endpoint (diff) | |
download | fs-tracer-backend-a0f2eab19883bee5e80343409764d0a79c6adeb4.tar.gz fs-tracer-backend-a0f2eab19883bee5e80343409764d0a79c6adeb4.tar.bz2 fs-tracer-backend-a0f2eab19883bee5e80343409764d0a79c6adeb4.zip |
Start setting up rabbitmq connection
-rw-r--r-- | MODULE.bazel | 9 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | k8s/rest-api/templates/deployment.yaml | 6 | ||||
-rw-r--r-- | src/rest-api/cmd/BUILD.bazel | 1 | ||||
-rw-r--r-- | src/rest-api/cmd/main.go | 44 |
6 files changed, 61 insertions, 3 deletions
diff --git a/MODULE.bazel b/MODULE.bazel index 77f4b3a..c9c22d2 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -12,7 +12,6 @@ git_override( ) oci = use_extension("@rules_oci//oci:extensions.bzl", "oci") - oci.pull( name = "distroless_base", digest = "sha256:ccaef5ee2f1850270d453fdf700a5392534f8d1a8ca2acda391fbb6a06b81c86", @@ -24,3 +23,11 @@ oci.pull( ) use_repo(oci, "distroless_base") + +go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps") +go_deps.from_file(go_mod = "//:go.mod") + +use_repo( + go_deps, + "com_github_rabbitmq_amqp091_go", +) diff --git a/README.md b/README.md index 8608737..bcbc18a 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,5 @@ kubectl apply -f metallb_announce.yml helm install rabbitmq oci://registry-1.docker.io/bitnamicharts/rabbitmq -helm install rest-api . - bazel run //src/rest-api/cmd:push -- --tag "$(git rev-parse --short HEAD)" helm upgrade rest-api --set image.tag=$(git rev-parse --short HEAD) k8s/rest-api diff --git a/go.mod b/go.mod index fe8def9..21c4602 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/Baitinq/rest-api go 1.22.2 + +require github.com/rabbitmq/amqp091-go v1.9.0 // indirect diff --git a/k8s/rest-api/templates/deployment.yaml b/k8s/rest-api/templates/deployment.yaml index 6e58bc5..4db09db 100644 --- a/k8s/rest-api/templates/deployment.yaml +++ b/k8s/rest-api/templates/deployment.yaml @@ -47,6 +47,12 @@ spec: port: http resources: {{- toYaml .Values.resources | nindent 12 }} + env: + - name: RABBITMQ_PASSWORD + valueFrom: + secretKeyRef: + name: rabbitmq + key: rabbitmq-password {{- with .Values.nodeSelector }} nodeSelector: {{- toYaml . | nindent 8 }} 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!") |