calculator | vangav backend

Khufu Djer
Aug 9, 2017 · 4 min read

This tutorial explains how expand vangav backend’s calculate sum template to a calculator backend service; based on: expand “calculate sum” to “calculator tutorial

Go to https://github.com/vangav/vos_backend for the source code and all the tutorials.


Expanding a service through regenerating it …

generate a new service

  1. create a new directory my_services/calculator
  2. copy controllers.json from vos_backend/vangav_backend_templates/vos_calculate_sum/ to the directory my_services/calculator created in (1)
  3. add as many features as desired by editing my_services/calculator/controllers.json; for example after adding a multiplication feature the controllers part of my_services/calculator/controllers.json will be as follows
"controllers": [  # CalculateSum
{
"is_preset": false,
"name": "CalculateSum",
...
},
# CalculateMultiplication
{
"is_preset": false,
"name": "CalculateMultiplication",
"type": "GET",
"request_params": [
{
"name": "a",
"type": "FLOAT",
"is_array": false,
"optionality": "MANDATORY"
},
{
"name": "b",
"type": "FLOAT",
"is_array": false,
"optionality": "MANDATORY"
}
],
"response_type": "JSON",
"response_params": [
{
"name": "c",
"type": "double",
"is_array": false
}
]
}
]
  1. open a terminal session and cd to my_services/vos_backend/tools_bin
  2. execute the command java -jar backend_generator.jar new calculator to generate the service
  3. enter y for using the config directory in order to use controllers.json for generating
  4. enter n for generating a worker service (using workers is explained in a separate section)

writing the service’s logic code

  • optionally for eclipse users: open eclipse and import vos_calculate_sum project
  • file > import > general > existing projects into workspace > next > set “select root directory” to my_services > under projects make sure that vos_calculate_sum is selected > finish
  • double check the java version used for compiling the project: right click the project > properties > java compiler >enable project specific settings > compiler compliance level > 1.7 or 1.8
  • open class HandlerCalculateSum.java under package com.vangav.vos_calculate_sum.controllers.calculate_sum, method processRequest should be as follows in order to complete the request-to-response logic
@Override
protected void processRequest (final Request request) throws Exception {
// use the following request Object to process the request and set
// the response to be returned
RequestCalculateSum requestCalculateSum =
(RequestCalculateSum)request.getRequestJsonBody();

// set response's value
((ResponseCalculateSum)request.getResponseBody() ).set(
requestCalculateSum.a + requestCalculateSum.b);
}
  • open class HandlerCalculateMultiplication.java under package com.vangav.vos_calculate_sum.controllers.calculate_multiplication, method processRequest should be as follows in order to complete the request-to-response logic
@Override
protected void processRequest (final Request request) throws Exception {
// use the following request Object to process the request and set
// the response to be returned
RequestCalculateMultiplication requestCalculateMultiplication =
(RequestCalculateMultiplication)request.getRequestJsonBody();

// set response's value
((ResponseCalculateMultiplication)request.getResponseBody() ).set(
requestCalculateMultiplication.a
* requestCalculateMultiplication.b);
}

start the service

  1. cd to my_services/calculator
  2. execute the command ./_run.sh

try it out

  1. test sum: open an internet browser page and type http://localhost:9000/calculate_sum?a=1.2&b=2.3 - this returns 3.5
  2. test multiplication: open an internet browser page and type http://localhost:9000/calculate_multiplication?a=1.2&b=2.3 - this returns 2.76

stop the service

in the terminal session where you started the service press control + d


Expanding a service without regenerating it …

in this section we will get the same result without regenerating the service; given the already generated calculate sum backend service, we will follow the following steps to add a multiplication feature

  1. under the package com/vangav/vos_calculate_sum/controllers/ we will make a copy of calculate_sum and call it calculate_multiplication
  2. inside calculate_multiplication we will do the following modifications
  3. rename ControllerCalculateSum.java to ControllerCalculateMultiplication.java
  4. rename getCalculateSum to getCalculateMultiplication
  5. rename HandlerCalculateSum and handlerCalculateSum to HandlerCalculateMultiplication and handlerCalculateMultiplication respectively
  6. rename RequestCalculateSum.java to RequestCalculateMultiplication.java
  7. alter the getName return to return "CalculateMultiplication";
  8. rename ResponseCalculateSum.java to ResponseCalculateMultiplication.java
  9. alter the getName return to return "CalculateMultiplication";
  10. rename HandlerCalculateSum.java to HandlerCalculateMultiplication.java
  11. alter kName = "CalculateSum"; to kName = "CalculateMultiplication";
  12. alter return new RequestCalculateSum(); to return new RequestCalculateMultiplication();
  13. alter return new ResponseCalculateSum(); to return new ResponseCalculateMultiplication();
  14. open copied class HandlerCalculateMultiplication.java under package com.vangav.vos_calculate_sum.controllers.calculate_multiplication, method processRequest should be as follows in order to complete the request-to-response logic
@Override
protected void processRequest (final Request request) throws Exception {
// use the following request Object to process the request and set
// the response to be returned
RequestCalculateMultiplication requestCalculateMultiplication =
(RequestCalculateMultiplication)request.getRequestJsonBody();

// set response's value
((ResponseCalculateMultiplication)request.getResponseBody() ).set(
requestCalculateMultiplication.a * requestCalculateMultiplication.b);
}
  1. append the following line to the routes conf file GET /calculate_multiplication com.vangav.vos_calculate_sum.controllers.calculate_multiplication.ControllerCalculateMultiplication.getCalculateMultiplication()

start the service

  1. cd to my_services/calculator
  2. execute the command ./_run.sh

try it out

  1. test sum: open an internet browser page and type http://localhost:9000/calculate_sum?a=1.2&b=2.3 - this returns 3.5
  2. test multiplication: open an internet browser page and type http://localhost:9000/calculate_multiplication?a=1.2&b=2.3 - this returns 2.76

stop the service

in the terminal session where you started the service press control + d


exercise

expand this service to a fully fledged calculator service


Go to https://github.com/vangav/vos_backend for the source code and all the tutorials.


Thanks for sharing the knowledge and for inspiring us: Y Combinator, StationF, Techstars, Stanford Business, …

 by the author.

Khufu Djer

Written by

https://github.com/vangav/vos_backend ex-SE @ FB, MS, IBM, GS & Sony

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade