Intro to Vert.x Shell
Vert.x Shell provides an extensible command line for Vert.x, accessible via SSH, Telnet or a nice Web interface. Vert.x Shell comes out of the box with plenty of commands for Vert.x which makes it very handy for doing simple management operations like deploying a Verticle or getting the list of deployed Verticles. One power feature of Vert.x Shell is its extensibility: one can easily augment Vert.x Shell with its own commands. Let’s build an http-client in JavaScript!
Booting the Shell
Vert.x Shell can be started in a couple of lines depending on the connectors you configure. The documentation provides several examples showing the Shell Service configuration. For testing our command, we will use the Telnet protocol because it is easy to configure and use, so we just need to copy the corresponding section in vertx-http-client.js:
We can run it:
And connect to the shell:
You can now already use the shell, the help command lists the available commands.
Creating a command
For the sake of simplicity we will write a single script that starts the Shell service and deploys our command. In the real world you would probably have the command in one file and the deployment in another.
The documentation explains how to add a new command to Vert.x shell, we can just copy this section and append it to the vertx-http-client.js script:
Now you can use the command just to see it in action:
Checking arguments
The http-client requires an url
argument, an argument check is performed at the beginning of the process handler:
Implementing the command
The final step of this tutorial is the actual implementation of the client logic based on Vert.x HttpClient:
And we can test the command in the shell:
Finally
We have seen how easy it is to extend Vert.x with a shell and create an http-client custom command, you can get the full source code here.
Our command is very simple, it only implements the very minimum, in future posts we will improve the command with support with more HTTP methods, SSL support or header support with the the Vert.x CLI API.