From 7e045a1dbf39a372b0b738bff3775b2326cedafc Mon Sep 17 00:00:00 2001 From: 0x1d Date: Mon, 27 Oct 2025 15:18:44 +0100 Subject: [PATCH] feat: add Docker build --- .dockerignore | 6 +++++ Dockerfile | 31 +++++++++++++++++++++++ Makefile | 45 +++++++++++++++++++++++++++++++++ internal/discovery/discovery.go | 4 +-- main.go | 12 ++++++--- 5 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 Makefile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..7094a54 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +.git +.gitignore +.cursor +*.md +spore-gateway + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..138a0a2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +# Build stage +FROM golang:1.24-alpine AS builder + +WORKDIR /app + +# Copy go mod files +COPY go.mod go.sum ./ +RUN go mod download + +# Copy source code +COPY . . + +# Build the application +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o spore-gateway ./main.go + +# Runtime stage +FROM alpine:latest + +RUN apk --no-cache add ca-certificates + +WORKDIR /root/ + +# Copy the binary from builder +COPY --from=builder /app/spore-gateway . + +# Expose ports +EXPOSE 3001 4210 + +# Run the application +CMD ["./spore-gateway"] + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e7ffad4 --- /dev/null +++ b/Makefile @@ -0,0 +1,45 @@ +.PHONY: build run clean docker-build docker-run docker-push + +# Build the application +build: + go build -o spore-gateway main.go + +# Run the application +run: + go run main.go + +# Clean build artifacts +clean: + rm -f spore-gateway + +# Format code +fmt: + go fmt ./... + +# Lint code (requires golangci-lint) +lint: + golangci-lint run + +# Install dependencies +deps: + go mod download + go mod tidy + +# Docker variables +DOCKER_REGISTRY ?= +IMAGE_NAME = wirelos/spore-gateway +IMAGE_TAG ?= latest +FULL_IMAGE_NAME = $(if $(DOCKER_REGISTRY),$(DOCKER_REGISTRY)/$(IMAGE_NAME),$(IMAGE_NAME)):$(IMAGE_TAG) + +# Build Docker image +docker-build: + docker build -t $(FULL_IMAGE_NAME) . + +# Run Docker container +docker-run: + docker run --network host --rm $(FULL_IMAGE_NAME) + +# Push Docker image +docker-push: + docker push $(FULL_IMAGE_NAME) + diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index 60ddc14..7d1dcac 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -82,8 +82,8 @@ func (nd *NodeDiscovery) handleUDPMessage(message string, remoteAddr *net.UDPAdd fullMessage := "node/update:" + payload nd.handleNodeUpdate(remoteAddr.IP.String(), fullMessage) }, - "raw": func(payload string, remoteAddr *net.UDPAddr) { - nd.logger.WithField("message", "raw:"+payload).Debug("Received raw message") + "RAW": func(payload string, remoteAddr *net.UDPAddr) { + nd.logger.WithField("message", "RAW:"+payload).Debug("Received raw message") }, "cluster/event": func(payload string, remoteAddr *net.UDPAddr) { nd.handleClusterEvent(payload, remoteAddr) diff --git a/main.go b/main.go index b46925f..a3a3c59 100644 --- a/main.go +++ b/main.go @@ -65,9 +65,15 @@ func main() { // Initialize MQTT client if enabled var mqttClient *mqtt.MQTTClient - if *mqttServer != "" { - log.WithField("server", *mqttServer).Info("Initializing MQTT client") - mqttClient = mqtt.NewMQTTClientFromEnv(*mqttServer) + // Check for MQTT server from flag or environment variable + mqttServerURL := *mqttServer + if mqttServerURL == "" { + mqttServerURL = os.Getenv("MQTT_SERVER") + } + + if mqttServerURL != "" { + log.WithField("server", mqttServerURL).Info("Initializing MQTT client") + mqttClient = mqtt.NewMQTTClientFromEnv(mqttServerURL) // Set callback to forward MQTT messages to WebSocket mqttClient.SetMessageCallback(func(topic string, data []byte) {