Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
49bcad4f
Commit
49bcad4f
authored
2 years ago
by
Johan de Klerk
Browse files
Options
Downloads
Patches
Plain Diff
Debug sqs and http calls
parent
774d5615
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
handler_utils/debug.go
+116
-0
116 additions, 0 deletions
handler_utils/debug.go
sqs/sqs.go
+11
-1
11 additions, 1 deletion
sqs/sqs.go
with
127 additions
and
1 deletion
handler_utils/debug.go
0 → 100644
+
116
−
0
View file @
49bcad4f
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
(
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
)
ctx
:=
context
.
Background
()
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
(
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
()
sqsType
:=
req
.
URL
.
Path
sqsType
=
strings
.
ReplaceAll
(
sqsType
,
"/sqs/"
,
""
)
// Call lambda function
sqsRequest
,
_
:=
json
.
Marshal
(
events
.
SQSEvent
{
Records
:
[]
events
.
SQSMessage
{
{
Body
:
body
,
MessageAttributes
:
map
[
string
]
events
.
SQSMessageAttribute
{
"type"
:
{
StringValue
:
utils
.
PointerValue
(
sqsType
),
},
},
},
},
})
ctx
:=
context
.
Background
()
responseBytes
,
err
:=
lambdaHandler
.
Invoke
(
ctx
,
sqsRequest
)
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
))
}
This diff is collapsed.
Click to expand it.
sqs/sqs.go
+
11
−
1
View file @
49bcad4f
...
@@ -14,6 +14,7 @@ import (
...
@@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/aws/aws-sdk-go/service/sqs"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs"
"github.com/go-resty/resty/v2"
)
)
var
sqsClient
*
sqs
.
SQS
var
sqsClient
*
sqs
.
SQS
...
@@ -102,7 +103,16 @@ func (m *Messenger) SendSQSMessage(headers map[string]string, body string, curre
...
@@ -102,7 +103,16 @@ func (m *Messenger) SendSQSMessage(headers map[string]string, body string, curre
return
*
res
.
MessageId
,
err
return
*
res
.
MessageId
,
err
}
}
func
SendSQSMessage
(
msgr
Messenger
,
objectToSend
interface
{},
currentRequestID
*
string
,
sqsType
string
)
error
{
func
SendSQSMessage
(
msgr
Messenger
,
objectToSend
interface
{},
currentRequestID
*
string
,
sqsType
string
,
isDebug
bool
)
error
{
if
isDebug
{
resty
.
New
()
.
R
()
.
SetBody
(
objectToSend
)
.
Post
(
"http://127.0.0.1:3000/sqs/"
+
sqsType
)
return
nil
}
if
sqsClient
==
nil
{
if
sqsClient
==
nil
{
var
err
error
var
err
error
sqsClient
,
err
=
NewSQSClient
(
msgr
.
Region
)
sqsClient
,
err
=
NewSQSClient
(
msgr
.
Region
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment