diff options
Diffstat (limited to 'src/rest-api/cmd/main.go')
-rw-r--r-- | src/rest-api/cmd/main.go | 44 |
1 files changed, 44 insertions, 0 deletions
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!") |