Beginner tutorial: Developing Web service drivers
In this tutorial you will create a basic Web service driver that reverses strings, learning the basic structure of a driver and how to invoke it from Siren Investigate.
Steps
-
Create a directory for the Web service project by running the following commands:
mkdir reverse cd reverse npm init --yes
-
Add Kibana to the
package.json
file. Ensure that the file contains the following parameters:{ "name": "reverse", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "kibana": { "version": "5.6.10" } }
-
Add the
web-service-interface
package by running the following command:npm install @sirensolutions/web-service-interface
-
Create an
index.js
file with the following contents:import { registerServices, ServiceDefinition } from '@sirensolutions/web-service-interface'; class StringReverser extends ServiceDefinition { (1) name = 'stringreverser'; inputSchema = { (2) message: { type: 'text', required: true // This defaults to false if you do not set it. } }; outputConfiguration = { reversed_message: 'text' }; (3) invoke(inputs) { (4) // Reverse my input const reversed_input = inputs.message.split('').reverse().join(''); return { reversed_message: reversed_input }; } } export default registerServices('reverse', [StringReverser]); (5)
1 Extends the class, so that it complies with the Web service interface. 2 Specifies the inputs that this Web service accepts and whether or not they are mandatory parameters. 3 Defines the structure and types of the output fields, which are used to create the Elasticsearch field mapping. 4 The function that is called when your Web service is invoked. It receives the parameters that are defined in inputSchema
and must return an object that has the same structure as theoutputConfiguration
.5 This export and function call is how you hook the Web service to Siren Investigate. -
It’s ready to go! Copy the
reverse
folder into the Siren Investigateplugins
folder by running the following command:cp -r . /path/to/investigate/plugins/reverse
-
Start Elasticsearch by running the following commands:
cd path/to/elasticsearch bin/elasticseach
-
Go the directory where Siren Investigate has been installed:
cd path/to/investigate
-
Open the
config/investigate.yml
file and add the following snippet to enable Web services if it is not present:web_services: global: enabled: true
-
Start Siren Investigate by running the following command:
bin/investigate
-
Open Siren Investigate in your browser (running on http://localhost:5606 by default), and click on the Web services dashboard.
-
Select
reverse stringreverser
as the active Web service profile in the Query Web services visualization and click Save. -
Enter a string in the message field and click Query; you should see the result of your request in the Invocation Results visualization.