From 0865aba0418e3fd3b353effad6a2c204a5040f88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Luis=20Monta=C3=B1es=20Ojados?= Date: Tue, 27 Jan 2026 02:41:43 +0100 Subject: [PATCH] Add systemd service and Makefile for Linux installation --- Makefile | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ grokway.service | 31 ++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 Makefile create mode 100644 grokway.service diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0160f9c --- /dev/null +++ b/Makefile @@ -0,0 +1,63 @@ +# Makefile for Grokway Server + +BINARY_NAME=grokway +INSTALL_DIR=/opt/grokway +SERVICE_NAME=grokway.service +SYSTEMD_DIR=/etc/systemd/system + +.PHONY: build clean install uninstall help + +help: + @echo "Available commands:" + @echo " make build - Build the server binary" + @echo " make install - Install the server as a systemd service (requires root)" + @echo " make uninstall - Remove the server and service (requires root)" + @echo " make clean - Clean build artifacts" + +build: + @echo "Building $(BINARY_NAME)..." + go build -o $(BINARY_NAME) ./cmd/server + +clean: + @echo "Cleaning..." + rm -f $(BINARY_NAME) + +install: build + @echo "Installing $(BINARY_NAME) to $(INSTALL_DIR)..." + # Create user if not exists + id -u grokway &>/dev/null || useradd -r -s /bin/false grokway + # Create directory + mkdir -p $(INSTALL_DIR) + # Copy binary + cp $(BINARY_NAME) $(INSTALL_DIR)/ + # Copy static assets (maintaining structure needed by code) + # The code expects ./cmd/server/static relative to CWD + mkdir -p $(INSTALL_DIR)/cmd/server + cp -r cmd/server/static $(INSTALL_DIR)/cmd/server/ + # Set permissions + chown -R grokway:grokway $(INSTALL_DIR) + chmod +x $(INSTALL_DIR)/$(BINARY_NAME) + # Install Service + cp $(SERVICE_NAME) $(SYSTEMD_DIR)/ + # Update paths in service file just in case they were modified + sed -i 's|WorkingDirectory=.*|WorkingDirectory=$(INSTALL_DIR)|g' $(SYSTEMD_DIR)/$(SERVICE_NAME) + sed -i 's|ExecStart=.*|ExecStart=$(INSTALL_DIR)/$(BINARY_NAME)|g' $(SYSTEMD_DIR)/$(SERVICE_NAME) + + systemctl daemon-reload + systemctl enable $(SERVICE_NAME) + systemctl start $(SERVICE_NAME) + @echo "Installation complete. Service started." + @echo "Important: Edit $(SYSTEMD_DIR)/$(SERVICE_NAME) to set GROKWAY_TOKEN environment variable if needed." + @echo "Then run: systemctl daemon-reload && systemctl restart $(SERVICE_NAME)" + @echo "Check status directly with: systemctl status $(SERVICE_NAME)" + +uninstall: + @echo "Uninstalling $(BINARY_NAME)..." + systemctl stop $(SERVICE_NAME) || true + systemctl disable $(SERVICE_NAME) || true + rm -f $(SYSTEMD_DIR)/$(SERVICE_NAME) + rm -rf $(INSTALL_DIR) + # Optional: remove user + # userdel grokway || true + systemctl daemon-reload + @echo "Uninstallation complete." diff --git a/grokway.service b/grokway.service new file mode 100644 index 0000000..f8973af --- /dev/null +++ b/grokway.service @@ -0,0 +1,31 @@ +[Unit] +Description=Grokway Server Service +After=network.target + +[Service] +# User/Group to run as. +User=grokway +Group=grokway + +# Working Directory. This is important because the code relies on relative paths +# like ./cmd/server/static. If we install everything to /opt/grokway, this works. +WorkingDirectory=/opt/grokway + +# Path to the executable. +ExecStart=/opt/grokway/grokway + +# Environment variables. +# You MUST set this to a secure token, otherwise a random one is generated on each start. +#Environment=GROKWAY_TOKEN=your-very-secure-token + +# Restart policy. +Restart=always +RestartSec=5 + +# Standard output/error logging. +StandardOutput=syslog +StandardError=syslog +SyslogIdentifier=grokway + +[Install] +WantedBy=multi-user.target