feat: add Docker build
This commit is contained in:
6
.dockerignore
Normal file
6
.dockerignore
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
.git
|
||||||
|
.gitignore
|
||||||
|
.cursor
|
||||||
|
*.md
|
||||||
|
spore-gateway
|
||||||
|
|
||||||
31
Dockerfile
Normal file
31
Dockerfile
Normal 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
45
Makefile
Normal 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)
|
||||||
|
|
||||||
@@ -82,8 +82,8 @@ func (nd *NodeDiscovery) handleUDPMessage(message string, remoteAddr *net.UDPAdd
|
|||||||
fullMessage := "node/update:" + payload
|
fullMessage := "node/update:" + payload
|
||||||
nd.handleNodeUpdate(remoteAddr.IP.String(), fullMessage)
|
nd.handleNodeUpdate(remoteAddr.IP.String(), fullMessage)
|
||||||
},
|
},
|
||||||
"raw": func(payload string, remoteAddr *net.UDPAddr) {
|
"RAW": func(payload string, remoteAddr *net.UDPAddr) {
|
||||||
nd.logger.WithField("message", "raw:"+payload).Debug("Received raw message")
|
nd.logger.WithField("message", "RAW:"+payload).Debug("Received raw message")
|
||||||
},
|
},
|
||||||
"cluster/event": func(payload string, remoteAddr *net.UDPAddr) {
|
"cluster/event": func(payload string, remoteAddr *net.UDPAddr) {
|
||||||
nd.handleClusterEvent(payload, remoteAddr)
|
nd.handleClusterEvent(payload, remoteAddr)
|
||||||
|
|||||||
12
main.go
12
main.go
@@ -65,9 +65,15 @@ func main() {
|
|||||||
|
|
||||||
// Initialize MQTT client if enabled
|
// Initialize MQTT client if enabled
|
||||||
var mqttClient *mqtt.MQTTClient
|
var mqttClient *mqtt.MQTTClient
|
||||||
if *mqttServer != "" {
|
// Check for MQTT server from flag or environment variable
|
||||||
log.WithField("server", *mqttServer).Info("Initializing MQTT client")
|
mqttServerURL := *mqttServer
|
||||||
mqttClient = mqtt.NewMQTTClientFromEnv(*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
|
// Set callback to forward MQTT messages to WebSocket
|
||||||
mqttClient.SetMessageCallback(func(topic string, data []byte) {
|
mqttClient.SetMessageCallback(func(topic string, data []byte) {
|
||||||
|
|||||||
Reference in New Issue
Block a user