build: Add Makefile, update README
This commit is contained in:
parent
97eb04183b
commit
d2f2de8c50
2 changed files with 53 additions and 23 deletions
41
Makefile
Normal file
41
Makefile
Normal file
|
|
@ -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 .
|
||||
35
README.md
35
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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue