Skip to content

simek-m/customers

Repository files navigation

customers

Running

With Podman Compose

make run-compose

With Docker Compose

Note: Only tested with Podman Compose, but Docker Compose should work the same.

mkdir -p .data
docker compose -f docker-compose.yml up --build

With Docker

Start the database:

make run-db

In another terminal, start the server directly (uses default configuration):

make run-server

Or run it in Docker too:

make run-server-docker

Example requests (CURL)

Save a customer

curl -X POST http://localhost:8080/save/ \
  -H 'Content-Type: application/json' \
  -d '{
    "external_id": "e4b1c2a3-1234-5678-9abc-def012345678",
    "name": "some name",
    "email": "email@email.com",
    "date_of_birth": "2020-01-01T12:12:34+00:00"
  }'

Get a customer

curl http://localhost:8080/e4b1c2a3-1234-5678-9abc-def012345678

Testing

Start the database and server first:

make run-db

Then run the server in another terminal (or in background):

make run-server

Then run the integration tests in yet another terminal:

make integration-test

Environment variables

Variable Description Default
CUSTOMERS_PORT HTTP server port 8080
DB_HOST MariaDB host localhost
DB_PORT MariaDB port 3306
DB_NAME Database name customers
DB_USER Database user customers
DB_PASSWORD Database password customers
API_BASE_URL Base URL for integration tests http://localhost:8080

Note: The configuration has defaults even for the DB password, but it wouldn't be a good practice for production.

Makefile targets

Target Description
make integration-test Run integration tests (requires running server)
make fmt Format code
make lint Run golangci-lint
make gorm-gen Run the GORM CLI generator
make run-db Start MariaDB in Docker
make run-server Run the server locally
make run-server-docker Build and run the server in Docker
make run-compose Build and start everything with Podman Compose
make clean Remove bin/ and .data/

Notes and assumptions:

  • I used GORM for the first time so the code might not be idiomatic (mainly used goqu, an SQL query builder).
  • The ExternalID is used as a primary key and is assumed to be unique. It's not named ID how GORM expects to be consistent in naming.
  • All fields are expected to be mandatory.
  • If there is an /save with an existing UUID, the record is updated.
  • The date_of_birth is stored including time based on the example.
  • There is no cleanup and if volume is used, the test expect empty DB (make clean). That's why the Docker DB doesn't use a volume.
  • The customers.Service is not really necessary as-is but would be if expanded and additional logic would be needed (e.g. notifications, caching, ...).
  • The project structure could be much flatter, but I wanted to mimic what I'm used to on bigger projects.
  • The DB is expected to run before the server. There is no retry, connection pooling or health checks (only in Compose).

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors