calculate sum | vangav backend

Khufu Djer
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

  1. create a workspace directory my_services - this is the directory to contain both of vos_backend and all the services generated using it
  2. download vos_backend.zip project (from the green clone or download @ https://github.com/vangav/vos_backend) inside the workspace directory created in (1) and unzip it
  3. rename downloaded vos_backend-master to vos_backend

generate a new service

  1. create a new directory my_services/vos_calculate_sum
  2. copy controllers.json from vos_backend/vangav_backend_templates/vos_calculate_sum/ to the directory my_services/vos_calculate_sum created in (1)
  3. open a terminal session and cd to my_services/vos_backend/tools_bin
  4. execute the command java -jar backend_generator.jar new vos_calculate_sum to generate the service
  5. enter y for using the config directory in order to use controllers.json for generating
  6. 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);
}

start the service

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

try it out

  1. open an internet browser page and type http://localhost:9000/calculate_sum?a=1.2&b=2.3 - this returns 3.5
  2. play with a and b values in the request string in (1)
  3. try issuing an invalid request (e.g.: set a to "xyz", don't set b, ...) 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, …


 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