gateway
gateway copied to clipboard
Toggle to fall back to http.ListenAndServe
Using sam local start-api is great but
- it's slow handling requests
- it would be nice to have the option of running code stand alone outside Lambda
I'd like to have a toggle to fall back to net/http only:
ApexGatewayDisabled := os.Getenv("APEX_GATEWAY_DISABLED")
if ApexGatewayDisabled == "true" {
log.Fatal(http.ListenAndServe(":3000", nil))
} else {
log.Fatal(gateway.ListenAndServe(":3000", nil))
}
Everywhere AWS specific functionality is used above condition must also be checked.
Does this approach make sense or is there a better way?
Why don't you use separate binaries - one for serverless (apex) and second for serverfull (http)? More code in handler = larger binary = more money spent on uploading artifact = (in many cases) worse performance = more money spent on Lambda runtime (yeah, I know free tier). I highly advocate for separating logic, especially in Lambda handlers.
you can use AWS_LAMBDA_FUNCTION_NAME envronment variable.
if os.Getenv("AWS_LAMBDA_FUNCTION_NAME") == "" {
log.Fatal(http.ListenAndServe(":3000", nil))
} else {
log.Fatal(gateway.ListenAndServe(":3000", nil))
}
@mohemohe thanks for the env suggestion. It is useful to know.