gateway icon indicating copy to clipboard operation
gateway copied to clipboard

Toggle to fall back to http.ListenAndServe

Open mozey opened this issue 7 years ago • 3 comments

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?

mozey avatar Jun 01 '18 11:06 mozey

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.

piotrkubisa avatar Jun 01 '18 14:06 piotrkubisa

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 avatar Sep 30 '20 07:09 mohemohe

@mohemohe thanks for the env suggestion. It is useful to know.

ilgityildirim avatar Oct 15 '20 10:10 ilgityildirim