A product reviews service, built with Go, PostgreSQL, and Redis.
- Start the service:
make upbOr
docker-compose upMigrations will be applied automatically.
- Enjoy
The service will be available at http://localhost:8080.
.
├── 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
# 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-
Authentication
- Currently not implemented to focus on core functionality
- Would add JWT-based auth in a production environment
-
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
-
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
-
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
-
Observability
- Currently logging is very basic and does not have any correlation IDs or structured logging for the sake of simplicity
-
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
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:
curl --request POST \
--url http://localhost:8080/v1/products \
--header 'Content-Type: application/json' \
--data '{
"name": "name",
"description": "description",
"priceCents": 123
}'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
}'curl --request GET \
--url http://localhost:8080/v1/products/1/reviews