Initial commit, really just spams lol on a random topic
authorjweigele <jweigele@portofseattle>
Tue, 6 Dec 2022 02:40:31 +0000 (18:40 -0800)
committerjweigele <jweigele@portofseattle>
Tue, 6 Dec 2022 02:40:31 +0000 (18:40 -0800)
Dockerfile [new file with mode: 0644]
go.mod [new file with mode: 0644]
wunder/main.go [new file with mode: 0644]

diff --git a/Dockerfile b/Dockerfile
new file mode 100644 (file)
index 0000000..f800b91
--- /dev/null
@@ -0,0 +1,22 @@
+# STEP 1 build executable binary
+FROM golang:alpine as builder
+# Install SSL ca certificates
+RUN apk update && apk add git && apk add ca-certificates
+# Create appuser
+RUN adduser -D -g '' appuser
+COPY . $GOPATH/src/mypackage/myapp/
+WORKDIR $GOPATH/src/mypackage/myapp/
+COPY ./go.mod ./
+#COPY ./go.sum ./
+RUN go get github.com/rabbitmq/amqp091-go
+RUN go mod download
+WORKDIR $GOPATH/src/mypackage/myapp/wunder
+
+#get dependancies
+#RUN go get -d -v
+#build the binary
+RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-w -s" -o /go/bin/wunder
+
+FROM alpine:edge
+COPY --from=builder /go/bin/wunder /bin/wunder
+ENTRYPOINT ["/bin/wunder"]
diff --git a/go.mod b/go.mod
new file mode 100644 (file)
index 0000000..fa5885d
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module unpiege.net/rabbit_go/wunder/m/v2
+
+go 1.18
diff --git a/wunder/main.go b/wunder/main.go
new file mode 100644 (file)
index 0000000..a9dc347
--- /dev/null
@@ -0,0 +1,78 @@
+package main
+
+import (
+       "context"
+       "crypto/tls"
+       "crypto/x509"
+       "encoding/json"
+       "fmt"
+       "io/ioutil"
+       "log"
+       "os"
+       "time"
+
+       amqp "github.com/rabbitmq/amqp091-go"
+)
+
+func failOnError(err error, msg string) {
+       if err != nil {
+               log.Panicf("%s: %s", msg, err)
+       }
+}
+
+func main() {
+       configJson, err := ioutil.ReadFile(os.Args[1])
+       if err != nil {
+               failOnError(err, "oh no read")
+       }
+       var result map[string]interface{}
+       json.Unmarshal([]byte(configJson), &result)
+       fmt.Println(result)
+
+       // no cert verification because we're self-signed
+       cfg := new(tls.Config)
+       cfg.RootCAs = x509.NewCertPool()
+       cfg.InsecureSkipVerify = true
+
+       conn, err := amqp.DialTLS(fmt.Sprintf("amqps://%s:%s@%s:5671/%s", result["user"], result["password"], result["host"], result["vhost"]), cfg)
+       failOnError(err, "Failed to connect to RabbitMQ")
+       defer conn.Close()
+
+       ch, err := conn.Channel()
+       failOnError(err, "Failed to open a channel")
+       defer ch.Close()
+
+       err = ch.ExchangeDeclare(
+               result["exchange"].(string), // name
+               amqp.ExchangeTopic,          // type
+               true,                        // durable
+               false,                       // autodelete
+               false,                       // internal (doesn't accept publish so...)
+               false,                       // block and wait for call to finish
+               nil,                         // optional args I don't know any
+       )
+       failOnError(err, "Failed exchange stuff")
+
+       err = ch.Confirm(false) // wait for this to finish too, confirm delivery of messages
+       failOnError(err, "Failed confirm")
+
+       ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+       defer cancel()
+       send_obj := make(map[string]interface{})
+       send_obj["text"] = "Hello World!"
+
+       data, err := json.Marshal(send_obj)
+       failOnError(err, "oh no json marshal")
+
+       err = ch.PublishWithContext(ctx,
+               result["exchange"].(string), // exchange
+               "lol",                       // routing key
+               false,                       // mandatory
+               false,                       // immediate
+               amqp.Publishing{
+                       ContentType: "application/json",
+                       Body:        data,
+               })
+       failOnError(err, "Failed to publish a message")
+       log.Printf(" [x] Sent %s\n", data)
+}