Skip to content
Snippets Groups Projects
Select Git revision
  • d98ac562356f074dad24b2c58e5ae20a28f26821
  • dev default protected
  • prod protected
  • 1.0.58
  • 1.0.57
  • 1.0.52
  • 1.0.56
  • 1.0.51
  • 1.0.50
  • 1.0.33
  • 1.0.32
  • 1.0.31
  • 1.0.30
  • 1.0.29
  • 1.0.28
  • 1.0.27
  • 1.0.26
  • 1.0.25
  • 1.0.24
  • 1.0.23
  • 1.0.22
  • 1.0.21
  • 1.0.20
23 results

php.xml

Blame
  • debug.go 2.86 KiB
    package handler_utils
    
    import (
    	"bytes"
    	"context"
    	"encoding/json"
    	"fmt"
    	"io"
    	"net/http"
    	"os"
    	"strings"
    
    	"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"
    )
    
    // ========================================================================
    // 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]
    	}
    
    	headers := map[string]string{}
    	for key, values := range req.Header {
    		if len(values) > 0 {
    			headers[key] = values[0]
    		}
    	}
    
    	// Call lambda function
    	request := events.APIGatewayProxyRequest{
    		Path:                  req.URL.Path,
    		Resource:              req.URL.Path,
    		HTTPMethod:            req.Method,
    		QueryStringParameters: query,
    		Body:                  body,
    		Headers:               headers,
    	}
    
    	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
    	}