-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_local.go
133 lines (117 loc) · 3.26 KB
/
main_local.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
package main
import (
"aws-lambda-s3/handler"
"aws-lambda-s3/repositories"
"context"
"fmt"
"net/http"
"os"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
)
var (
env = os.Getenv("ENV")
region = os.Getenv("AWS_REGION")
hndlr MyHandler
)
// type Handler struct {
// S3Client *repositories.S3Client
// }
type MyHandler struct {
Handler handler.Handler
}
type Msg struct {
Message string
}
type Response struct {
Objects []string `json:"objects"`
NextContinuationToken string `json:"nextContinuationToken"`
}
func initializeAWSCfg(ctx context.Context) (cfg aws.Config, error error) {
if strings.ToLower(env) == "dev" {
return config.LoadDefaultConfig(
ctx,
config.WithRegion(region),
config.WithClientLogMode(aws.LogRequest),
config.WithClientLogMode(aws.LogRequestWithBody),
config.WithClientLogMode(aws.LogResponseWithBody),
config.WithClientLogMode(aws.LogResponse),
)
}
return config.LoadDefaultConfig(ctx, config.WithRegion(region))
}
func main() {
// Initialize the AWS configuration
awsCfg, awsCfgErr := initializeAWSCfg(context.TODO())
if awsCfgErr != nil {
panic("unable to initialize AWS Config")
}
// Initialize S3 Client
s3Client := repositories.S3Client{
Client: repositories.InitializeS3Client(awsCfg),
}
hndlr = MyHandler{
Handler: handler.Handler{
S3Client: &s3Client,
},
}
// Create a mock API Gateway request
request := events.APIGatewayProxyRequest{
//HTTPMethod: "GET",
HTTPMethod: "GET",
// PathParameters: map[string]string{
// "source": "create_folder",
// "action": "SELECT",
// "baseEncodedDocumentTitle": "UFJUWV9JRCNDVVNUX0lECg==",
// },
PathParameters: map[string]string{
"source": "",
"action": "",
"baseEncodedDocumentTitle": "",
},
// PathParameters: map[string]string{
// "source": "create_folder",
// "action": "INSERT",
// "baseEncodedDocumentTitle": "UFJUWV9JRCNDVVNUX0lECg==",
// },
}
// Set environment variables locally
os.Setenv("BUCKET_NAME", "bcdr-lambda-function")
os.Setenv("ENV", "dev")
os.Setenv("AWS_REGION", "us-east-1")
// Call the handler function
response, err := hndlr.handleRequest(request)
if err != nil {
fmt.Println("Error: ", err)
} else {
fmt.Println("Response: ", response)
}
}
func (h *MyHandler) handleRequest(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
bucketName := os.Getenv("BUCKET_NAME")
// if bucketName == "" {
// return events.APIGatewayProxyResponse{
// StatusCode: http.StatusInternalServerError,
// Body: "BUCKET_NAME environment variable is not set",
// }, nil
// }
// if h.Handler.S3Client == nil {
// return events.APIGatewayProxyResponse{
// StatusCode: http.StatusInternalServerError,
// Body: "S3Client is not initialized",
// }, nil
// }
switch request.HTTPMethod {
case "GET":
return h.Handler.GetHandler(request, bucketName)
case "DELETE":
return h.Handler.DeleteHandler(request, bucketName)
default:
return events.APIGatewayProxyResponse{
StatusCode: http.StatusMethodNotAllowed,
Body: "Unsupported HTTP method",
}, nil
}
}