Running Javascript as a microservice inside of Jolie
Transcript of 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
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 )()
}
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
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.
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 )()
}
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
Files overview
You should now have the following files:
main.ol
calculator.iol
calculator.js Our Javascript program
Our Jolie program
Shared interface
We are done!
jolie main.ol
You can run the example by launching:
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:
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