Trying out IBM Bluemix with Watson API Beta, I realized there was no example in Go showing the magic. In addition a very old article explaining how to run a Go application with a custom buildpack that was not (entirely) working.
So with this article, I would like to explain in few steps, how to run a simple Go example on Bluemix, using Watson API Beta.
First of all we need to setup the following:
– IBM Bluemix account
– Go environment
Once everything has been correctly setup we are ready to start building the project.
1) Create a new application with the following command:
> cf push [$NEW_APP_NAME] -b https://github.com/cloudfoundry/go-buildpack.git
2) Through the website dashboard, create a new git repository (hosted on hub.jazz.net – point 1 and 2 are deeply explained at this link) and add a Watson API Question and Answer service to your application.
3) Checkout the entire repository into your Go environment.
4) Time to get our hands dirty! First of all I would like to mention that for this example we are going to use Martini a powerful package for quickly writing modular web applications/services in Golang.
5) Let’s build our folder structure:
Public: This folder is going to keep all the public files (css, js, images, etc)
Templates: It will contains all the .tmpl files used to create views.
.godir file: a direct link to our hub.jazz.net repository
manifest.yml file: this file is really important because it’s used during the building stage and contains required information for our application
Procfile: it is a mechanism for declaring what commands are run
server.go : our main code will work run there.
In this example I will trait mainly on the server.go file because it’s the core.
1) As required for a Go file we are going to import all the dependencies , declare the structs useful for json parsing and all the variables with global scope.
2) the main function is where actually our program will get its life. First of all we are using the os space to get the environment variables, and for this example and with Bluemix, we are using the Watson QA (question and answer) Service. So if everything setup correctly we should receive a json containing all the information about our services and taking care about the “question and answer” service storing url, username and password creating a base64 authentication parameter.
3) After we setup the service requirements we can start our application using the simple Martini syntax using the Render and Recovery middlewares, setting up the routes (GET and POST) and starting listening for the incoming requests using the Run method provided by Martini (it will automatically look for the PORT variable, 3000 instead).
4) GET route will handle everything providing the exact view while the POST route will get the question posed by the user and create a request to the Watson environment with the required syntax and parameters.
5) Expected POST parameters will be questionText and dataset (first is really explicit while dataset is used to choose between healthcare and travel API services. A JSON object is built using the parameters and will be set up as body for the outgoing request. A request is created using the url provided by the service environment and an encrypted basic authentication created before and a question json body request.
When the request has been executed we wait for a success or failure. As success we are going to expect a JSON response (all the details are states here)
Click here for a complete DEMO
The full code is hosted on GitHub