Documentation
¶
Overview ¶
Package apigatewayproxy provides a way to process AWS API Gateway Proxy requests using a standard HTTP handler. This makes it simple to build a program that operates as a HTTP server when run normally, and runs as an AWS lambda when running in an AWS lambda container.
Example ¶
package main
import (
"net/http"
"github.com/jjeffery/apigatewayproxy"
)
func main() {
// define a simple HTTP handler
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world\n"))
})
if apigatewayproxy.IsLambda() {
// this process is running in an AWS Lambda container
apigatewayproxy.Start(h)
} else {
// run as a conventional HTTP server
http.ListenAndServe(":8080", h)
}
}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // RequestReceived is called when a request is received from Lambda. Useful for logging. // The default implementation does nothing. RequestReceived func(request *events.APIGatewayProxyRequest) // SendingResponse is called just prior to returning the response to Lambda. Useful for logging. // The default implementation does nothing. SendingResponse func(request *events.APIGatewayProxyRequest, response *events.APIGatewayProxyResponse) // ShouldEncodeBody is called to determine if the body should be base64-encoded. // The default implementation returns true if the response has a Content-Encoding header, // or if body contains bytes outside the range [0x09, 0x7f]. ShouldEncodeBody func(response *events.APIGatewayProxyResponse, body []byte) bool )
Callback functions that can be overridden.
Functions ¶
func IsLambda ¶
func IsLambda() bool
IsLambda returns true if the current process is operating in an AWS Lambda container. It determines this by checking for the presence of the "_LAMBDA_SERVER_PORT" environment variable.
func Request ¶
func Request(ctx context.Context) *events.APIGatewayProxyRequest
Request returns a pointer to the API Gateway proxy request, or nil if the current context is not associated with an API Gateway proxy lambda.
Example ¶
package main
import (
"fmt"
"net/http"
"github.com/jjeffery/apigatewayproxy"
)
func main() {
// define a HTTP handler that knows whether it is
// running as an AWS lambda
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
pr := apigatewayproxy.Request(r.Context())
var msg string
if pr == nil {
msg = "I am not running in an AWS Lambda container"
} else {
msg = fmt.Sprintf("I am running in an AWS Lambda under account %s", pr.RequestContext.AccountID)
}
w.Write([]byte(msg))
})
if apigatewayproxy.IsLambda() {
// this process is running in an AWS Lambda container
apigatewayproxy.Start(h)
} else {
// run as a conventional HTTP server
http.ListenAndServe(":8080", h)
}
}
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.