Select Git revision
-
Johan de Klerk authoredJohan de Klerk authored
debug.go 2.55 KiB
package handler_utils
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/utils"
"io"
"net/http"
"os"
"strings"
)
// ========================================================================
// HTTP functions
// ========================================================================
func ServeHTTPFunctions(ctx context.Context, lambdaHandler lambda.Handler, w http.ResponseWriter, req *http.Request) {
// Read body
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(req.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
req.Body.Close()
body := buf.String()
// Read Query
queryValues := req.URL.Query()
query := map[string]string{}
for key, values := range queryValues {
query[key] = values[0]
}
// Call lambda function
request := events.APIGatewayProxyRequest{
Resource: req.URL.Path,
HTTPMethod: req.Method,
QueryStringParameters: query,
Body: body,
}
jsonRequest, _ := json.Marshal(request)
responseBytes, err := lambdaHandler.Invoke(ctx, jsonRequest)
if err != nil {
panic(err)
}
// Parse response
response := map[string]any{}
err = json.Unmarshal(responseBytes, &response)
if err != nil {
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(int(response["statusCode"].(float64)))
_, _ = io.WriteString(w, response["body"].(string))
}
// ========================================================================
// SQS Functions
// ========================================================================
func ServeSQSFunctions(ctx context.Context, lambdaHandler lambda.Handler, w http.ResponseWriter, req *http.Request) {