go-mux-api
Learning exercise using Go, Mux, Postgres and Docker to create a basic CRUD API.
Resources
DB Setup
Install go migrate (Go 1.16+):
go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
Setup initial migration (don't need to run this again, but can use as reference for future migrations):
migrate create -ext sql -dir db/migrations -seq create_items_table
Run the migrations:
export POSTGRESQL_URL="postgres://risky:rainy2@localhost:5432/ror2?sslmode=disable"
migrate -database ${POSTGRESQL_URL} -path db/migrations up
Tidbits
If you need to reset the database completely and reinitialize (eg. with a different user/pass or db name) don't forget to remove the volume:
docker container prune
docker network prune
docker volume prune
docker volume rm -f go-mux-api_data
Run Locally
docker compose up --build
Verification
Test connecting to DB by exec'ing into the container while it's running:
docker exec -it postgres psql -d ror2 -U risky
Test the API with cURL:
curl http://localhost:8010/items
GraphQL
This API also supports graphql queries like so:
curl -X POST -H "Content-Type: application/json" \
-d '{"query": "{list{id,name,description,quality}}" }' \
http://localhost:8010/graphql/item