-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
198 lines (147 loc) · 5.02 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"testeSQSGsqgLambda/cadastro"
"github.com/Pinablink/sqg"
"github.com/Pinablink/sqg/util"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
//
func HandleRequest(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
var nameQueue string = os.Getenv("NAME_QUEUE")
var refSqg *sqg.SQG = sqg.NewSQG(nameQueue)
if request.HTTPMethod == "POST" {
return postInQueue(request, refSqg, nameQueue)
} else if request.HTTPMethod == "GET" {
return getInQueue(request, refSqg, nameQueue)
} else {
messageresponse := "Verbo HTTP inválido para essa chamada"
responseObj := cadastro.CadastroStatus{
Status: "ERROR",
Message: messageresponse,
}
apiResponse := facResponse(responseObj, nil)
return apiResponse, nil
}
}
// getInQueue: Obtêm a mensagem disponível na fila
func getInQueue(request events.APIGatewayProxyRequest, sqgref *sqg.SQG, nameQueue string) (events.APIGatewayProxyResponse, error) {
var apiResponse events.APIGatewayProxyResponse
var responseObj interface{}
var formatMessageResponse string = "%s. %s"
var strMessageResponse string
var refCadastro *cadastro.Cadastro = &cadastro.Cadastro{}
var refCadastroMsgHeader *cadastro.CadastroMsgHeader = &cadastro.CadastroMsgHeader{}
reviewMessageOK, deleteMessageOK, err := sqgref.GetMsgInQueue(refCadastro, refCadastroMsgHeader)
if reviewMessageOK {
if err != nil {
strMessageResponse = "Ocorreu um erro na obtenção de dados na fila."
strMessageResponse = fmt.Sprintf(formatMessageResponse, strMessageResponse, err.Error())
} else if !reviewMessageOK {
strMessageResponse = "Não foi encontrado mensagem na fila."
strMessageResponse = fmt.Sprintf(formatMessageResponse, strMessageResponse, "")
} else if reviewMessageOK && !deleteMessageOK {
strMessageResponse = "Mensagem na fila não foi deletada"
strMessageResponse = fmt.Sprintf(formatMessageResponse, strMessageResponse, "")
} else if reviewMessageOK {
strMessageResponse = "Mensagem obtida"
strMessageResponse = fmt.Sprintf(formatMessageResponse, strMessageResponse, "")
}
responseObj = cadastro.TesteCadastro{
CadastroHeader: *refCadastroMsgHeader,
DataCadastro: *refCadastro,
}
} else {
responseObj = cadastro.TesteCadastroResponseMessage{
Message: "Não existe mensagem na fila para consulta",
}
}
apiResponse = facResponse(responseObj, nil)
return apiResponse, nil
}
// postInQueue: Adiciona Mensagem a uma fila SQS AWS
// request - Struct de requisição do API Gateway
// refSqg - Struct com os dados da mensagem
// nameQueue - Nome da fila
func postInQueue(request events.APIGatewayProxyRequest, refSqg *sqg.SQG, nameQueue string) (events.APIGatewayProxyResponse, error) {
var apiResponse events.APIGatewayProxyResponse
var responseObj cadastro.CadastroStatus
err, cadastroRequest := getRequestData(request)
if err != nil {
messageresponse := "Erro ao incluir cadastro"
responseObj = cadastro.CadastroStatus{
Status: "ERROR",
Message: messageresponse,
}
err = errors.New(messageresponse)
} else {
if len(nameQueue) == 0 {
messageresponse := "Identificacao da fila não encontrado"
responseObj = cadastro.CadastroStatus{
Status: "ERROR",
Message: messageresponse,
}
err = errors.New(messageresponse)
} else {
headerMessage := cadastroRequest.CadastroHeader
mcadastro := cadastroRequest.DataCadastro
var messageModel util.GSQGMessageModel = util.GSQGMessageModel{
ContentMessage: mcadastro,
DataMessage: headerMessage,
}
refSqg.SetGSQGMessageModel(messageModel)
responseId, responseErr := refSqg.JoinTheQueue()
if responseErr != nil {
messageError := "Erro na inclusao de dados na fila\n %s"
messageError = fmt.Sprintf(messageError, responseErr)
responseObj = cadastro.CadastroStatus{
Status: "ERROR",
Message: messageError,
}
err = errors.New(messageError)
} else {
responseObj = cadastro.CadastroStatus{
Status: "OK",
Message: "Sucesso ao incluir cadastro na fila",
IdResponse: *responseId,
}
}
}
}
apiResponse = facResponse(responseObj, err)
return apiResponse, err
}
//
func getRequestData(request events.APIGatewayProxyRequest) (error, cadastro.TesteCadastro) {
byteBody := []byte(request.Body)
var cadastroRequest cadastro.TesteCadastro
err := json.Unmarshal(byteBody, &cadastroRequest)
return err, cadastroRequest
}
//
func facResponse(structResponse interface{}, refError error) events.APIGatewayProxyResponse {
var mapHeader map[string]string
var dataByte []byte
var dataReturn string
var statusCode int
statusCode = 200
mapHeader = make(map[string]string)
mapHeader["Content-Type"] = "application/json"
dataByte, refError = json.Marshal(structResponse)
if refError != nil {
statusCode = 500
}
dataReturn = string(dataByte)
return events.APIGatewayProxyResponse{
Body: dataReturn,
StatusCode: statusCode,
Headers: mapHeader,
}
}
func main() {
lambda.Start(HandleRequest)
}