-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustom.go
46 lines (41 loc) · 1.16 KB
/
Custom.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
package response
import (
"encoding/json"
"fmt"
"os"
"github.com/kkesley/go-apigw-httpparser"
"github.com/aws/aws-lambda-go/events"
)
//Custom return custom code and message
func Custom(code int, message string, err error) (events.APIGatewayProxyResponse, error) {
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
}
fmt.Fprintf(os.Stderr, "RESPONSE-%d: %s", code, message)
return events.APIGatewayProxyResponse{
Body: message,
IsBase64Encoded: false,
Headers: map[string]string{
"Content-Type": "text/plain",
"Access-Control-Allow-Origin": "*",
},
StatusCode: code,
}, nil
}
//CustomJSON return custom code and message
func CustomJSON(code int, responseObject interface{}, extErr error) (events.APIGatewayProxyResponse, error) {
resByte, err := json.Marshal(responseObject)
if err != nil {
return ServerError(err)
}
httpparser.LogResponse(code, responseObject)
return events.APIGatewayProxyResponse{
Body: string(resByte),
IsBase64Encoded: false,
Headers: map[string]string{
"Content-Type": "text/plain",
"Access-Control-Allow-Origin": "*",
},
StatusCode: code,
}, nil
}