From 211f3446fc933f263384d728760f12c4c4a46e4b Mon Sep 17 00:00:00 2001
From: Jan Semmelink <jan@uafrica.com>
Date: Wed, 29 Sep 2021 08:36:35 +0200
Subject: [PATCH] Added CurrentRequestID to api.Context for legacy handlers

---
 api/api.go    | 4 ++++
 api/lambda.go | 4 ++++
 2 files changed, 8 insertions(+)

diff --git a/api/api.go b/api/api.go
index 79dd9ea..5fcf8ca 100644
--- a/api/api.go
+++ b/api/api.go
@@ -18,6 +18,10 @@ import (
 	"gitlab.com/uafrica/go-utils/service"
 )
 
+//LEGACY: global variable is set only for backward compatibility
+//When handlers are changed to accept context, they should get this from the context
+var CurrentRequestID *string
+
 //New creates the API with the specified routes keys on [path][method]
 //value could be any of the handler function signatures supported by the api.Router
 //requestIDHeaderKey is defined in the response header to match the requestID from the request
diff --git a/api/lambda.go b/api/lambda.go
index ec476a3..5f4836e 100644
--- a/api/lambda.go
+++ b/api/lambda.go
@@ -113,6 +113,10 @@ func (api Api) Handler(baseCtx context.Context, apiGatewayProxyReq events.APIGat
 
 	rand.Seed(time.Now().Unix())
 
+	//LEGACY: delete this as soon as all handlers accepts context
+	//this does not support concurrent execution!
+	CurrentRequestID = &ctx.Request.RequestContext.RequestID
+
 	ctx.Debugf("HTTP %s %s ...\n", apiGatewayProxyReq.HTTPMethod, apiGatewayProxyReq.Resource)
 	ctx.WithFields(map[string]interface{}{
 		"http_method":                ctx.Request.HTTPMethod,
-- 
GitLab