Skip to content

Commit e6b2eb1

Browse files
author
Dathan Vance Pattishall
committed
support for stop, start of dev environment. Postgres run's from a container, while the program runs on the host
claude did this
1 parent 41cdb68 commit e6b2eb1

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

Makefile

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# ─────────────────────────────────────────────────────────────────────────────
22
# Go Project Template — Makefile
33
# Usage: make setup (first time — auto-detects name from directory)
4+
# make dev (build + run Go server + Vite, Ctrl-C to stop)
45
# make build / test / run / docker-build
56
# ─────────────────────────────────────────────────────────────────────────────
67

@@ -22,7 +23,7 @@ REPO ?= ghcr.io/$(GIT_OWNER)/$(PROJECT_NAME)
2223
GIT_SHA := $(shell git rev-parse --short HEAD 2>/dev/null || echo "dev")
2324

2425
.DEFAULT_GOAL := all
25-
.PHONY: all lint build build-linux run run-server run-tui test test-unit \
26+
.PHONY: all lint build build-linux run dev dev-stop dev-restart run-server run-tui test test-unit \
2627
test-integration migrate-up migrate-down migrate-create \
2728
frontend-install frontend-build frontend-dev \
2829
docker-build docker-tag docker-push docker-clean \
@@ -90,6 +91,64 @@ build-linux:
9091
# ── Run ───────────────────────────────────────────────────────────────────────
9192
run: run-server
9293

94+
# dev: build the Go server, start it in the background, then start the Vite
95+
# dev server in the foreground. Ctrl-C kills both via the EXIT trap.
96+
# The Go server is ready when it logs "server starting"; we poll /healthz
97+
# so the frontend doesn't proxy to a port that isn't listening yet.
98+
dev-start: build
99+
@echo "==> Starting Postgres, Go server + Vite dev server (Ctrl-C to stop all)"
100+
@docker rm -f dev-postgres 2>/dev/null || true
101+
@docker run -d --name dev-postgres \
102+
-e POSTGRES_HOST_AUTH_METHOD=trust \
103+
-p 5432:5432 \
104+
postgres:latest
105+
@trap 'echo; echo "==> Stopping..."; kill $$(cat /tmp/.dev-server.pid) 2>/dev/null; docker rm -f dev-postgres 2>/dev/null || true; exit 0' INT TERM EXIT; \
106+
echo " Waiting for Postgres on :5432..."; \
107+
for i in $$(seq 1 40); do \
108+
docker exec dev-postgres pg_isready -q 2>/dev/null && break; \
109+
sleep 0.5; \
110+
done; \
111+
docker exec dev-postgres pg_isready -q 2>/dev/null \
112+
|| (echo "ERROR: Postgres did not start"; docker rm -f dev-postgres 2>/dev/null; exit 1); \
113+
./bin/$(SERVER_BINARY) & echo $$! > /tmp/.dev-server.pid; \
114+
echo " Waiting for Go server on :8080..."; \
115+
for i in $$(seq 1 30); do \
116+
curl -sf http://127.0.0.1:8080/healthz >/dev/null 2>&1 && break; \
117+
sleep 0.5; \
118+
done; \
119+
curl -sf http://127.0.0.1:8080/healthz >/dev/null 2>&1 \
120+
|| (echo "ERROR: Go server did not start"; kill $$(cat /tmp/.dev-server.pid) 2>/dev/null; docker rm -f dev-postgres 2>/dev/null; exit 1); \
121+
echo " Go server ready. Starting Vite at http://127.0.0.1:5173 ..."; \
122+
cd frontend && npm run dev
123+
124+
# dev-stop: kill the background Go server and tear down the Postgres container.
125+
# Safe to run even if nothing is running.
126+
dev-stop:
127+
@echo "==> Stopping Go server..."
128+
@kill $$(cat /tmp/.dev-server.pid 2>/dev/null) 2>/dev/null || true
129+
@rm -f /tmp/.dev-server.pid
130+
@pkill -x $(SERVER_BINARY) 2>/dev/null && echo " Server stopped" || echo " Server was not running"
131+
@echo "==> Stopping Postgres..."
132+
@docker rm -f dev-postgres 2>/dev/null && echo " Postgres stopped" || echo " Postgres was not running"
133+
134+
# dev-restart: rebuild the Go server and hot-swap it without touching Postgres
135+
# or the Vite dev server. Run this in a second terminal while `make dev` is
136+
# running in the first.
137+
dev-restart: build
138+
@echo "==> Restarting Go server..."
139+
@kill $$(cat /tmp/.dev-server.pid 2>/dev/null) 2>/dev/null || true
140+
@pkill -x $(SERVER_BINARY) 2>/dev/null || true
141+
@rm -f /tmp/.dev-server.pid
142+
@./bin/$(SERVER_BINARY) & echo $$! > /tmp/.dev-server.pid
143+
@echo " Waiting for Go server on :8080..."; \
144+
for i in $$(seq 1 30); do \
145+
curl -sf http://127.0.0.1:8080/healthz >/dev/null 2>&1 && break; \
146+
sleep 0.5; \
147+
done; \
148+
curl -sf http://127.0.0.1:8080/healthz >/dev/null 2>&1 \
149+
&& echo " Go server ready (pid $$(cat /tmp/.dev-server.pid))" \
150+
|| echo "ERROR: Go server did not start"
151+
93152
run-server: build
94153
./bin/$(SERVER_BINARY)
95154

0 commit comments

Comments
 (0)