Skip to content

Commit

Permalink
Merge pull request #26 from project-flogo/#23-DefaultHttpPattern
Browse files Browse the repository at this point in the history
#23 default http pattern
  • Loading branch information
pointlander committed Feb 12, 2019
2 parents 61ae624 + 285a086 commit cbc1bb7
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 50 deletions.
5 changes: 5 additions & 0 deletions action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,12 @@ func TestMicrogatewayHttpPattern(t *testing.T) {
"useJWT": false,
"useCircuitBreaker": false,
"backendUrl": "http://localhost:1234/",
"method": "GET",
"rateLimit": "3-M",
"mode": "a",
"threshold": 5,
"timeout": 60,
"period": 60,
})
assert.Nil(t, err)
assert.NotNil(t, action)
Expand Down
26 changes: 26 additions & 0 deletions examples/api/default-http-pattern/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
# Gateway using Default Http Pattern
This recipe is a gateway using the defult http pattern which uses JWT, Rate Limiter, and Circuit Breaker.

#Circuit Breaker Service:
| Name | Type | Description |
|:-----------|:--------|:--------------|
| mode | string | The tripping mode: 'a' for contiguous errors, 'b' for errors within a time period, 'c' for contiguous errors within a time period, and 'd' for a probabilistic smart circuit breaker mode. Defaults to mode 'a' |
| threshold | number | The number of errors required for tripping|
| period | number | Number of seconds in which errors have to occur for the circuit breaker to trip. Applies to modes 'b' and 'c'|
| timeout | number | Number of seconds that the circuit breaker will remain tripped. Applies to modes 'a', 'b', 'c'|


#Rate Limiter
| Name | Type | Description |
|:-----------|:--------|:--------------|
| limit | string | Limit can be specifed in the format of "limit-period". Valid periods are 'S', 'M' & 'H' to represent Second, Minute & Hour. Example: "10-S" represents 10 request/second |


#JWT
| Name | Type | Description |
|:-----------|:--------|:--------------|
| token | string | The raw token |
| key | string | The key used to sign the token |
| signingMethod | string | The signing method used (HMAC, ECDSA, RSA, RSAPSS) |
| issuer | string | The 'iss' standard claim to match against |
| subject | string | The 'sub' standard claim to match against |
| audience | string | The 'aud' standard claim to match against |


## Installation
* Install [Go](https://golang.org/)

Expand Down
40 changes: 4 additions & 36 deletions examples/api/default-http-pattern/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ import (
"io/ioutil"
"net/http"

trigger "github.com/project-flogo/contrib/trigger/rest"
"github.com/project-flogo/core/api"
"github.com/project-flogo/core/engine"
"github.com/project-flogo/microgateway"

_ "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/activity/jwt"
_ "github.com/project-flogo/microgateway/activity/ratelimiter"
"github.com/project-flogo/microgateway/examples"
)

var (
Expand All @@ -24,8 +21,7 @@ var (

const reply = `{
"id": 1,
"category": {
"id": 0,
"category": { "id": 0,
"name": "string"
},
"name": "sally",
Expand Down Expand Up @@ -61,35 +57,7 @@ func main() {
return
}

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(&microgateway.Action{}, map[string]interface{}{
"pattern": "DefaultHttpPattern",
"useRateLimiter": true,
"rateLimit": "1-S",
"useJWT": true,
"jwtSigningMethod": "HMAC",
"jwtKey": "qwertyuiopasdfghjklzxcvbnm789101",
"jwtAud": "www.mashling.io",
"jwtIss": "Mashling",
"jwtSub": "tempuser@mail.com",
"useCircuitBreaker": true,
"backendUrl": "http://localhost:1234/pets",
})
if err != nil {
panic(err)
}

e, err := api.NewEngine(app)
e, err := examples.DefaultHTTPPattern()
if err != nil {
panic(err)
}
Expand Down
39 changes: 39 additions & 0 deletions examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,42 @@ func HandlerRoutingExample() (engine.Engine, error) {

return api.NewEngine(app)
}

// DefaultHTTPPattern returns an engine configured for the DefaultHttpPattern
func DefaultHTTPPattern() (engine.Engine, error) {
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(&microgateway.Action{}, map[string]interface{}{
"pattern": "DefaultHttpPattern",
"useRateLimiter": true,
"rateLimit": "1-S",
"useJWT": true,
"jwtSigningMethod": "HMAC",
"jwtKey": "qwertyuiopasdfghjklzxcvbnm789101",
"jwtAud": "www.mashling.io",
"jwtIss": "Mashling",
"jwtSub": "tempuser@mail.com",
"useCircuitBreaker": true,
"backendUrl": "http://localhost:1234/pets",
"mode": "a",
"threshold": 5,
"timeout": 60,
"period": 60,
"method": "GET",
"content": "",
})
if err != nil {
panic(err)
}

return api.NewEngine(app)
}
111 changes: 111 additions & 0 deletions examples/examples_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package examples

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"testing"
"time"

_ "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/activity/jwt"
_ "github.com/project-flogo/microgateway/activity/ratelimiter"
"github.com/project-flogo/microgateway/api"
test "github.com/project-flogo/microgateway/internal/testing"
"github.com/stretchr/testify/assert"
Expand All @@ -19,6 +25,40 @@ type Response struct {
Error string `json:"error"`
}

type handler struct {
Slow bool
}

func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
_, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}

if h.Slow {
time.Sleep(10 * time.Second)
}

w.Header().Set("Content-Type", "application/json")
_, err = w.Write([]byte(reply))
if err != nil {
panic(err)
}
}

const reply = `{
"id": 1,
"category": {
"id": 0,
"name": "string"
},
"name": "sally",
"photoUrls": ["string"],
"tags": [{ "id": 0,"name": "string" }],
"status":"available"
}`

func testBasicGatewayApplication(t *testing.T, e engine.Engine) {
defer api.ClearResources()
test.Drain("9096")
Expand Down Expand Up @@ -145,3 +185,74 @@ func TestHandlerRoutingIntegrationJSON(t *testing.T) {
assert.Nil(t, err)
testHandlerRoutingApplication(t, e)
}

func testDefaultHTTPPattern(t *testing.T, e engine.Engine) {
defer api.ClearResources()
test.Drain("1234")
testHandler := handler{}
s := &http.Server{
Addr: ":1234",
Handler: &testHandler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
go func() {
s.ListenAndServe()
}()
test.Pour("1234")
defer s.Shutdown(context.Background())

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/pets/1", 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()
assert.NotEqual(t, 0, len(body))
}

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

e, err := DefaultHTTPPattern()
assert.Nil(t, err)
testDefaultHTTPPattern(t, e)
}

func TestDefaultHttpPatternJSON(t *testing.T) {
if testing.Short() {
t.Skip("skipping Basic Gateway JSON integration test in short mode")
}
data, err := ioutil.ReadFile(filepath.FromSlash("./json/default-http-pattern/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)
testDefaultHTTPPattern(t, e)
}
27 changes: 27 additions & 0 deletions examples/json/default-http-pattern/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
# Gateway using Default Http Pattern
This recipe is a gateway using the defult http pattern which uses JWT, Rate Limiter, and Circuit Breaker.

#Circuit Breaker Service:
| Name | Type | Description |
|:-----------|:--------|:--------------|
| mode | string | The tripping mode: 'a' for contiguous errors, 'b' for errors within a time period, 'c' for contiguous errors within a time period, and 'd' for a probabilistic smart circuit breaker mode. Defaults to mode 'a' |
| threshold | number | The number of errors required for tripping|
| period | number | Number of seconds in which errors have to occur for the circuit breaker to trip. Applies to modes 'b' and 'c'|
| timeout | number | Number of seconds that the circuit breaker will remain tripped. Applies to modes 'a', 'b', 'c'|


#Rate Limiter
| Name | Type | Description |
|:-----------|:--------|:--------------|
| limit | string | Limit can be specifed in the format of "limit-period". Valid periods are 'S', 'M' & 'H' to represent Second, Minute & Hour. Example: "10-S" represents 10 request/second |


#JWT
| Name | Type | Description |
|:-----------|:--------|:--------------|
| token | string | The raw token |
| key | string | The key used to sign the token |
| signingMethod | string | The signing method used (HMAC, ECDSA, RSA, RSAPSS) |
| issuer | string | The 'iss' standard claim to match against |
| subject | string | The 'sub' standard claim to match against |
| audience | string | The 'aud' standard claim to match against |



## Installation
* Install [Go](https://golang.org/)
* Install the flogo [cli](https://github.com/project-flogo/cli)
Expand Down
8 changes: 7 additions & 1 deletion examples/json/default-http-pattern/flogo.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@
"jwtIss": "Mashling",
"jwtSub": "tempuser@mail.com",
"useCircuitBreaker": true,
"backendUrl": "http://localhost:1234/pets"
"backendUrl": "http://localhost:1234/pets",
"mode": "a",
"threshold": 2,
"timeout": 60,
"period": 60,
"method": "POST",
"content": "{\"name\":\"sally\"}"
}
}
]
Expand Down
Loading

0 comments on commit cbc1bb7

Please sign in to comment.