Wednesday, September 4, 2019

The Flogo project. A simple REST example

The Tibco community has developed an ultralight Open Source Framework to develop Go microservices or, as they say, "event driven applications". Actually, the community edition, includes triggers which listen the following events: prompt commands (CLI triggers), timers, load testers, channels and HTTP messages.


Figure 1. The Flogo project.
Image from https://tibcosoftware.github.io/flogo/introduction/

In this tutorial, we’ll illustrate how to use Flogo to make POST requests sending JSON content which will trigger two request (actions) to other REST services.


Setting up the example

Requirements:  Docker (https://docs.docker.com)

First, let's go over a simple example. We have a blog service and will use Flogo to develop a POST method for the "posts" resource in order to add comments:

 POST  /posts/{postId}/addComment
{
  "body": "Lorem ipsum dolor sit amet, consectetur ...",
  "author": "alnura"
}

Our method will check that the post exists and, in that case, it will add the comment. Suppose that we already have a some resources which we will use in his example

 GET  /posts/{postID}

 POST  /comments
{
 "date": "2019-09-29T18:46:19Z",
 "body": "Lorem ipsum dolor sit amet, consectetur ...",
 "author": "alnura",
 "postId": 1
}

I will use json-server to implement this services using the following db.json:

{ "posts": [    { "id": 1, "body": "foo" },    { "id": 2, "body": "bar" }  ], "comments": [    { "id": 1,       "body": "baz",       "author": "alnura",       "postId": 1      }  ]}

Fetch the later json-server docker image and launch the server:

$ docker run -d -p 80:80 -v <my_path>/db.json:/data/db.json clue/json-server

You can build Flogo applications using Web UI or typing your flow definition json file and using the Flogo cli tool to build your executable file. In this post we will use Web UI so let's start by launching it:

$ docker run -it -p 3303:3303 flogo/flogo-docker:latest eula-accept

The WebUI connects to GitHub to build applications. Disable the SSL certificate checking to avoid connection problems:

$ docker exec <container ID> git config --global http.sslVerify false


Open your favorite web browser, and navigate to http://localhost:3303

Figure 2. Web UI welcome screen.


Create a new application

Click on the New button, set the name (my-blog).


Figure 3. Empty application.


Add a new action called posts.

Figure 4. Adding a new action.


Add a trigger

Click on the "Post" action and add a new trigger by clicking on the (+) icon on the left hand side of the screen and selecting  "Receive an HTTP message".

Now it is time to configure the trigger. To start, click on the trigger and a new dialog will open with a bunch of options. In this dialog you’ll have to provide:

  • Port: 8081
  • Method: POST
  • Path: /posts/:postid/addComment

Save and close.

Note:
Each new action requires a different ports. Choose the "triggers view" if you want to add new methods to the "posts" resource.

Set the flow input and map trigger input to flow input

Click on the input/output icon and define input params.

Figure 5. Setting input/output data.



Save, close and click on the the trigger. Select the "Map to flow's input" tab and provide the following data:

  • author:   $.content.author
  • body:     $.content.body
  • post_id:  $.pathParams.postid


Call "posts" REST service (GET request)

Let's check that the "postid" exist before adding a comment. It's a good practice to include log actions in your flows. I will include some in order to make this one clearer. Add a new "Log" action to the flow. Click on the "..." to activate/deactivate options and click in the  wheel to edit the action. Type the following message in the "Map input" tab:

message:   string.concat("Adding a comment to the post ", $flow.post_id

Add a REST invoke action.

Figura 6. Adding a new REST invoke action.


Edit the REST invoke action and insert the following data:

Settings tab

  • method: GET
  • uri: "http://localhost/posts/:id"

Map input tab

  • pathParams:  {"id":"=$flow.post_id"

Call "comments" REST service (POST request)

Add a new REST invoke action next to the previous one.
Settings tab
method: POST
uri: "http://localhost/posts/:id"
Map input tab
content: {  "body": "=$flow.body",  "author": "=$flow.author",  "postId": "=$flow.post_id"}
Our application just return the http status, however you can customize the response by adding a Return activity and map inputs to outputs.

Build and run the application

Flogo command CLI allows you to build the application from the command line however I will show you how to build the application using the Web UI. Let's start a linux container (Ubuntu), where we will run the application.

$ docker run -v <your_home>/Downloads:/tmp --name ubuntu --network=my-net  -p:8081:8081 --rm -i -t ubuntu bash

Note that I used the Downloads folder as a mount point. WebUI download the executable generated to the user's downloads folder so change to the /tmp directory

$ root@ba300b47a479:/tmp#

Edit you application on the WebUI, click on the build button and select the Linux/arm64 target.

Figure 7. Building your application.


Once your application is downloaded, grant it execution permissions:

$ root@ba300b47a479:/tmp# chmod o+rx my-blog_linux_arm64

And run the application.

$ root@ba300b47a479:/tmp# ./my-blog_linux_arm64


2019-09-03T17:09:08.193Z INFO [flogo.engine] - Starting app [ app-build ] with version [ 0.0.1 ]
2019-09-03T17:09:08.203Z INFO [flogo.engine] - Engine Starting...
2019-09-03T17:09:08.204Z INFO [flogo.engine] - Starting Services...
2019-09-03T17:09:08.211Z INFO [flogo] - ActionRunner Service: Started
2019-09-03T17:09:08.212Z INFO [flogo.engine] - Started Services
2019-09-03T17:09:08.212Z INFO [flogo.engine] - Starting Application...
2019-09-03T17:09:08.213Z INFO [flogo] - Starting Triggers...
2019-09-03T17:09:08.223Z INFO [flogo] - Trigger [ receive_http_message ]: Started
2019-09-03T17:09:08.224Z INFO [flogo] - Triggers Started
2019-09-03T17:09:08.225Z INFO [flogo.engine] - Application Started
2019-09-03T17:09:08.226Z INFO [flogo] - Listening on http://0.0.0.0:8081
2019-09-03T17:09:08.229Z INFO [flogo.engine] - Engine Started

Let's test the application

Add a new comment to the post with id 1 using our application:


$ curl -v --header "Content-Type: application/json" -d '{"body": "Great post.","author": "alnura"}' http://localhost:8081/posts/1/addComment

*   Trying ::1...

* TCP_NODELAY set
* Connected to localhost (::1) port 8081 (#0)
> POST /posts/1/addComment HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 42
>
* upload completely sent off: 42 out of 42 bytes
< HTTP/1.1 200 OK
< Access-Control-Allow-Origin: *
< Date: Tue, 03 Sep 2019 17:15:53 GMT
< Content-Length: 0
<* Connection #0 to host localhost left intact

Check that the comment was added:

$curl http://localhost/comments?postId=1

[
  {
    "id": 1,
    "body": "some comment",
    "postId": 1
  },
  {
    "author": "alnura",
    "body": "Great post.",
    "postId": "1",
    "id": 2
  }
]



8 comments:

  1. buy twitch followers Mario games are the most popular and the most played games over the internet. Kids and adults simply love them.

    ReplyDelete
  2. https://www.visualaidscentre.com/service/eyes-specialist-delhi/ For anyone who's ever made a backup of a game on another console before, the routine should be fairly familiar to you. The Xbox 360 contains only one real quirk that separates it from backing up games for other systems, though that quirk is an important consideration. So whether you're new to backing up games, or are an experienced veteran, read on to familiarize yourself with the best way to backup Xbox 360 games.

    ReplyDelete
  3. The 22 Best Casinos Near Atlanta | MapYRO
    Find 제천 출장마사지 the BEST Casinos in Atlanta, GA 양주 출장샵 with MapYRO's 남원 출장샵 complete casino 의정부 출장마사지 floor, Best Live Dealer Casinos Near 충청북도 출장샵 Atlanta · Bar & Grill – Best Live

    ReplyDelete
  4. V.T. Furniture - titanium element | TITIAN ART
    (TINY V.T) Furniture. Find the finest quality ion titanium on brassy hair V.T. Furniture titanium sponge products keith titanium at TITIAN ART. Contact us for more gold titanium product titanium scrap price information.

    ReplyDelete
  5. https://dynamichealthstaff.com/nursing-jobs-in-qatar You would have surely heard about, or even probably had already used, Search Engine Optimization (SEO) firms if you own a website or an online, e-commerce business. These SEO companies offer a host of services that help in enhancing and driving more online traffic to your site and that, in turn, would increase your business revenues through higher conversion rates owing to the increasing number of potential customers visiting your site. Discover how to choose a good SEO firm in this article.

    ReplyDelete
  6. https://www.buyyoutubesubscribers.in/2021/10/30/does-youtube-pay-views/ How to use video sharing sites like YouTube to make serious cash online. Video's getting more and more popular online so you don't want to miss out!

    ReplyDelete
  7. https://onohosting.com YouTube's millions of videos receive a collective 2 billion+ views per day. If you're looking to slice a nice piece out for yourself, you need to keep these YouTube SEO tips in mind to make sure that you're doing all that you can to ensure that it's your videos which are being seen by the masses.

    ReplyDelete
  8. https://hostinglelo.in When you are looking for a mobile game download, you are going to find that for every true gem on the market, there will be a sea of titles that quickly end up erased off your device. While you sometimes get lucky and the mistake is only a free mobile game, there are some paid downloads that should have spent more time in the development stages. On this list, you will find the best mobile games that are currently available

    ReplyDelete