diff --git a/event_proxy/.env b/event_proxy/.env new file mode 100644 index 0000000..da768a5 --- /dev/null +++ b/event_proxy/.env @@ -0,0 +1,2 @@ +export MQTT_HOST="localhost" +export MQTT_PORT="5883" diff --git a/event_proxy/Dockerfile b/event_proxy/Dockerfile new file mode 100644 index 0000000..abf500d --- /dev/null +++ b/event_proxy/Dockerfile @@ -0,0 +1,30 @@ +# Build stage +FROM golang:1.25-alpine AS builder + +WORKDIR /app + +# Install git, gcc, musl-dev, and sqlite-dev for go mod and CGO +RUN apk add --no-cache git gcc musl-dev sqlite-dev + +# Copy go mod and sum files +COPY go.mod go.sum ./ + +# Download dependencies +RUN go mod download + +COPY ./*.go . + +# Enable CGO for go-sqlite3 +ENV CGO_ENABLED=1 + +RUN go build -o main . + +FROM alpine:latest + +WORKDIR /app + +COPY --from=builder /app/main . + +EXPOSE 8080 + +CMD ["./main"] diff --git a/event_proxy/build.sh b/event_proxy/build.sh new file mode 100755 index 0000000..780aee7 --- /dev/null +++ b/event_proxy/build.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +set -e + +export AWS_PROFILE=personal +export AWS_REGION=eu-central-1 + +REPO_NAME="homeassistant-event-proxy-homelabstack" + +if ! aws ecr describe-repositories --repository-names "$REPO_NAME" >/dev/null 2>&1; then + aws ecr create-repository --repository-name "$REPO_NAME" +fi + +docker buildx build --platform linux/amd64,linux/arm64 -t "853019563312.dkr.ecr.eu-central-1.amazonaws.com/$REPO_NAME:latest" --push . + +echo "Docker image built and pushed to AWS ECR" diff --git a/event_proxy/go.mod b/event_proxy/go.mod index 5e8e747..c51cddd 100644 --- a/event_proxy/go.mod +++ b/event_proxy/go.mod @@ -2,8 +2,9 @@ module event_proxy go 1.25.0 +require github.com/eclipse/paho.mqtt.golang v1.5.1 + require ( - github.com/eclipse/paho.mqtt.golang v1.5.1 // indirect github.com/gorilla/websocket v1.5.3 // indirect golang.org/x/net v0.44.0 // indirect golang.org/x/sync v0.17.0 // indirect diff --git a/event_proxy/main.go b/event_proxy/main.go index 97bb6ec..2f3c44a 100644 --- a/event_proxy/main.go +++ b/event_proxy/main.go @@ -18,9 +18,20 @@ type App struct { } func getMQTTClient() mqtt.Client { - // TODO: get port and host from env vars + mqttHost := os.Getenv("MQTT_HOST") + if mqttHost == "" { + panic("MQTT_HOST environment variable not set") + } + mqttPort := os.Getenv("MQTT_PORT") + if mqttPort == "" { + panic("MQTT_PORT environment variable not set") + } + + brokerURL := "tcp://" + mqttHost + ":" + mqttPort + slog.Info("Connecting to MQTT", "broker", brokerURL) + opts := mqtt.NewClientOptions(). - AddBroker("tcp://localhost:5883"). + AddBroker(brokerURL). SetClientID("event-proxy") client := mqtt.NewClient(opts) @@ -29,11 +40,6 @@ func getMQTTClient() mqtt.Client { panic(token.Error()) } return client - - // // Publish a message - // token := client.Publish("homeassistant/sensor/bws/node1/state1", 0, false, `{"temp": 23.5, "humidity": 45, "pressure": 1013}`) - // token.Wait() - // client.Disconnect(250) } type Message struct {