feat: add Docker build

This commit is contained in:
2025-10-27 15:18:44 +01:00
parent 5e8474b43b
commit 7e045a1dbf
5 changed files with 93 additions and 5 deletions

6
.dockerignore Normal file
View File

@@ -0,0 +1,6 @@
.git
.gitignore
.cursor
*.md
spore-gateway

31
Dockerfile Normal file
View File

@@ -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"]

45
Makefile Normal file
View File

@@ -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)

View File

@@ -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)

12
main.go
View File

@@ -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) {