This is Simple Key value store web service with a subscription feature to pull changes happening to any of the keys. Also has a CLI client which supports the set value and get value along with watch operation to watch changes.
This project can be divided into steps:
- Build the key value store server and write endpoints for GET and SET operations.
- Setup a real-time connection with client to send live changes.
- Build a CLI client that consumes the web service and facilitates the required operations.
Going with spf13/cobra is an easy choice when building a CLI application. I could easily add stuff like persistent flags and such with the help of it.
The code can be found in /keyvaluecli
.
The requirement to run the storage server is Docker. It can be run as follows,
# Bring up the Kafka containers
docker-compose up --build
# Run the server
# --network="host" allows the container to access host's network
# this means that it can connect to Kafka easily as compared to
# setting up a bridge network inside Docker
docker run -it --rm --network="host" mahendrarathore/keyvaluestorages:latest
Once the server is up and running, we can make requests using curl
,
# SET operation
curl localhost:3000/set -d '{"Key": "Name", "Value": "Mahendra"}'
# GET operation
curl localhost:3000/get -d '{"Key": "Name"}'
# GET all key-value pairs
curl localhost:3000/getall
To install the CLI client
go install github.com/mahendrarathore1742/keyvaluestorages/keyvaluecli@latest
Once installed, use the CLI as follows,
# GET all keys
keyvaluecli --server-address localhost:3000 get --all
# GET a given key-value pair
keyvaluecli --server-address localhost:3000 get --key "Name"
# SET a given key-value pair
keyvaluecli --server-address localhost:3000 set --key "Name" --value "Mahendra"
# Watch changes
keyvaluecli --server-address localhost:3000 watch