# README
Go API Assessment
This is the official repository of my Go API Assessment.
Tech stack:
- Go with version
1.20
- Backend Language. - MySQL - Database Layer.
- Migrate - Database Migration tool.
- sqlc - Database Query/Migration generation tool.
- Docker - Container tools.
- AWS Lightsail - Remote environment.
Pre-requisites:
-
docker
service is running. -
sqlc is installed. We need
sqlc
to generate our SQL related queries to a valid Go types.
NOTE: If you are using
go
to installsqlc
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.
- 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:
Testing the API:
A Postman Collection has been provided that can be found under docs
folder.