Skip to content

Commit

Permalink
Add sample plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyaw Myint Thein committed Sep 21, 2021
1 parent 84ee3da commit 523d5df
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ module github.com/kyawmyintthein/lura-twirp
go 1.13

require (
github.com/gofrs/uuid v4.0.0+incompatible // indirect
github.com/google/martian v2.1.0+incompatible
github.com/luraproject/lura v1.4.1
github.com/qntfy/jsonparser v1.0.2 // indirect
github.com/qntfy/kazaam v3.4.9+incompatible // indirect
google.golang.org/protobuf v1.27.1
gopkg.in/qntfy/kazaam.v3 v3.4.9
gorm.io/gorm v1.21.15
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/dimfeld/httptreemux v5.0.1+incompatible/go.mod h1:rbUlSV+CCpv/SuqUTP/
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-gonic/gin v1.1.5-0.20170702092826-d459835d2b07/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
github.com/go-chi/chi v4.0.1+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
Expand All @@ -19,6 +21,10 @@ github.com/luraproject/lura v1.4.1 h1:zNzLYMzM13EaSrb9bfaPICD4bXtNqMCdyi42R1eBgr
github.com/luraproject/lura v1.4.1/go.mod h1:KIo1/+nsRZVxIO04Hkbth0GXSSzypvkFpF5KaIoLvlo=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/qntfy/jsonparser v1.0.2 h1:hko+J4L7HSaYoB2yuzinWc9MkO93zWKUmzPHJwB53OM=
github.com/qntfy/jsonparser v1.0.2/go.mod h1:F+LCdwPnFBsubQ+ugnBczIP9RWv5wSCqnUmLHPUx4ZU=
github.com/qntfy/kazaam v3.4.9+incompatible h1:L5M3waKZ7abX4hWKD27HYBPRkydrYtcVZJKw6NKbNLQ=
github.com/qntfy/kazaam v3.4.9+incompatible/go.mod h1:aN8m9eOLEtyeypys9YtGYm0rFjKWlobu18ez6GcBtsg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go v0.0.0-20180112141927-9831f2c3ac10/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
Expand All @@ -38,6 +44,8 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
gopkg.in/qntfy/kazaam.v3 v3.4.9 h1:TdJrHe4YT8XTG5aNLz8ACLvyV0a8piUkRiC6MUrVRDQ=
gopkg.in/qntfy/kazaam.v3 v3.4.9/go.mod h1:Wd/NtL7lla1KLeVKvET8CWojg+DmX0tDUC6J+ut61Z0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gorm.io/gorm v1.21.15 h1:gAyaDoPw0lCyrSFWhBlahbUA1U4P5RViC1uIqoB+1Rk=
gorm.io/gorm v1.21.15/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
19 changes: 19 additions & 0 deletions plugins/querystringtobody/modifer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package querystringtobody

import (
"github.com/google/martian/parse"
"github.com/kyawmyintthein/lura-twirp/plugins/requestbodytransformer/modifier"
)

func init() {
parse.Register("body.FromQueryString", FromJSON)
}

func FromJSON(b []byte) (*parse.Result, error) {
msg, err := modifier.FromJSON(b)
if err != nil {
return nil, err
}

return parse.NewResult(msg, []parse.ModifierType{parse.Request})
}
141 changes: 141 additions & 0 deletions plugins/querystringtobody/modifer/modifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Package modifier exposes a request modifier for generating bodies
// from the querystring params
package modifier

import (
"bytes"
b64 "encoding/base64"
"encoding/json"
"io/ioutil"
"log"
"net/http"

kazaam "gopkg.in/qntfy/kazaam.v3"
)

type Config struct {
URLPattern string `json:"url_pattern"`
Template string `json:"template"`
Method string `json:"method"`
ContentType string `json:"content_type"`
}

type Query2BodyModifier struct {
template string
method string
contentType string
}

func (m *Query2BodyModifier) ModifyRequest(req *http.Request) error {
if req.Body == nil {
return nil
}
payloadBytes, err := ioutil.ReadAll(req.Body)
if err != nil {
return err
}
req.Body.Close()
log.Println("Payload", string(payloadBytes))
log.Println("Template", string(m.template))
k, err := kazaam.NewKazaam(m.template)
if err != nil {
return err
}

tranformedDataBytes, err := k.TransformJSONString(string(payloadBytes))
if err != nil {
return err
}

log.Println("Transform Data Bytes", string(tranformedDataBytes))

buf := new(bytes.Buffer)
buf.Read(tranformedDataBytes)
if m.method != "" {
req.Method = m.method
}
if m.contentType != "" {
req.Header.Set("Content-Type", m.contentType)
} else {
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
}
req.ContentLength = int64(len(tranformedDataBytes))
req.Body = ioutil.NopCloser(bytes.NewReader(tranformedDataBytes))

return nil
}

func FromJSON(b []byte) (*Query2BodyModifier, error) {
cfg := &Config{}
if err := json.Unmarshal(b, cfg); err != nil {
return nil, err
}

bytes, err := b64.StdEncoding.DecodeString(cfg.Template) // Converting data
if err != nil {
return nil, err
}

return &Query2BodyModifier{
template: string(bytes),
method: cfg.Method,
}, nil
}

func FromResponseJSON(b []byte) (*Query2BodyModifier, error) {
cfg := &Config{}
if err := json.Unmarshal(b, cfg); err != nil {
return nil, err
}

bytes, err := b64.StdEncoding.DecodeString(cfg.Template) // Converting data
if err != nil {
return nil, err
}

return &Query2BodyModifier{
template: string(bytes),
method: cfg.Method,
}, nil
}

// ModifyResponse sets the Content-Type header and overrides the response body.
func (m *Query2BodyModifier) ModifyResponse(res *http.Response) error {
log.Println("body.ModifyResponse: request: %s", res.Request.URL)

if res.Body == nil {
return nil
}
responseBodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
// Replace the existing body, close it first.
res.Body.Close()

res.Header.Set("Content-Type", m.contentType)

// Reset the Content-Encoding since we know that the new body isn't encoded.
res.Header.Del("Content-Encoding")

log.Println("Response", string(responseBodyBytes))
log.Println("Template", string(m.template))
k, err := kazaam.NewKazaam(m.template)
if err != nil {
return err
}

tranformedDataBytes, err := k.TransformJSONString(string(responseBodyBytes))
if err != nil {
return err
}

log.Println("Transform Data Bytes", string(tranformedDataBytes))

buf := new(bytes.Buffer)
buf.Read(tranformedDataBytes)
res.ContentLength = int64(len(tranformedDataBytes))
res.Body = ioutil.NopCloser(bytes.NewReader(tranformedDataBytes))

return nil
}
29 changes: 29 additions & 0 deletions plugins/requestbodytransformer/modifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package requestbodytransformer

import (
"github.com/google/martian/parse"
"github.com/kyawmyintthein/lura-twirp/plugins/requestbodytransformer/modifier"
)

func init() {
parse.Register("body.FromRequestBody", FromJSON)
parse.Register("body.ToResponseBody", FromRespJSON)
}

func FromJSON(b []byte) (*parse.Result, error) {
msg, err := modifier.FromJSON(b)
if err != nil {
return nil, err
}

return parse.NewResult(msg, []parse.ModifierType{parse.Request})
}

func FromRespJSON(b []byte) (*parse.Result, error) {
msg, err := modifier.FromResponseJSON(b)
if err != nil {
return nil, err
}

return parse.NewResult(msg, []parse.ModifierType{parse.Request})
}
142 changes: 142 additions & 0 deletions plugins/requestbodytransformer/modifier/modifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Package modifier exposes a request modifier for generating bodies
// from the querystring params
package modifier

import (
"bytes"
b64 "encoding/base64"
"encoding/json"
"io/ioutil"
"log"
"net/http"

kazaam "gopkg.in/qntfy/kazaam.v3"
)

type Config struct {
URLPattern string `json:"url_pattern"`
Template string `json:"template"`
Method string `json:"method"`
ContentType string `json:"content_type"`
}

type Query2BodyModifier struct {
template string
method string
contentType string
}

func (m *Query2BodyModifier) ModifyRequest(req *http.Request) error {
log.Println("Request received")
if req.Body == nil {
return nil
}
payloadBytes, err := ioutil.ReadAll(req.Body)
if err != nil {
return err
}
req.Body.Close()
log.Println("Payload", string(payloadBytes))
log.Println("Template", string(m.template))
k, err := kazaam.NewKazaam(m.template)
if err != nil {
return err
}

tranformedDataBytes, err := k.TransformJSONString(string(payloadBytes))
if err != nil {
return err
}

log.Println("Transform Data Bytes", string(tranformedDataBytes))

buf := new(bytes.Buffer)
buf.Read(tranformedDataBytes)
if m.method != "" {
req.Method = m.method
}
if m.contentType != "" {
req.Header.Set("Content-Type", m.contentType)
} else {
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
}
req.ContentLength = int64(len(tranformedDataBytes))
req.Body = ioutil.NopCloser(bytes.NewReader(tranformedDataBytes))

return nil
}

func FromJSON(b []byte) (*Query2BodyModifier, error) {
cfg := &Config{}
if err := json.Unmarshal(b, cfg); err != nil {
return nil, err
}

bytes, err := b64.StdEncoding.DecodeString(cfg.Template) // Converting data
if err != nil {
return nil, err
}

return &Query2BodyModifier{
template: string(bytes),
method: cfg.Method,
}, nil
}

func FromResponseJSON(b []byte) (*Query2BodyModifier, error) {
cfg := &Config{}
if err := json.Unmarshal(b, cfg); err != nil {
return nil, err
}

bytes, err := b64.StdEncoding.DecodeString(cfg.Template) // Converting data
if err != nil {
return nil, err
}

return &Query2BodyModifier{
template: string(bytes),
method: cfg.Method,
}, nil
}

// ModifyResponse sets the Content-Type header and overrides the response body.
func (m *Query2BodyModifier) ModifyResponse(res *http.Response) error {
log.Println("body.ModifyResponse: request: %s", res.Request.URL)

if res.Body == nil {
return nil
}
responseBodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
// Replace the existing body, close it first.
res.Body.Close()

res.Header.Set("Content-Type", m.contentType)

// Reset the Content-Encoding since we know that the new body isn't encoded.
res.Header.Del("Content-Encoding")

log.Println("Response", string(responseBodyBytes))
log.Println("Template", string(m.template))
k, err := kazaam.NewKazaam(m.template)
if err != nil {
return err
}

tranformedDataBytes, err := k.TransformJSONString(string(responseBodyBytes))
if err != nil {
return err
}

log.Println("Transform Data Bytes", string(tranformedDataBytes))

buf := new(bytes.Buffer)
buf.Read(tranformedDataBytes)
res.ContentLength = int64(len(tranformedDataBytes))
res.Body = ioutil.NopCloser(bytes.NewReader(tranformedDataBytes))

return nil
}

0 comments on commit 523d5df

Please sign in to comment.