Skip to content

Instantly share code, notes, and snippets.

@Jimeux
Last active December 22, 2021 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jimeux/11155ed5e4da7e3f71eac28b7c546ada to your computer and use it in GitHub Desktop.
Save Jimeux/11155ed5e4da7e3f71eac28b7c546ada to your computer and use it in GitHub Desktop.
// Package-level vars survive for the life of the
// Lambda, and help avoid unnecessary allocations.
var (
logger *ws.Logger
svc *ws.WebSocketService
)
// main initialises package-level vars and calls lambda.Start, passing
// handler, which is wrapped in middleware that initialises logging.
func main() {
logger = ws.NewLogger()
cf := ws.NewConfig()
cfg, _ := config.LoadDefaultConfig(context.Background())
repository := ws.NewRepositoryFromConfig(cfg, cf.ConnectionsTable)
client := ws.NewAPIClientFromConfig(cfg, cf.Stage, cf.APIGatewayDomain)
svc = ws.NewWebSocketService(client, repository)
lambda.Start(ws.Middleware(logger, handler))
}
// handler delegates to WebSocketService to avoid implementing core logic itself.
func handler(ctx context.Context, event *events.APIGatewayWebsocketProxyRequest) (ws.Response, error) {
res, err := svc.Connect(ctx, event.RequestContext.ConnectionID)
if err != nil {
logger.Error(ctx, "connect failure", err)
return res, err
}
logger.Info(ctx, "connect success")
return res, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment