Skip to content

Commit

Permalink
fix: revert GCP changes in the handlers (#777)
Browse files Browse the repository at this point in the history
* fix: revert gcp changes in notify handler

* fix: revert gcp changes in decompress and webhook
  • Loading branch information
nourbalaha authored Jul 12, 2023
1 parent 5b7a877 commit aca4cc7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 29 deletions.
21 changes: 7 additions & 14 deletions server/internal/app/m2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"io"
"net/http"
"strings"

"github.com/labstack/echo/v4"
"github.com/reearth/reearth-cms/server/internal/adapter"
Expand All @@ -24,7 +23,7 @@ func NotifyHandler() echo.HandlerFunc {
if isAWS(c.Request()) {
input, err = parseSNSMessage(c.Request().Body)
} else if isGCP(c.Request()) {
input, err = parsePubSubMessage(c.Request().Body)
input, err = parsePubSubMessage(c, c.Request().Body)
} else {
err = errors.New("unsupported request source")
}
Expand Down Expand Up @@ -53,13 +52,8 @@ func isAWS(r *http.Request) bool {
}

func isGCP(r *http.Request) bool {
// TODO: need to find a better way to detect GCP requests
for headerName := range r.Header {
if strings.HasPrefix(strings.ToLower(headerName), "x-goog-") {
return true
}
}
return false
// TODO: need to find a way to detect GCP requests
return true
}

func parseSNSMessage(body io.Reader) (rhttp.NotifyInput, error) {
Expand All @@ -82,12 +76,11 @@ func parseSNSMessage(body io.Reader) (rhttp.NotifyInput, error) {
return input, nil
}

func parsePubSubMessage(body io.Reader) (rhttp.NotifyInput, error) {
var b pubsubBody
func parsePubSubMessage(c echo.Context, body io.Reader) (rhttp.NotifyInput, error) {
var input rhttp.NotifyInput

if err := json.NewDecoder(body).Decode(&b); err != nil {
if err := json.NewDecoder(body).Decode(&input); err != nil {
var b pubsubBody
if err := c.Bind(&b); err != nil {
if err := c.Bind(&input); err != nil {
return input, err
}
}
Expand Down
24 changes: 9 additions & 15 deletions worker/internal/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"io"
"net/http"
"strings"

"github.com/labstack/echo/v4"
rhttp "github.com/reearth/reearth-cms/worker/internal/adapter/http"
Expand All @@ -31,7 +30,7 @@ func (h Handler) DecompressHandler() echo.HandlerFunc {
if h.isAWS(c.Request()) {
input, err = parseSNSDecompressMessage(c.Request().Body)
} else if h.isGCP(c.Request()) {
input, err = parsePubSubDecompressMessage(c.Request().Body)
input, err = parsePubSubDecompressMessage(c, c.Request().Body)
} else {
err = errors.New("unsupported request source")
}
Expand All @@ -58,7 +57,7 @@ func (h Handler) WebhookHandler() echo.HandlerFunc {
if h.isAWS(c.Request()) {
webhook, err = parseSNSWebhookMessage(c.Request().Body)
} else if h.isGCP(c.Request()) {
webhook, err = parsePubSubWebhookMessage(c.Request().Body)
webhook, err = parsePubSubWebhookMessage(c, c.Request().Body)
} else {
err = errors.New("unsupported request source")
}
Expand All @@ -83,13 +82,8 @@ func (h Handler) isAWS(r *http.Request) bool {
}

func (h Handler) isGCP(r *http.Request) bool {
// TODO: need to find a better way to detect GCP requests
for headerName := range r.Header {
if strings.HasPrefix(strings.ToLower(headerName), "x-goog-") {
return true
}
}
return false
// TODO: need to find a way to detect GCP requests
return true
}

func parseSNSDecompressMessage(body io.Reader) (rhttp.DecompressInput, error) {
Expand All @@ -112,10 +106,10 @@ func parseSNSDecompressMessage(body io.Reader) (rhttp.DecompressInput, error) {
return input, nil
}

func parsePubSubDecompressMessage(body io.Reader) (rhttp.DecompressInput, error) {
func parsePubSubDecompressMessage(c echo.Context, body io.Reader) (rhttp.DecompressInput, error) {
var input rhttp.DecompressInput

if err := json.NewDecoder(body).Decode(&input); err != nil {
if err := c.Bind(&input); err != nil {
log.Errorf("failed to decompress: err=%s", err.Error())
return input, err
}
Expand Down Expand Up @@ -143,12 +137,12 @@ func parseSNSWebhookMessage(body io.Reader) (webhook.Webhook, error) {
return w, nil
}

func parsePubSubWebhookMessage(body io.Reader) (webhook.Webhook, error) {
func parsePubSubWebhookMessage(c echo.Context, body io.Reader) (webhook.Webhook, error) {
var msg msgBody
var w webhook.Webhook

if err := json.NewDecoder(body).Decode(&msg); err != nil {
if err := json.NewDecoder(body).Decode(&w); err != nil {
if err := c.Bind(&msg); err != nil {
if err := c.Bind(&w); err != nil {
return w, err
}
} else if data, err := msg.Data(); err != nil {
Expand Down

0 comments on commit aca4cc7

Please sign in to comment.