diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/rest-api/cmd/BUILD.bazel | 1 | ||||
-rw-r--r-- | src/rest-api/cmd/main.go | 44 |
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!") |