-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
41 lines (34 loc) · 857 Bytes
/
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
package main
import (
"encoding/json"
"fmt"
"github.com/a-h/awsapigatewayv2handler"
"github.com/aws/aws-lambda-go/lambda"
"net/http"
"os"
"time"
)
var httpLambdaHandler lambda.Handler
type Response struct {
From string
Message string
}
func init() {
http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
_ = json.NewEncoder(w).Encode(Response{From: "net/http", Message: time.Now().Format(time.UnixDate)})
})
httpLambdaHandler = awsapigatewayv2handler.NewLambdaHandler(http.DefaultServeMux)
}
func main() {
if isRunningOnLambda() {
// Run on AWS Lambda
lambda.Start(httpLambdaHandler)
} else {
// Run local server
fmt.Println("Running on 0.0.0.0:8080")
_ = http.ListenAndServe("0.0.0.0:8080", http.DefaultServeMux)
}
}
func isRunningOnLambda() bool {
return os.Getenv("AWS_LAMBDA_RUNTIME_API") != ""
}