Skip to content
Snippets Groups Projects
Select Git revision
  • 83358e589a34acdfa50cc8b04a6b64105c794b00
  • main default protected
  • trading_hours
  • refactor_trading_hours
  • audit_cleaning_cater_for_non_struct_fields
  • remove-info-logs
  • sl-refactor
  • 18-use-scan-for-param-values
  • 17-order-search-results
  • 4-simplify-framework-2
  • 1-http-error
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
  • v1.278.0
31 results

string_utils.go

Blame
  • lambda.go 9.50 KiB
    package api
    
    import (
    	"context"
    	"database/sql"
    	"encoding/json"
    	"fmt"
    	"math/rand"
    	"net/http"
    	"reflect"
    	"time"
    
    	"github.com/aws/aws-lambda-go/events"
    	"github.com/aws/aws-lambda-go/lambdacontext"
    	"gitlab.com/uafrica/go-utils/errors"
    	"gitlab.com/uafrica/go-utils/logger"
    )
    
    func (api Api) NewContext(baseCtx context.Context, requestID string, request events.APIGatewayProxyRequest) (Context, error) {
    	serviceContext, err := api.Service.NewContext(baseCtx, requestID, nil)
    	if err != nil {
    		return nil, err
    	}
    
    	return &apiContext{
    		Context: serviceContext,
    		request: request,
    	}, nil
    }
    
    //this is native handler for lambda passed into lambda.Start()
    //to run locally, this is called from app.ServeHTTP()
    func (api Api) Handler(baseCtx context.Context, apiGatewayProxyReq events.APIGatewayProxyRequest) (res events.APIGatewayProxyResponse, err error) {
    	res = events.APIGatewayProxyResponse{
    		StatusCode: http.StatusInternalServerError,
    		Body:       "undefined response",
    		Headers:    map[string]string{},
    	}
    
    	// Replace the proxy resource with the path, has some edge cases but works for our current API implementation
    	// Edge case being that if have path params then specify those routes explicitly
    	if apiGatewayProxyReq.Resource == "/{proxy+}" {
    		apiGatewayProxyReq.Resource = apiGatewayProxyReq.Path
    	}
    
    	//get request-id from HTTP headers (used when making internal service calls)
    	//if not defined in header, get the AWS request id from the AWS context
    	requestID, ok := apiGatewayProxyReq.Headers[api.requestIDHeaderKey]
    	if !ok || requestID == "" {
    		if lambdaContext, ok := lambdacontext.FromContext(baseCtx); ok && lambdaContext != nil {
    			requestID = lambdaContext.AwsRequestID
    		}
    	}
    
    	//service context invoke the starters and could fail, e.g. if cannot connect to db
    	ctx, err := api.NewContext(baseCtx, requestID, apiGatewayProxyReq)
    	if err != nil {
    		return res, err
    	}
    
    	//report handler crashes
    	if api.crashReporter != nil {
    		defer api.crashReporter.Catch(ctx)
    	}
    
    	defer func() {
    		//set CORS headers on every response
    		if api.cors != nil {
    			for n, v := range api.cors.CORS() {
    				res.Headers[n] = v