11package cmd
22
33import (
4- "context"
54 "fmt"
6- "net/http"
75 "os"
8- "time"
96
107 "github.com/aws/aws-lambda-go/lambda"
118 log "github.com/sirupsen/logrus"
129 "github.com/spf13/cobra"
1310
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 "
1512)
1613
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-
6414// lambdaHandlerCmd is the Cobra command invoked when the binary is run with the "lambda-handler" argument.
6515// This happens inside the AWS Lambda environment via the bootstrap script.
6616var lambdaHandlerCmd = & cobra.Command {
@@ -79,7 +29,7 @@ var lambdaHandlerCmd = &cobra.Command{
7929 // The aws-lambda-go library takes over execution when lambda.Start is called.
8030 // It handles reading events, invoking the handler, and writing responses.
8131 log .Info ("Lambda Handler: Starting AWS Lambda handler loop." )
82- lambda .Start (handleLambdaEvent )
32+ lambda .Start (runner . HandleLambdaEvent )
8333 // lambda.Start blocks and never returns unless there's a critical error during initialization
8434 log .Error ("Lambda Handler: lambda.Start returned unexpectedly (should not happen)" )
8535 return fmt .Errorf ("lambda.Start returned unexpectedly" )
0 commit comments