-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
119 lines (111 loc) · 3.27 KB
/
handlers.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package websocket
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/apigatewaymanagementapi"
)
var apiClient *apigatewaymanagementapi.Client
func init() {
cfg, err := config.LoadDefaultConfig(
context.TODO(),
)
if err != nil {
log.Printf("Error creating AWS session: %v", err)
return
}
apiClient = apigatewaymanagementapi.NewFromConfig(cfg)
}
func publish(ctx context.Context, endpoint, id string, data []byte) error {
if endpoint == "" {
return errors.New("endpointunknown")
}
_, err := apiClient.PostToConnection(
context.TODO(),
&apigatewaymanagementapi.PostToConnectionInput{
Data: data,
ConnectionId: &id,
},
apigatewaymanagementapi.WithEndpointResolver(
apigatewaymanagementapi.EndpointResolverFromURL(endpoint),
),
)
return err
}
func WebsocketHandler(_ context.Context, req *events.APIGatewayWebsocketProxyRequest) (events.APIGatewayProxyResponse, error) {
b, err := json.Marshal(req)
if err != nil {
log.Printf("Error marshalling request: %v", err)
return InternalServerErrorResponse(), nil
}
log.Printf("running, Method %s", string(b))
ws := &WebsocketLambda{
ConnectionID: req.RequestContext.ConnectionID,
EventType: req.RequestContext.EventType,
ChannelID: "test",
ResourceID: req.RequestContext.ResourceID,
ExtendedRequestID: req.RequestContext.ExtendedRequestID,
RequestTimeEpoch: req.RequestContext.RequestTimeEpoch,
Endpoint: fmt.Sprintf("https://%s/%s", req.RequestContext.DomainName, req.RequestContext.Stage),
}
switch ws.EventType {
case "CONNECT":
err := ws.save()
if err != nil {
log.Printf("Error connecting websocket: %v", err)
return InternalServerErrorResponse(), err
}
return OkResponse(), nil
case "DISCONNECT":
err := ws.delete()
if err != nil {
log.Printf("Error disconnecting websocket: %v", err)
return InternalServerErrorResponse(), err
}
default:
msg := fmt.Sprintf("%s? Roger!)", req.Body)
err := publish(context.TODO(), ws.Endpoint, ws.ConnectionID, []byte(msg))
if err != nil {
log.Printf("Error message websocket: %v", err)
return InternalServerErrorResponse(), err
}
return OkResponse(), nil
}
return OkResponse(), nil
}
func HTTPHandler(_ context.Context, req *events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
b, err := json.Marshal(req)
if err != nil {
log.Printf("Error marshalling request: %v", err)
return HTTPInternalServerErrorResponse(), nil
}
if req.RequestContext.HTTP.Method != "POST" {
log.Printf("running, Method %s", string(b))
}
message := req.Body
if req.IsBase64Encoded {
data, err := base64.StdEncoding.DecodeString(message)
if err != nil {
log.Printf("Error decoding base64 body: %v", err)
return HTTPInternalServerErrorResponse(), nil
}
message = string(data)
}
l, err := getWebsocketLambda("test")
if err != nil {
log.Printf("Error decoding base64 body: %v", err)
return HTTPInternalServerErrorResponse(), nil
}
for _, v := range l {
err = publish(context.TODO(), v.Endpoint, v.ConnectionID, []byte(message))
if err != nil {
log.Printf("Error sending message: %v", err)
}
}
return HTTPOKResponse(), nil
}