Skip to content

Commit

Permalink
Merge pull request #32 from project-flogo/#30-async_example
Browse files Browse the repository at this point in the history
#30 async example
  • Loading branch information
pointlander committed Feb 27, 2019
2 parents 0c01a81 + ce61f44 commit f90ab84
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 0 deletions.
16 changes: 16 additions & 0 deletions examples/api/async-gateway/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Async Gateway

## Testing

Run:
```bash
go run main.go
```

Run the following command:
```bash
curl --request GET http://localhost:9096/endpoint
```

You should see:
On the server screen, you get 200 response code and log service outputs "Output: Test log message service invoked"
14 changes: 14 additions & 0 deletions examples/api/async-gateway/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"github.com/project-flogo/core/engine"
"github.com/project-flogo/microgateway/examples"
)

func main() {
e, err := examples.AsyncGatewayExample()
if err != nil {
panic(err)
}
engine.RunEngine(e)
}
34 changes: 34 additions & 0 deletions examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func DefaultChannelPattern() (engine.Engine, error) {
step.AddInput("message", "Output: Test log message service invoked")
response := gateway.NewResponse(false)
response.SetCode(200)

settings, err := gateway.AddResource(app)
if err != nil {
panic(err)
Expand Down Expand Up @@ -254,3 +255,36 @@ func CustomPattern(patternName string, custompattern string) (engine.Engine, err

return api.NewEngine(app)
}

func AsyncGatewayExample() (engine.Engine, error) {
app := api.NewApp()
gateway := microapi.New("Log")
service := gateway.NewService("log", &log.Activity{})
service.SetDescription("Invoking test Log service in async gateway")
step := gateway.NewStep(service)
step.AddInput("message", "Output: Test log message service invoked")
response := gateway.NewResponse(false)
response.SetCode(200)
response.SetData("Successful call to action")
settings, err := gateway.AddResource(app)
settings["async"] = true
if err != nil {
return nil, err
}

trg := app.NewTrigger(&trigger.Trigger{}, &trigger.Settings{Port: 9096})
handler, err := trg.NewHandler(&trigger.HandlerSettings{
Method: "GET",
Path: "/endpoint",
})
if err != nil {
return nil, err
}

_, err = handler.NewAction(&microgateway.Action{}, settings)
if err != nil {
return nil, err
}

return api.NewEngine(app)
}
58 changes: 58 additions & 0 deletions examples/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,61 @@ func TestDefaultChannelPatternJSON(t *testing.T) {
assert.Nil(t, err)
testDefaultChannelPattern(t, e)
}

func testAsyncGatewayExample(t *testing.T, e engine.Engine) {
defer api.ClearResources()

test.Drain("9096")
err := e.Start()
assert.Nil(t, err)
defer func() {
err := e.Stop()
assert.Nil(t, err)
}()
test.Pour("9096")

transport := &http.Transport{
MaxIdleConns: 1,
}
defer transport.CloseIdleConnections()
client := &http.Client{
Transport: transport,
}
request := func() string {
req, err := http.NewRequest(http.MethodGet, "http://localhost:9096/endpoint", nil)
assert.Nil(t, err)
response, err := client.Do(req)
assert.Nil(t, err)
body, err := ioutil.ReadAll(response.Body)
assert.Nil(t, err)
response.Body.Close()
return string(body)
}

body := request()
fmt.Println("body is:",body)
assert.NotEqual(t, 0, len(body))
}

func TestAsyncGatewayExampleAPI(t *testing.T) {
if testing.Short() {
t.Skip("skipping Basic Gateway API integration test in short mode")
}

e, err := AsyncGatewayExample()
assert.Nil(t, err)
testAsyncGatewayExample(t, e)
}

func TestAsyncGatewayExampleJSON(t *testing.T) {
if testing.Short() {
t.Skip("skipping Basic Gateway JSON integration test in short mode")
}
data, err := ioutil.ReadFile(filepath.FromSlash("./json/async-gateway/flogo.json"))
assert.Nil(t, err)
cfg, err := engine.LoadAppConfig(string(data), false)
assert.Nil(t, err)
e, err := engine.New(cfg)
assert.Nil(t, err)
testAsyncGatewayExample(t, e)
}
43 changes: 43 additions & 0 deletions examples/json/async-gateway/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Async Gateway
This is an example of Async gateway. This receipe executes simple log action asynchronously


#Log Activity
| Name | Type | Description |
|:-----------|:--------|:--------------|
| message | string | The message to log |
| addDetails | string | If set to true this will append the execution information to the log message |


## Installation
* Install [Go](https://golang.org/)
* Install the flogo [cli](https://github.com/project-flogo/cli)

## Setup
```bash
git clone https://github.com/project-flogo/microgateway
cd microgateway/examples/api/default-http-pattern
```

## Testing
Create the gateway:
```
flogo create -f flogo.json
cd MyProxy
flogo install github.com/project-flogo/contrib/activity/log
flogo build
```

Start the gateway:
```
bin/MyProxy
and test below scenario.
Run the following command:
```bash
curl --request GET http://localhost:9096/endpoint
```

You should see:
On the server screen, you get 200 response code and log service outputs "Output: Test log message service invoked"
77 changes: 77 additions & 0 deletions examples/json/async-gateway/flogo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"name": "MyProxy",
"type": "flogo:app",
"version": "1.0.0",
"description": "This is an example for async microgateway.",
"properties": null,
"channels": null,
"triggers": [
{
"name": "flogo-rest",
"id": "MyProxy",
"ref": "github.com/project-flogo/contrib/trigger/rest",
"settings": {
"port": "9096"
},
"handlers": [
{
"settings": {
"method": "GET",
"path": "/endpoint"
},
"actions": [
{
"id": "microgateway:Log"
}
]
}
]
}
],
"resources": [
{
"id": "microgateway:Log",
"compressed": false,
"data": {
"name": "Log",
"steps": [
{
"service": "LogService",
"input": {
"message": "Output: Test log message service invoked"
}
}
],
"responses": [
{
"error": false,
"output": {
"code": 200,
"data": "Successful call to action"
}
}
],
"services": [
{
"name": "LogService",
"description": "simple Log service",
"ref": "github.com/project-flogo/contrib/activity/log",
"settings": {
}
}
]
}
}
],
"actions": [
{
"ref": "github.com/project-flogo/microgateway",
"settings": {
"uri": "microgateway:Log",
"async": true
},
"id": "microgateway:Log",
"metadata": null
}
]
}

0 comments on commit f90ab84

Please sign in to comment.