Last active
December 22, 2021 14:44
-
-
Save Jimeux/11155ed5e4da7e3f71eac28b7c546ada to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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