Categorygithub.com/rbo13/go-api-assessment
module
0.0.0-20240420021241-1c3da2e6359f
Repository: https://github.com/rbo13/go-api-assessment.git
Documentation: pkg.go.dev

# README

Go API Assessment

This is the official repository of my Go API Assessment.

Tech stack:

  1. Go with version 1.20 - Backend Language.
  2. MySQL - Database Layer.
  3. Migrate - Database Migration tool.
  4. sqlc - Database Query/Migration generation tool.
  5. Docker - Container tools.
  6. AWS Lightsail - Remote environment.

Pre-requisites:

  1. docker service is running.

  2. sqlc is installed. We need sqlc to generate our SQL related queries to a valid Go types.

NOTE: If you are using go to install sqlc command, the installation instructions below assumes that Go is correctly installed, i.e $GOPATH/bin is in your $PATH:

# using brew/linuxbrew
brew install sqlc

# or using go
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest

# check version
sqlc version

For more installation instructions, please check the official sqlc installation docs.

  1. Golang-Migrate is installed. Needed to run the database migrations.

To install the CLI:

# Go 1.16+
# Since we are using mysql as our database we need to use the mysql tag.
go install -tags 'mysql' github.com/golang-migrate/migrate/v4/cmd/migrate@latest

# check if correctly installed
migrate --version

Note: The installed CLI will be installed on your $GOPATH/bin, so make sure that said path is available on your $PATH.

For more installation instructions, see golang-migrate installation docs.

Cloning the project:

git clone [email protected]:rbo13/go-api-assessment.git

Setup the project:

$ cd go-api-assessment

# generate db related files via `sqlc`
sqlc generate

# secure environment files
cp -a .env.test .env

# tidy project
$ go mod tidy

Run MySQL via docker:

docker run -d \
  --name=mysql_teacher_db \
  -p 3306:3306 \
  -e MYSQL_ROOT_PASSWORD=password \
  -e MYSQL_DATABASE=api_db \
  mysql:latest

Run the database migration:

$ export DATABASE_URL="mysql://root:password@tcp(localhost:3306)/api_db?parseTime=true&loc=Local"
$ migrate -path db/migrations -database ${DATABASE_URL} up

# or if you have `make` you can use the provided make command.
$ make migrate

Run the tests:

go test -v -race ./...

Finally, run the project via go run:

go run ./cmd/api/root.go ./cmd/api/main.go

Check application:


If all is well, a message like this should be seen:

image

Testing the API:

A Postman Collection has been provided that can be found under docs folder.

A screencast of how-to setup the project can be seen here:

asciicast

# Packages

No description provided by the author
No description provided by the author