An HTTP API for managing decks of cards.
- Running the server
- Running tests
- Storage implementations
- API
If you have Docker and Docker Compose on your machine, you can start the server with the make server.start
command. This command will start the server inside of a Docker container, and the server will be available on localhost:4000
.
You can also:
- Stop the server with
make server.stop
- Inspect its logs with
make server.logs
- Make the server start on a different by specifying the
API_PORT
environment variable indocker-compose.yml
If you don't want to run the server with Docker, you can build it and run it directly:
make build
./build/server
You can also specify a different port for the server to start:
API_PORT=8012 ./build/server
Note: you'll need to have Go 1.21 installed on your machine.
Tests are run with the make test
command.
The persistence of decks is done through the Storage interface. The current implementations available are:
- In-memory: the default one. Keeps all the decks in memory. All the decks are lost if the server is shutdown.
- Redis: a Redis one. Note that this implementation has a few caveats currently, explained in here. To use it, set the
DECK_STORAGE_TYPE
toredis
.
There was no requirement about authentication on the task description, so I decided not to implement it. The API is currently behind no authentication. However, I did register a auth middleware, so if authentication is needed, that would be a good place to put it.
POST /api/v1alpha/decks
shuffled
(optional) - determines if the cards in the deck will be shuffled or not. Default: false.cards
(optional) - comma-separated list of card codes to include in the deck. If this is not specified, a deck with all 52 cards is created.
201 Created
{
"deck_id": "289970dd-32b0-4c88-a4c0-d2b2d1fbc53c",
"shuffled": false,
"remaining": 52
}
400 Bad Request
If the card codes specified in the cards
parameter contains an invalid code, a 400 is returned.
curl -X POST http://localhost:4000/api/v1alpha/decks
curl -X POST http://localhost:4000/api/v1alpha/decks?shuffled=true
curl -X POST http://localhost:4000/api/v1alpha/decks?cards=AH,2C,3D,KS
curl -X POST http://localhost:4000/api/v1alpha/decks?cards=AH,2C,3D,KS&shuffled=true
GET /api/v1alpha/decks/:deck_id
deck_id
(required) - the ID of the deck to open
200 OK
{
"deck_id": "bbf72234-b1a7-4671-aa47-1d75a99476a7",
"shuffled": true,
"remaining": 4,
"cards": [
{
"Value": "KING",
"Suit": "SPADES",
"Code": "KS"
},
{
"Value": "2",
"Suit": "CLUBS",
"Code": "2C"
},
{
"Value": "ACE",
"Suit": "HEARTS",
"Code": "AH"
},
{
"Value": "3",
"Suit": "DIAMONDS",
"Code": "3D"
}
]
}
400 Bad Request
If the deck_id
specified is not a valid UUID, 400 is returned.
404 Not Found
If the deck_id
specified does not exist, 404 is returned.
POST /api/v1alpha/decks/:deck_id/draw
deck_id
(required) - the ID of the deck to draw cards from.count
(required) - how many cards to draw from the deck. This must be a positive integer. If this number is bigger than the current number of cards in the deck, all the cards in the deck are returned.
200 OK
{
"cards": [
{
"Value": "KING",
"Suit": "SPADES",
"Code": "KS"
},
{
"Value": "2",
"Suit": "CLUBS",
"Code": "2C"
}
]
}
400 Bad Request
A 400 status code is returned when:
- The
deck_id
specified is not a valid UUID. - The
count
parameter is not specified, or it is not a valid positive integer. - The deck is already empty.
404 Not Found
If the deck_id
specified does not exist, 404 is returned.
curl -X POST http://localhost:4000/api/v1alpha/decks/{deck_id}/draw?count=1