Running Javascript as a microservice inside of Jolie

10
Using Javascript from Jolie Copyright © 2014 The Jolie Team. This work is licensed under the Creative Commons Attribution License (CC BY). To view a copy of this license, visit https://creativecommons.org/licenses/by/4.0 http://www.jolie-lang.org

Transcript of Running Javascript as a microservice inside of Jolie

Page 1: Running Javascript as a microservice inside of Jolie

Using Javascript from Jolie

Copyright © 2014 The Jolie Team.This work is licensed under the Creative Commons Attribution License (CC BY).To view a copy of this license, visit https://creativecommons.org/licenses/by/4.0

http://www.jolie-lang.org

Page 2: Running Javascript as a microservice inside of Jolie

In this tutorial...

We learn how to run Javascript code in a Jolie service.

We will use the following Jolie program as an example. It asks an external service, Calculator, to calculate x to the power of y.

Our objective is to implement Calculator in Javascript!

main{

request.x = 2;request.y = 3;pow@Calculator( request )( result );println@Console( "The result is: " + result )()

}

Page 3: Running Javascript as a microservice inside of Jolie

Define an interface

We first define an interface between the Jolie and Javascript programs:

type PowRequest:void {.x:double.y:double

}

interface CalculatorIface {RequestResponse:

pow( PowRequest )( double )}

Save this interface in file calculator.iol

Page 4: Running Javascript as a microservice inside of Jolie

Write the Javascript program

We implement operation pow as a Javascript function:

function pow( request ){

return Math.pow( request.x, request.y );}

Save this program in file calculator.js

Above, parameter request is the Jolie data tree we will receive from the Jolie program.

Page 5: Running Javascript as a microservice inside of Jolie

Embed the Javascript program

We use embedding to run the Javascript program inside of Jolie.

Save the following Jolie program in file main.ol

include "calculator.iol"include "console.iol"

outputPort Calculator { Interfaces: CalculatorIface }

embedded {JavaScript: "calculator.js" in Calculator}

main {request.x = 2; request.y = 3;pow@Calculator( request )( result );println@Console( "The result is: " + result )()

}

Page 6: Running Javascript as a microservice inside of Jolie

Embed the Javascript program

Here is what it all means:

include "calculator.iol"include "console.iol"

outputPort Calculator { Interfaces: CalculatorIface }

embedded {JavaScript: "calculator.js" in Calculator}

main {request.x = 2; request.y = 3;pow@Calculator( request )( result );println@Console( "The result is: " + result )()

}

Include the interface

Create an output port with our interface.

Bind the output port to our Javascript program

Page 7: Running Javascript as a microservice inside of Jolie

Files overview

You should now have the following files:

main.ol

calculator.iol

calculator.js Our Javascript program

Our Jolie program

Shared interface

Page 8: Running Javascript as a microservice inside of Jolie

We are done!

jolie main.ol

You can run the example by launching:

Page 9: Running Javascript as a microservice inside of Jolie

Extend it

request.x = double( args[0] );request.y = double( args[1] )

If you like, you can take the numbers as command line arguments in your Jolie program:

jolie main.ol 3.0 4.0

For example, try running:

Page 10: Running Javascript as a microservice inside of Jolie

Copyright © 2014 The Jolie Team.This work is licensed under the Creative Commons Attribution License (CC BY).To view a copy of this license, visit https://creativecommons.org/licenses/by/4.0

http://www.jolie-lang.org

The first language for microservices