This is a simple classified-ads microservice in Go.
- A way to insert a new ad. An ad consists of a subject, a body, an optional price, and an email address.
- A way of fetching existing ads. It should be possible to sort the ads on the time they were inserted and by their price.
- A way of deleting a previously inserted ad.
The service needs Mysql to run successfully. Let's use docker-compose
to run those servers.
docker-compose up -d mysql
And now you can build and run the server:
go run main.go
To run as a service we can create an image and use docker-compose to start the containers. First the image:
docker build . -t service:latest
PS: Docker file uses revive (go lint successor) and gosec tools to validate code written in the solution is following some standard go best practices.
Now we can run all the servers together using docker-compose
.
docker-compose up
POST http://localhost:8000/api/ads
{ "email": "[email protected]", "subject": "random-subject", "price": 100.00, "body": "random-body" }
http://localhost:8000/api/ads?sortBy={created_at, price}&orderBy={asc, desc}
http://localhost:8000/api/ads/{id}
Again with docker-compose
:
docker-compose down
Firstly we need mysql to run the tests
docker-compose up mysql
Run all tests
go test ./...