Skip to content

Commit

Permalink
add websocket enpoint management config
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjoz committed Jun 7, 2024
1 parent e449215 commit dfc5db5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
6 changes: 2 additions & 4 deletions backend/aws/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,13 @@ func (e DynamoTableRecords[T]) QueryBatch(querys []DynamoQueryParam) ([]T, error

output, err := client.Query(context.TODO(), &queryInput)
if err != nil {
core.Log("Error al ejecutar la DynamoDB")
panic(err)
return nil, core.Err("Error al ejecutar la query:", err)
}

items := []DynamoDBItem{}
err = attributevalue.UnmarshalListOfMaps(output.Items, &items)
if err != nil {
core.Log("Error al deserializar Dynamodb: " + err.Error())
return nil, err
return nil, core.Err("Error al deserializar DynamoDB:", err)
}

for _, dynamoItem := range items {
Expand Down
14 changes: 12 additions & 2 deletions backend/core/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io"
"log"
"net/http"
"net/url"
"os"
"runtime"
"strconv"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/andybalholm/brotli"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go-v2/service/apigatewaymanagementapi"
smithyendpoints "github.com/aws/smithy-go/endpoints"
"github.com/gorilla/websocket"
)

Expand Down Expand Up @@ -56,13 +58,21 @@ func PrintMemUsage() {

var apiClient *apigatewaymanagementapi.Client

type endpointResolverV2 struct{}

func (r endpointResolverV2) ResolveEndpoint(ctx context.Context, params apigatewaymanagementapi.EndpointParameters) (smithyendpoints.Endpoint, error) {
uri, _ := url.Parse("https://pv5s7gfoge.execute-api.us-east-1.amazonaws.com/p/")
return smithyendpoints.Endpoint{URI: *uri}, nil
}

func MakeAPIGatewayClient() *apigatewaymanagementapi.Client {
if apiClient == nil {
awsCfg := GetAwsConfig()

options := apigatewaymanagementapi.Options{
Credentials: awsCfg.Credentials,
Region: awsCfg.Region,
Credentials: awsCfg.Credentials,
Region: awsCfg.Region,
EndpointResolverV2: endpointResolverV2{},
}

apiClient = apigatewaymanagementapi.New(options)
Expand Down
15 changes: 3 additions & 12 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func LambdaHandler(_ context.Context, request *core.APIGatewayV2HTTPRequest) (*e
response := core.MainResponse{}
// Revisa si es websocket
if core.Contains(wssEvents, args.EventType) {
if args.EventType != "MESSAGE" {
return &events.APIGatewayProxyResponse{StatusCode: 200, Body: "OK"}, nil
}
fmt.Println("Parseando websocket:: ", args.ConnectionID)
awsArgs := ParseWssMessage([]byte(request.Body))
args.Body = awsArgs.Body
Expand Down Expand Up @@ -161,18 +164,6 @@ func ParseWssMessage(messageRaw []byte) core.HandlerArgs {
}
}

// Handler principal (para lambda y para local)
func WssHandler(args core.HandlerArgs) core.MainResponse {

fmt.Println("Respondiendo status 200 Connected")
return core.MainResponse{
LambdaResponse: &events.APIGatewayV2HTTPResponse{
StatusCode: 200,
Body: "Connected",
},
}
}

func OnPanic(panicMessage interface{}) {
core.Logx(5, "Error 500 (Panic): ", panicMessage)
core.Log(string(debug.Stack()))
Expand Down
2 changes: 1 addition & 1 deletion deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if [[ $ACCIONES == *"2"* ]]; then
cd ./cloud
go run . accion=1

echo "El deploy backend-node finalizado!"
echo "El deploy backend finalizado!"

fi

Expand Down

0 comments on commit dfc5db5

Please sign in to comment.