From d2f2de8c5075f9ab0946dc5fc23d6a7939782b2d Mon Sep 17 00:00:00 2001 From: Leo Date: Tue, 18 Aug 2026 08:04:20 +0300 Subject: [PATCH] build: Add Makefile, update README --- Makefile | 41 +++++++++++++++++++++++++++++++++++++++++ README.md | 35 ++++++++++++----------------------- 2 files changed, 53 insertions(+), 23 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8c1f3d1 --- /dev/null +++ b/Makefile @@ -0,0 +1,41 @@ +.PHONY: help setup run dev build fmt migrate-up migrate-down migrate-status migrate-create generate docker-build + +.DEFAULT_GOAL := help + +help: ## List available targets + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-16s\033[0m %s\n", $$1, $$2}' + +setup: ## Copy .env, tidy modules, enable git hooks + cp -n .env.example .env || true + go mod tidy + git config core.hooksPath .githooks + +run: ## Run the server + go run ./cmd + +dev: ## Run the server with live reload (air) + air + +build: ## Build the server binary to ./tmp/main + go build -o ./tmp/main ./cmd + +fmt: ## Format all Go files + gofmt -l -w . + +migrate-up: ## Apply all pending migrations + ./scripts/goose.sh up + +migrate-down: ## Roll back the last migration + ./scripts/goose.sh down + +migrate-status: ## Show migration status + ./scripts/goose.sh status + +migrate-create: ## Create a new migration (name=add_widgets) + ./scripts/goose-create.sh $(name) + +generate: ## Run sqlc codegen + sqlc generate + +docker-build: ## Build the Docker image + docker build -t go-backend . diff --git a/README.md b/README.md index fd4cebc..c09781d 100644 --- a/README.md +++ b/README.md @@ -30,52 +30,41 @@ The intention for this project is to work as a template, where the developer sho ## Setup -1. Copy environment variables: +1. Run setup (copies `.env.example` to `.env`, runs `go mod tidy`, enables the + repo's git hooks which reject commits with unformatted Go files): ```bash - cp .env.example .env + make setup ``` 2. Update `.env` with your database credentials (both the `DB_*` and `GOOSE_*` values). -3. Install dependencies: +3. Run database migrations: ```bash - go mod tidy + make migrate-up ``` -4. Run database migrations: - +4. Run the server: ```bash - ./scripts/goose.sh up - ``` - -5. Run the server: - ```bash - air # live reload, or: - go run ./cmd - ``` - -6. Enable the repo's git hooks (rejects commits with unformatted Go files): - - ```bash - git config core.hooksPath .githooks + make dev # live reload, or: + make run ``` ## Migrations -`goose.sh` loads `.env` and passes through to goose (`up`, `down`, `status`, ...). -Create a new migration with: +`make migrate-up` / `make migrate-down` / `make migrate-status` load `.env` and +pass through to goose. Create a new migration with: ```bash -./scripts/goose-create.sh add_widgets +make migrate-create name=add_widgets ``` ## SQLC Query codegen is configured in `sqlc.yaml` (engine `postgresql`, schema read from `migrations/`). Add `.sql` files under `internal/database/queries/` and run -`sqlc generate` to emit typed accessors into `internal/database/sqlc/`. +`make generate` to emit typed accessors into `internal/database/sqlc/`. ## Database Schema