Calculator
Calculator provides an easy to use API to evaluate mathematical expressions created with following operators:
+
-
*
/
(
)
For example: This expression 1 + 2 * (9 / 3 - 1) * 2
would evaluate to 9
API Reference
Evaluate mathematical expression
GET /calculus
Parameter | Type | Description |
---|---|---|
query |
string |
Required. UTF-8 Base64 encoded string |
Tech Stack
Server: JDK-11, Kotlin, Spring-Boot v2.6, Docker, Gradle
Run Locally
Clone the project
git clone https://github.com/ashish-bhatt-go/calculator
Go to the project directory
cd calculator
Build the project - Make sure you have JDK-11 installed and its path set correctly
./gradlew clean build
Start the server(by default it would start server on port 8080)
./gradlew bootRun
Running Tests
Run the following command from the project's root directory
./gradlew clean build test
Demo/Example
You can access and play with the public API deployed on Heroku here. Below is a sample curl request to evaluate an expression using the deployed public API.
curl --location --request GET 'https://calculus-128.herokuapp.com/calculus?query=MiArIDMgKiAoIDUgLSA2ICogOSAvIDMpICogNCAvIDM='
Deployment
Deploy Locally
You can deploy locally using Docker. Please install it first and make sure Docker daemon is running.
Create a local Docker image
docker build -t calculus .
Run the created Docker image
docker run calculus
Deploy to Production
This project uses GitHub actions to build and test the gradle project. After that it builds a docker image and then deploy to Heroku instance. This is completely an automated process though some manual steps were taken to setup project on Heroku.
Manual steps:
- Create an account on Heroku, if you don't have one
- Create a new public Java project
- Create appropriate secrets in GitHub project e.g. Heroku app name, email and API
- Use GitHub action to build, push and deploy the docker image to Heroku with the new project that you created
Automated steps in the CI/CD pipeline:
- Push the code to main branch
- It would run the pipeline as configured with GitHub actions
- If all the actions are successful, you could access your latest changes here: https://calculus-128.herokuapp.com using the API as described in the Demo section