lets-go-further

module
v0.0.0-...-be4c55d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2022 License: MIT

README

lets-go-further

Repository for exercise from the book https://lets-go-further.alexedwards.net

Run the app with command line flags:

go run ./cmd/api -port=3030 -env=production

Create a local Postgres DB, user and extension

create database greenlight;

create role greenlight with login password 'greenlight';

-- adds a case-insensitive character string type to PostgreSQL
create extension if not exists citext;

Add posrtgres dsn to env

 GREENLIGHT_DB_DSN='postgres://greenlight:greenlight@localhost/greenlight?sslmode=disable'

Database Migration tool

Install tool on Mac OS:

brew install golang-migrate

Install tool on Linux:

curl -L https://github.com/golang-migrate/migrate/releases/download/v4.14.1/migrate.linux-amd64.tar.gz | tar xvz
mv migrate.linux-amd64 $GOPATH/bin/migrate

Create migration scripts

migrate create -seq -ext=.sql -dir=./migrations create_movies_table
  • The -seq flag indicates that we want to use sequential numbering like 0001, 0002, ... for the migration files (instead of a Unix timestamp, which is the default).
  • The -ext flag indicates that we want to give the migration files the extension .sql.
  • The -dir flag indicates that we want to store the migration files in the ./migrations directory (which will be created automatically if it doesn’t already exist).
  • The name create_movies_table is a descriptive label that we give the migration files to signify their contents.

Execute migration scripts

migrate -path=./migrations -database=$GREENLIGHT_DB_DSN up

Verify migration version:

migrate -path=./migrations -database=$EXAMPLE_DSN version

Migrate up or down to a specific version:

migrate -path=./migrations -database=$EXAMPLE_DSN goto 1

Run the down migration:

migrate -path=./migrations -database =$EXAMPLE_DSN down 1

Force the DB version to a specific version:

migrate -path=./migrations -database=$EXAMPLE_DSN force 1

Send a bunch concurrent request to update a movie to verify there is not data races:

xargs -I % -P8 curl -X PATCH -d '{"runtime": "97 mins"}' "localhost:4000/v1/movies/4" < <(printf '%s\n' {1..8})

Rate limiting

To test rate limits work:

for i in {1..6}; do curl http://localhost:4000/v1/healthcheck; done

Run migration via makefile

make db/migrations/new name=create_example_table

Install staticcheck for additional veirifications

go install honnef.co/go/tools/cmd/staticcheck@latest

Make sure GOPATH is added to the system PATH (e.g. in the ~/.zshrc:

 export PATH=$PATH:$(go env GOPATH)/bin

Directories

Path Synopsis
cmd
api
internal
vcs

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL