Skip to content

Repository files navigation

Product service

A product reviews service, built with Go, PostgreSQL, and Redis.

Quick start

  1. Start the service:
make upb

Or

docker-compose up

Migrations will be applied automatically.

  1. Enjoy

The service will be available at http://localhost:8080.

Development

Project Structure

.
├── api/          # API handlers 
├── cmd/          # Application entrypoint
├── dbgen/        # Generated type-safe database code (SQLC)
├── domain/       # Domain models and interfaces
├── infra/        # External services implementation
├── integration/  # Integration tests
├── sql/          # SQL schema, migrations and queries
└── tool/         # Development tools

Available commands

# Install dev dependencies
make install-tools

# Run unit tests
make test-unit

# Run integration tests
make test-integration

# Run all tests
make test

# Run linter
make lint

# Generate database code
make sqlc

# Create a new migration
make migrate-create

# Generate V1 API
make generate-v1

Design choices

  1. Authentication

    • Currently not implemented to focus on core functionality
    • Would add JWT-based auth in a production environment
  2. Database

    • Using Atlas for migrations handling, migrations automatically applied in dev environment on docker-compose up
    • Using sqlc for code generation of type-safe interfaces for queries
    • Chose sequential IDs for simplicity
    • Would use UUIDs in a production environment in order to not disclose internal IDs
  3. Oapi-codegen

    • Chose oapi-codegen to generate API client and models. In my experience, it is easier to write spec and generate client from it, then do it the other way around
    • Would expose the specification as an API endpoint
  4. Notifications

    • Implemented Redis Pub/Sub since it is easy to use and since Redis was already used for caching
    • Known problems: no message persistence, messages lost if no subscribers are active.
    • Would replace it with a proper message broker in production
  5. Observability

    • Currently logging is very basic and does not have any correlation IDs or structured logging for the sake of simplicity
  6. Service configuration

    • The configuration is very basic via environment variables directly in the main package
    • Would use something fancy like viper in a production environment

API examples

The easiest way to interact with the API is to import the api/v1/openapi.yaml file into your favorite API client like Insomnia.

But just in case, below are some cURL examples:

Create a product

curl --request POST \
  --url http://localhost:8080/v1/products \
  --header 'Content-Type: application/json' \
  --data '{
  "name": "name",
  "description": "description",
  "priceCents": 123
}'

Create a review

curl --request POST \
  --url http://localhost:8080/v1/products/1/reviews \
  --header 'Content-Type: application/json' \
  --data '{
  "firstName": "Qwe",
  "lastName": "Rty",
  "text": "Meh",
  "rating": 1
}'

List reviews

curl --request GET \
  --url http://localhost:8080/v1/products/1/reviews

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages