-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (39 loc) · 1.04 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
package fume
import (
"context"
"fmt"
"net/http"
"os"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
ginadapter "github.com/awslabs/aws-lambda-go-api-proxy/gin"
"github.com/gin-gonic/gin"
)
var ginLambda *ginadapter.GinLambdaV2
type Options struct {
// optional - hostname to listen on (default: localhost)
Host string
// optional - port to run on (default: 8080)
Port int
}
func Start(routes *gin.Engine, options Options) {
defaults := &Options{
Host: "localhost",
Port: 8080,
}
if options.Port != 0 { defaults.Port = options.Port }
if options.Host != "" { defaults.Host = options.Host }
if os.Getenv("_HANDLER") != "" {
ginLambda = ginadapter.NewV2(routes)
lambda.Start(Handler)
} else {
server := &http.Server{
Addr: fmt.Sprintf("%s:%d", defaults.Host, defaults.Port),
Handler: routes,
}
server.ListenAndServe()
}
}
func Handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
return ginLambda.ProxyWithContext(ctx, req)
}