Documentation
¶
Overview ¶
Package awswebsocketadapter can be used to run an AWS Lambda function integration for a websocket-type API Gateway, locally.
It implements http.Handler to handle websocket connections and invokes the provided AWS Lambda handler function for any CONNECT, DISCONNECT or MESSAGE events.
It also implements the apigatewaymanagementapiiface.ApiGatewayManagementApiAPI client interface, so it can be used to write messages back to a client.
Example ¶
package main import ( "context" "log" "net/http" "github.com/aws/aws-lambda-go/events" "github.com/aws/aws-sdk-go/service/apigatewaymanagementapi" "github.com/aws/aws-sdk-go/service/apigatewaymanagementapi/apigatewaymanagementapiiface" awswebsocketadapter "github.com/armsnyder/aws-websocket-adapter" ) func main() { // Create the adapter. var adapter awswebsocketadapter.Adapter // Set the LambdaHandler to a real lambda handler function. adapter.LambdaHandler = createHandler(&adapter) // Listen for ws:// requests. err := http.ListenAndServe(":8080", &adapter) log.Fatal(err) } // createHandler returns a handler function that can be passed to lambda.Start, // from the aws-lambda-go SDK. func createHandler(client apigatewaymanagementapiiface.ApiGatewayManagementApiAPI) func(context.Context, events.APIGatewayWebsocketProxyRequest) (events.APIGatewayProxyResponse, error) { return func(ctx context.Context, request events.APIGatewayWebsocketProxyRequest) (resp events.APIGatewayProxyResponse, err error) { resp = events.APIGatewayProxyResponse{StatusCode: 200} // As an example, send a message in response to every message received. // This demonstrates how Adapter can be used as an API Gateway Management API client. if request.RequestContext.EventType == "MESSAGE" { reply := &apigatewaymanagementapi.PostToConnectionInput{ ConnectionId: &request.RequestContext.ConnectionID, Data: []byte("hello"), } _, err = client.PostToConnectionWithContext(ctx, reply) } return resp, err } }
Output:
Index ¶
- type Adapter
- func (a *Adapter) DeleteConnection(_ *apigatewaymanagementapi.DeleteConnectionInput) (*apigatewaymanagementapi.DeleteConnectionOutput, error)
- func (a *Adapter) DeleteConnectionRequest(_ *apigatewaymanagementapi.DeleteConnectionInput) (*request.Request, *apigatewaymanagementapi.DeleteConnectionOutput)
- func (a *Adapter) DeleteConnectionWithContext(_ aws.Context, _ *apigatewaymanagementapi.DeleteConnectionInput, ...) (*apigatewaymanagementapi.DeleteConnectionOutput, error)
- func (a *Adapter) GetConnection(_ *apigatewaymanagementapi.GetConnectionInput) (*apigatewaymanagementapi.GetConnectionOutput, error)
- func (a *Adapter) GetConnectionRequest(_ *apigatewaymanagementapi.GetConnectionInput) (*request.Request, *apigatewaymanagementapi.GetConnectionOutput)
- func (a *Adapter) GetConnectionWithContext(_ aws.Context, _ *apigatewaymanagementapi.GetConnectionInput, ...) (*apigatewaymanagementapi.GetConnectionOutput, error)
- func (a *Adapter) PostToConnection(input *apigatewaymanagementapi.PostToConnectionInput) (*apigatewaymanagementapi.PostToConnectionOutput, error)
- func (a *Adapter) PostToConnectionRequest(_ *apigatewaymanagementapi.PostToConnectionInput) (*request.Request, *apigatewaymanagementapi.PostToConnectionOutput)
- func (a *Adapter) PostToConnectionWithContext(_ aws.Context, input *apigatewaymanagementapi.PostToConnectionInput, ...) (*apigatewaymanagementapi.PostToConnectionOutput, error)
- func (a *Adapter) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type LambdaHandler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
type Adapter struct { LambdaHandler LambdaHandler // contains filtered or unexported fields }
Adapter is an implementation of an API Gateway Websocket API that invokes an AWS Lambda function in-memory. It is a handler that upgrades requests to websockets and invokes an AWS Lambda handler on each message. It also provides API Gateway Management APIs for writing back to connections.
func (*Adapter) DeleteConnection ¶
func (a *Adapter) DeleteConnection(_ *apigatewaymanagementapi.DeleteConnectionInput) (*apigatewaymanagementapi.DeleteConnectionOutput, error)
func (*Adapter) DeleteConnectionRequest ¶
func (a *Adapter) DeleteConnectionRequest(_ *apigatewaymanagementapi.DeleteConnectionInput) (*request.Request, *apigatewaymanagementapi.DeleteConnectionOutput)
func (*Adapter) DeleteConnectionWithContext ¶
func (a *Adapter) DeleteConnectionWithContext(_ aws.Context, _ *apigatewaymanagementapi.DeleteConnectionInput, _ ...request.Option) (*apigatewaymanagementapi.DeleteConnectionOutput, error)
func (*Adapter) GetConnection ¶
func (a *Adapter) GetConnection(_ *apigatewaymanagementapi.GetConnectionInput) (*apigatewaymanagementapi.GetConnectionOutput, error)
func (*Adapter) GetConnectionRequest ¶
func (a *Adapter) GetConnectionRequest(_ *apigatewaymanagementapi.GetConnectionInput) (*request.Request, *apigatewaymanagementapi.GetConnectionOutput)
func (*Adapter) GetConnectionWithContext ¶
func (a *Adapter) GetConnectionWithContext(_ aws.Context, _ *apigatewaymanagementapi.GetConnectionInput, _ ...request.Option) (*apigatewaymanagementapi.GetConnectionOutput, error)
func (*Adapter) PostToConnection ¶
func (a *Adapter) PostToConnection(input *apigatewaymanagementapi.PostToConnectionInput) (*apigatewaymanagementapi.PostToConnectionOutput, error)
func (*Adapter) PostToConnectionRequest ¶
func (a *Adapter) PostToConnectionRequest(_ *apigatewaymanagementapi.PostToConnectionInput) (*request.Request, *apigatewaymanagementapi.PostToConnectionOutput)
func (*Adapter) PostToConnectionWithContext ¶
func (a *Adapter) PostToConnectionWithContext(_ aws.Context, input *apigatewaymanagementapi.PostToConnectionInput, _ ...request.Option) (*apigatewaymanagementapi.PostToConnectionOutput, error)
type LambdaHandler ¶
type LambdaHandler func(context.Context, events.APIGatewayWebsocketProxyRequest) (events.APIGatewayProxyResponse, error)