-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (51 loc) · 1.51 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"flag"
"fmt"
"net/http"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
echoadapter "github.com/awslabs/aws-lambda-go-api-proxy/echo"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
api "github.com/seanrmurphy/lambda-openapi3-test/api"
)
var e *echo.Echo
var echoAdapter *echoadapter.EchoLambda
var nullHandler = false
// Lambda mode determines whether this is run locally or remotely
var lambdaMode = false
func init() {
// Create an instance of our handler which satisfies the generated interface
a := api.NewApi()
e = echo.New()
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "method=${method}, uri=${uri}, status=${status}\n",
}))
api.RegisterHandlers(e, a)
if lambdaMode {
echoAdapter = echoadapter.New(e)
}
}
// Handler handles API requests
func Handler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
if nullHandler {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Body: `{"message": "Null handler operational - always returns this message"}`,
}, nil
} else {
fmt.Printf("Path = %v, Resource = %v, req = %v\n", req.Path, req.Resource, req)
req.Path = req.Path[(len(req.RequestContext.Stage) + 2):]
return echoAdapter.Proxy(req)
}
}
func main() {
if lambdaMode {
lambda.Start(Handler)
} else {
var port = flag.Int("port", 8080, "Port for test HTTP server")
flag.Parse()
e.Logger.Fatal(e.Start(fmt.Sprintf("0.0.0.0:%d", *port)))
}
}