
calculator | vangav backend
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
- create a new directory
my_services/calculator - copy
controllers.jsonfromvos_backend/vangav_backend_templates/vos_calculate_sum/to the directorymy_services/calculatorcreated in (1) - add as many features as desired by editing
my_services/calculator/controllers.json; for example after adding a multiplication feature thecontrollerspart ofmy_services/calculator/controllers.jsonwill 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
}
]
}]
- open a terminal session and
cdtomy_services/vos_backend/tools_bin - execute the command
java -jar backend_generator.jar new calculatorto generate the service - enter
yfor using the config directory in order to usecontrollers.jsonfor generating - enter
nfor 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, methodprocessRequestshould 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.javaunder packagecom.vangav.vos_calculate_sum.controllers.calculate_multiplication, methodprocessRequestshould 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
cdtomy_services/calculator- execute the command
./_run.sh
try it out
- test sum: open an internet browser page and type
http://localhost:9000/calculate_sum?a=1.2&b=2.3- this returns 3.5 - 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
- under the package
com/vangav/vos_calculate_sum/controllers/we will make a copy ofcalculate_sumand call itcalculate_multiplication - inside
calculate_multiplicationwe will do the following modifications - rename
ControllerCalculateSum.javatoControllerCalculateMultiplication.java - rename
getCalculateSumtogetCalculateMultiplication - rename
HandlerCalculateSumandhandlerCalculateSumtoHandlerCalculateMultiplicationandhandlerCalculateMultiplicationrespectively - rename
RequestCalculateSum.javatoRequestCalculateMultiplication.java - alter the
getName returntoreturn "CalculateMultiplication"; - rename
ResponseCalculateSum.javatoResponseCalculateMultiplication.java - alter the
getName returntoreturn "CalculateMultiplication"; - rename
HandlerCalculateSum.javatoHandlerCalculateMultiplication.java - alter
kName = "CalculateSum";tokName = "CalculateMultiplication"; - alter
return new RequestCalculateSum();toreturn new RequestCalculateMultiplication(); - alter
return new ResponseCalculateSum();toreturn new ResponseCalculateMultiplication(); - open copied class
HandlerCalculateMultiplication.javaunder packagecom.vangav.vos_calculate_sum.controllers.calculate_multiplication, methodprocessRequestshould 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);
}
- append the following line to the
routesconf fileGET /calculate_multiplication com.vangav.vos_calculate_sum.controllers.calculate_multiplication.ControllerCalculateMultiplication.getCalculateMultiplication()
start the service
cdtomy_services/calculator- execute the command
./_run.sh
try it out
- test sum: open an internet browser page and type
http://localhost:9000/calculate_sum?a=1.2&b=2.3- this returns 3.5 - 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, …
