1
1
package cmd
2
2
3
3
import (
4
- "context"
5
4
"fmt"
6
- "net/http"
7
5
"os"
8
- "time"
9
6
10
7
"github.com/aws/aws-lambda-go/lambda"
11
8
log "github.com/sirupsen/logrus"
12
9
"github.com/spf13/cobra"
13
10
14
- "github.com/gitpod-io/enterprise-deployment-toolkit/gitpod-network-check/pkg/lambda_types "
11
+ "github.com/gitpod-io/enterprise-deployment-toolkit/gitpod-network-check/pkg/runner "
15
12
)
16
13
17
- // Core logic for handling the Lambda event
18
- // This function is called by the aws-lambda-go library.
19
- func handleLambdaEvent (ctx context.Context , request lambda_types.CheckRequest ) (lambda_types.CheckResponse , error ) {
20
- log .Infof ("Lambda Handler: Received check request for %d endpoints." , len (request .Endpoints ))
21
-
22
- response := lambda_types.CheckResponse {
23
- Results : make (map [string ]lambda_types.CheckResult ),
24
- }
25
-
26
- client := & http.Client {
27
- Timeout : 10 * time .Second , // Consider making this configurable if needed
28
- }
29
-
30
- // Perform checks
31
- for name , url := range request .Endpoints {
32
- log .Debugf ("Lambda Handler: Checking endpoint: %s (%s)" , name , url )
33
-
34
- // Use the context provided by the Lambda runtime
35
- req , err := http .NewRequestWithContext (ctx , "GET" , url , nil )
36
- if err != nil {
37
- response .Results [name ] = lambda_types.CheckResult {Success : false , Error : fmt .Sprintf ("failed to create request: %v" , err )}
38
- log .Warnf (" -> Failed (request creation): %v" , err )
39
- continue
40
- }
41
-
42
- resp , err := client .Do (req )
43
- if err != nil {
44
- response .Results [name ] = lambda_types.CheckResult {Success : false , Error : fmt .Sprintf ("HTTP request failed: %v" , err )}
45
- log .Warnf (" -> Failed (HTTP request): %v" , err )
46
- } else {
47
- resp .Body .Close () // Ensure body is closed
48
- if resp .StatusCode >= 200 && resp .StatusCode < 300 {
49
- response .Results [name ] = lambda_types.CheckResult {Success : true }
50
- log .Debugf (" -> Success (Status: %d)" , resp .StatusCode )
51
- } else {
52
- response .Results [name ] = lambda_types.CheckResult {Success : false , Error : fmt .Sprintf ("unexpected status code: %d" , resp .StatusCode )}
53
- log .Warnf (" -> Failed (Status: %d)" , resp .StatusCode )
54
- }
55
- }
56
- }
57
-
58
- log .Info ("Lambda Handler: Check processing complete." )
59
- // The lambda library handles marshalling the response and deals with errors.
60
- // We return the response struct and nil error if processing logic itself didn't fail critically.
61
- return response , nil
62
- }
63
-
64
14
// lambdaHandlerCmd is the Cobra command invoked when the binary is run with the "lambda-handler" argument.
65
15
// This happens inside the AWS Lambda environment via the bootstrap script.
66
16
var lambdaHandlerCmd = & cobra.Command {
@@ -79,7 +29,7 @@ var lambdaHandlerCmd = &cobra.Command{
79
29
// The aws-lambda-go library takes over execution when lambda.Start is called.
80
30
// It handles reading events, invoking the handler, and writing responses.
81
31
log .Info ("Lambda Handler: Starting AWS Lambda handler loop." )
82
- lambda .Start (handleLambdaEvent )
32
+ lambda .Start (runner . HandleLambdaEvent )
83
33
// lambda.Start blocks and never returns unless there's a critical error during initialization
84
34
log .Error ("Lambda Handler: lambda.Start returned unexpectedly (should not happen)" )
85
35
return fmt .Errorf ("lambda.Start returned unexpectedly" )
0 commit comments