From d99829b9dca334e25f84ace81504911fb791e0b8 Mon Sep 17 00:00:00 2001 From: Akshay Gadikar Date: Tue, 26 Feb 2019 12:11:37 -0800 Subject: [PATCH 1/3] 1.Added example for Async microgateway 2. Added testcases --- examples/api/async-gateway/main.go | 14 +++++ examples/examples.go | 34 ++++++++++++ examples/examples_test.go | 58 +++++++++++++++++++ examples/json/async-gateway/flogo.json | 77 ++++++++++++++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 examples/api/async-gateway/main.go create mode 100644 examples/json/async-gateway/flogo.json diff --git a/examples/api/async-gateway/main.go b/examples/api/async-gateway/main.go new file mode 100644 index 0000000..bfbb225 --- /dev/null +++ b/examples/api/async-gateway/main.go @@ -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) +} \ No newline at end of file diff --git a/examples/examples.go b/examples/examples.go index 7674436..c6eb5bd 100644 --- a/examples/examples.go +++ b/examples/examples.go @@ -206,6 +206,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) @@ -226,3 +227,36 @@ func DefaultChannelPattern() (engine.Engine, error) { 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 { + panic(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(µgateway.Action{}, settings) + if err != nil { + return nil, err + } + + return api.NewEngine(app) +} \ No newline at end of file diff --git a/examples/examples_test.go b/examples/examples_test.go index 4eb47a8..21a21af 100644 --- a/examples/examples_test.go +++ b/examples/examples_test.go @@ -315,3 +315,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) +} diff --git a/examples/json/async-gateway/flogo.json b/examples/json/async-gateway/flogo.json new file mode 100644 index 0000000..4741ab3 --- /dev/null +++ b/examples/json/async-gateway/flogo.json @@ -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 + } + ] +} From 16ea217e54f8ac0efe1ef2c66f32410ca115a47c Mon Sep 17 00:00:00 2001 From: Akshay Gadikar Date: Tue, 26 Feb 2019 13:22:39 -0800 Subject: [PATCH 2/3] Added Readme --- examples/api/async-gateway/README.md | 16 ++++++++++ examples/json/async-gateway/README.md | 43 +++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 examples/api/async-gateway/README.md create mode 100644 examples/json/async-gateway/README.md diff --git a/examples/api/async-gateway/README.md b/examples/api/async-gateway/README.md new file mode 100644 index 0000000..aa7a2da --- /dev/null +++ b/examples/api/async-gateway/README.md @@ -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" \ No newline at end of file diff --git a/examples/json/async-gateway/README.md b/examples/json/async-gateway/README.md new file mode 100644 index 0000000..d6e2620 --- /dev/null +++ b/examples/json/async-gateway/README.md @@ -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" From ce61f443544800dade01fb904a585bbda69713ae Mon Sep 17 00:00:00 2001 From: Andrew Snodgrass Date: Wed, 27 Feb 2019 12:00:56 -0700 Subject: [PATCH 3/3] Misc fixes --- examples/examples.go | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/examples/examples.go b/examples/examples.go index c6eb5bd..379bcb7 100644 --- a/examples/examples.go +++ b/examples/examples.go @@ -10,6 +10,7 @@ import ( "github.com/project-flogo/core/engine/channels" "github.com/project-flogo/microgateway" microapi "github.com/project-flogo/microgateway/api" + "github.com/project-flogo/microgateway/internal/pattern" ) // BasicGatewayExample returns a Basic Gateway API example @@ -228,6 +229,33 @@ func DefaultChannelPattern() (engine.Engine, error) { return api.NewEngine(app) } +// CustomPattern returns an engine configured for given pattern name +func CustomPattern(patternName string, custompattern string) (engine.Engine, error) { + err := pattern.Register(patternName, custompattern) + if err != nil { + panic(err) + } + app := api.NewApp() + + trg := app.NewTrigger(&trigger.Trigger{}, &trigger.Settings{Port: 9096}) + handler, err := trg.NewHandler(&trigger.HandlerSettings{ + Method: "GET", + Path: "/endpoint", + }) + if err != nil { + panic(err) + } + + _, err = handler.NewAction(µgateway.Action{}, map[string]interface{}{ + "pattern": patternName, + }) + if err != nil { + panic(err) + } + + return api.NewEngine(app) +} + func AsyncGatewayExample() (engine.Engine, error) { app := api.NewApp() gateway := microapi.New("Log") @@ -241,7 +269,7 @@ func AsyncGatewayExample() (engine.Engine, error) { settings, err := gateway.AddResource(app) settings["async"] = true if err != nil { - panic(err) + return nil, err } trg := app.NewTrigger(&trigger.Trigger{}, &trigger.Settings{Port: 9096}) @@ -259,4 +287,4 @@ func AsyncGatewayExample() (engine.Engine, error) { } return api.NewEngine(app) -} \ No newline at end of file +}