
calculate sum | vangav backend
Aug 9, 2017 · 3 min read
This tutorial explains how to generate and use the first vangav backend service; based on: quick start tutorial
Go to https://github.com/vangav/vos_backend for the source code and all the tutorials.
calculate sum is a backend service that takes two floats (a and b) request and returns a double ‘c’ response representing the summation of a and b.
init
- create a workspace directory
my_services- this is the directory to contain both of vos_backend and all the services generated using it - download
vos_backend.zipproject (from the greenclone or download@ https://github.com/vangav/vos_backend) inside the workspace directory created in (1) and unzip it - rename downloaded
vos_backend-mastertovos_backend
generate a new service
- create a new directory
my_services/vos_calculate_sum - copy
controllers.jsonfromvos_backend/vangav_backend_templates/vos_calculate_sum/to the directorymy_services/vos_calculate_sumcreated in (1) - open a terminal session and
cdtomy_services/vos_backend/tools_bin - execute the command
java -jar backend_generator.jar new vos_calculate_sumto 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);
}
start the service
cdtomy_services/vos_calculate_sum- execute the command
./_run.sh
try it out
- open an internet browser page and type
http://localhost:9000/calculate_sum?a=1.2&b=2.3- this returns 3.5 - play with
aandbvalues in the request string in (1) - try issuing an invalid request (e.g.: set
ato "xyz", don't setb, ...) to get a sense of how the default error response looks like (error responses are explained in depth in a separate section)
stop the service
in the terminal session where you started the service press control + d
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, …

