-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#29-custompattern
- Loading branch information
Showing
8 changed files
with
265 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Gateway using Custom-Pattern | ||
This recipe is a gateway using the custom pattern. It uses counter activity to keep the track of number of successful | ||
and failed HTTP calls. | ||
|
||
Example: | ||
#Counter Activity | ||
| Name | Type | Description | | ||
|:-----------|:--------|:--------------| | ||
| counterName | string, required:true | The name of the counter | | ||
| op | string | Counter operation, 'get' is the default operation | | ||
|
||
|
||
## Installation | ||
* Install [Go](https://golang.org/) | ||
|
||
## Setup | ||
```bash | ||
git clone https://github.com/project-flogo/microgateway | ||
cd microgateway/examples/api/custom-pattern | ||
``` | ||
|
||
## Testing | ||
Start the gateway: | ||
```bash | ||
go run main.go | ||
``` | ||
In another terminal start the server: | ||
```bash | ||
go run main.go -server | ||
|
||
### Request is successful | ||
Run the following command: | ||
```bash | ||
curl --request GET http://localhost:9096/endpoint | ||
``` | ||
|
||
You should see on successful call: | ||
```json | ||
{"Error-Calls":0,"Success-Calls":1} | ||
``` | ||
|
||
Similarly, on unsuccessful call...or in case of error: | ||
You should see on successful call: | ||
```json | ||
{"Error-Calls":1,"Success-Calls":0} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"html" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
_ "github.com/project-flogo/contrib/activity/counter" | ||
_ "github.com/project-flogo/contrib/activity/rest" | ||
"github.com/project-flogo/core/engine" | ||
_ "github.com/project-flogo/microgateway/activity/circuitbreaker" | ||
"github.com/project-flogo/microgateway/examples" | ||
) | ||
|
||
var ( | ||
server = flag.Bool("server", false, "run the test server") | ||
) | ||
|
||
const reply = `{ | ||
"name": "sally" | ||
}` | ||
|
||
const pattern = `{ | ||
"name": "CustomPattern", | ||
"steps": [ | ||
{ | ||
"service": "HttpBackend", | ||
"halt": "($.HttpBackend.error != nil) && !error.isneterror($.HttpBackend.error)" | ||
}, | ||
{ | ||
"if": "$.HttpBackend.error == nil", | ||
"service": "SuccessCounter" | ||
}, | ||
{ | ||
"if": "$.HttpBackend.error != nil", | ||
"service": "ErrorCounter" | ||
}, | ||
{ | ||
"service": "GetCounterSuccess" | ||
}, | ||
{ | ||
"service": "GetCounterError" | ||
} | ||
], | ||
"responses": [ | ||
{ | ||
"if" : "$.GetCounterSuccess.error == nil", | ||
"error": false, | ||
"output": { | ||
"code": 200, | ||
"data": { | ||
"Success-Calls": "=$.GetCounterSuccess.outputs.value", | ||
"Error-Calls": "=$.GetCounterError.outputs.value" | ||
} | ||
} | ||
}, | ||
{ | ||
"error": true, | ||
"output": { | ||
"code": 400, | ||
"data": "Error" | ||
} | ||
} | ||
], | ||
"services": [ | ||
{ | ||
"name": "HttpBackend", | ||
"description": "Make an http call to your backend", | ||
"ref": "github.com/project-flogo/contrib/activity/rest", | ||
"settings": { | ||
"method": "GET", | ||
"uri": "http://localhost:1234/pets" | ||
} | ||
}, | ||
{ | ||
"name": "SuccessCounter", | ||
"description": "Increment counter on successful call", | ||
"ref": "github.com/project-flogo/contrib/activity/counter", | ||
"settings": { | ||
"counterName": "SuccessCounter", | ||
"op": "increment" | ||
} | ||
}, | ||
{ | ||
"name": "ErrorCounter", | ||
"description": "Increment counter on error call", | ||
"ref": "github.com/project-flogo/contrib/activity/counter", | ||
"settings": { | ||
"counterName": "ErrorCounter", | ||
"op": "increment" | ||
} | ||
}, | ||
{ | ||
"name": "GetCounterSuccess", | ||
"description": "Get success counter", | ||
"ref": "github.com/project-flogo/contrib/activity/counter", | ||
"settings": { | ||
"counterName": "SuccessCounter" | ||
} | ||
}, | ||
{ | ||
"name": "GetCounterError", | ||
"description": "Get error counter", | ||
"ref": "github.com/project-flogo/contrib/activity/counter", | ||
"settings": { | ||
"counterName": "ErrorCounter" | ||
} | ||
} | ||
] | ||
}` | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if *server { | ||
http.HandleFunc("/pets", func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Printf("url: %q\n", html.EscapeString(r.URL.Path)) | ||
defer r.Body.Close() | ||
body, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(string(body)) | ||
w.Header().Set("Content-Type", "application/json") | ||
_, err = w.Write([]byte(reply)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
}) | ||
|
||
err := http.ListenAndServe(":1234", nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return | ||
} | ||
|
||
e, err := examples.CustomPattern("CustomPattern", pattern) | ||
if err != nil { | ||
panic(err) | ||
} | ||
engine.RunEngine(e) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,4 +152,4 @@ | |
} | ||
} | ||
] | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters