checkpoint-service

General purpose golang API to create a checkpoint service on Redis. Keys are saved under the prefix tables with a unique value call Time e.g

{
    "Time": "20220123"
}

This project contains source code and supporting files for a containerized golang application. It includes the following files and folders.

  • src (Directory) - Go source code package.
    • checkpoint.go: Defines a Struct for the checkpoint service.
    • redisutil: Set/Gets values from Redis
  • docker-compose.yml - docker-compose file to be used in Production.
  • Dockerfile - Docker image manifest.
  • main.go - Application main runner, creates a webserver for REST API.
  • Makefile - Unix utility that is designed to start an execution.

Requirements

  • Docker:
    • This application has been packaged and requires Docker to be run. All require dependencies e.g (Go Redis, Marshal, gin-gonic, etc) have been already configured in the main Docker image, this guaranties a very easy to manage and portable code across the Windows/Linux ecosystems.
  • Git CLI
    • This project is using Unix Makefile, to test locally using Windows the Git Bash (terminal emulator) is required, also set the environmental variables binary for git on top of the system environmental variables, this will allow command shell to use the Unix instead of Windows.

Run application

This repo contains a Makefile with different target formulas to build and run (locally/production). To run the application:

$ make bootstrap

A directory (redis-data) will show up if this is the first time you run the project, this will use a mouting volume storing the Redis data inside the project, in case services are down the data will be recovered automatically if you run the application again.

To check the different target formulas run:

$ make

Add Checkpoints

The based URL is:

http://localhost:1111/tables/<your_table_name>

POST: Example

The following example will set up a checkpoint for table: sampleTableA

curl -X POST -H 'Content-Type: application/json' --data '{"Time":"20220123"}' http://localhost:1111/tables/sampleTableA

Response:

{
    "message": "Value Set"
}

GET Example

The following example will get the checkpoint previously set.

curl -X GET http://localhost:1111/tables/sampleTableA

Response

{
    "Time": "20220123"
}

GET All Keys: Example

The following example will return all the keys.

curl -X GET http://localhost:1111/tables

Python Example

To test API you can create another container inside the same network and test your code

$ docker container run --rm -it \
    --name python-runer \
    --network checkpoint-service-network \
    python:alpine3.14 /bin/ash

# Inside of the container install
apk -U add vim
pip install requests

# 1.- GET request
# Create a new file
vim main.py

import requests

if __name__ == "__main__":
    URL = "http://api-checkpoint:1111/tables/sampleTableA"
    PARAMS = {}
    r = requests.get(url = URL, params = PARAMS)
    data = r.json()
    print(data)

python main.py

# 2.- POST request
vim main2.py

import requests
import json

if __name__ == "__main__":
    url = "http://api-checkpoint:1111/tables/sampleTableA"

    payload = json.dumps({
    "Time": "20220113"
    })
    headers = {
    'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)

    print(response.text)

python main2.py

Recovering Data in case of outage

In case there is an issue with the services the volume is mounted in this repository under redis-data/dump.rdb, once all the services are stop using:

make clean

Add the changes under dump.rdb to export the checkpoints saved to this repo. Once you restart all services the redis data store should be intact.

How to contribute to the project

There are couple off rules in order to contribute to this repo.

  • Git

    • DO NOT merge to the main master branch.
    • Use the following to create Pull Requests (fix/<your_fix>, feat/<your_feature>, etc.)
    • Every Pull requests will require a review.
  • Go Standards

    • The Go Programming Language Specification Go Code style.
      • functions should be lower camel case.
      • Use typing such as str, int, list, etc.
      • Functions should be comment using the standards.
      • Add test to your code.
      • Follow the directory standard.
      • ETC

Author

  • Luis Enrique Fuentes Plata - 2022-01-11