apigatewayhttpapi

package
v0.0.0-...-3a9ac50 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 23 Imported by: 0

README

API Gateway HTTP Integration

Modules around API Gateway have fallen behind in terms of bug fixes and new features in comparison to Lambda Function URL since I've mostly migrated completely away from API Gateway.

The only redeeming feature of API Gateway at the moment is the authorisers and their ability to cache based on a header or query parameter identity source. Since I've moved to cookie-based authenication and authorisation, however, the point is moot.

package main

import (
	"context"

	"github.com/aws/aws-lambda-go/events"
	"github.com/nguyengg/golambda"
	"github.com/nguyengg/golambda/apigatewayhttpapi"
	"github.com/nguyengg/golambda/apigatewayhttpapi/auth"
	"github.com/nguyengg/golambda/apigatewayhttpapi/framework"
)

func main() {
	// without a context wrapper.
	apigatewayhttpapi.Start(func(ctx context.Context, request events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
		// will set LambdaContext.AwsRequestID to log prefix and reset upon completion.
		defer logsupport.SetUpGlobalLogger(ctx)()

		return events.APIGatewayV2HTTPResponse{
			StatusCode: 200,
			Body:       "hello, world!",
		}, nil
	})

	// with a context wrapper.
	framework.Start(func(c *framework.Context) error {
		return c.RespondOKWithText("hello, world!")
	})

	// authorizer example.
	auth.StartV2(func(ctx context.Context, request events.APIGatewayV2CustomAuthorizerV2Request) (events.APIGatewayV2CustomAuthorizerSimpleResponse, error) {
		return events.APIGatewayV2CustomAuthorizerSimpleResponse{
			IsAuthorized: true,
		}, nil
	})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheControlMaxAgeValue

func CacheControlMaxAgeValue(duration time.Duration) string

func Error

func Error(statusCode int, opts ...Opt) events.APIGatewayV2HTTPResponse

func ErrorWithMessage

func ErrorWithMessage(statusCode int, message string, opts ...Opt) events.APIGatewayV2HTTPResponse

func Errorf

func Errorf(statusCode int, layout string, v ...interface{}) events.APIGatewayV2HTTPResponse

func JSONError

func JSONError(statusCode int, opts ...Opt) events.APIGatewayV2HTTPResponse

func JSONErrorWithMessage

func JSONErrorWithMessage(statusCode int, message string, opts ...Opt) events.APIGatewayV2HTTPResponse

func JSONErrorf

func JSONErrorf(statusCode int, layout string, v ...interface{}) events.APIGatewayV2HTTPResponse

func ProxyS3

func ProxyS3(ctx context.Context, client *s3.Client, method, bucket, key string, opts ...Opt) (events.APIGatewayV2HTTPResponse, error)

ProxyS3 will call S3 with the appropriate GET or HEAD method and return the response as either plain text or base64-encoded data.

The argument method will determine whether ProxyS3GET or ProxyS3HEAD will be used. If the method is not recognized, http.StatusMethodNotAllowed will be returned.

func ProxyS3GET

func ProxyS3GET(ctx context.Context, client *s3.Client, bucket, key string, opts ...Opt) (events.APIGatewayV2HTTPResponse, error)

ProxyS3GET is ProxyS3 for http.MethodGet and will call S3's GetObject.

func ProxyS3GETWithRequestHeaders

func ProxyS3GETWithRequestHeaders(ctx context.Context, client *s3.Client, bucket, key string, header http.Header, opts ...Opt) (events.APIGatewayV2HTTPResponse, error)

ProxyS3GETWithRequestHeaders is a variant of ProxyS3GET with request headers.

Only these headers are proxied: If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, and Range.

func ProxyS3HEAD

func ProxyS3HEAD(ctx context.Context, client *s3.Client, bucket, key string, opts ...Opt) (events.APIGatewayV2HTTPResponse, error)

ProxyS3HEAD is ProxyS3 for http.MethodHead and will call S3's HeadObject.

func ProxyS3HEADWithRequestHeaders

func ProxyS3HEADWithRequestHeaders(ctx context.Context, client *s3.Client, bucket, key string, header http.Header, opts ...Opt) (events.APIGatewayV2HTTPResponse, error)

ProxyS3HEADWithRequestHeaders is a variant of ProxyS3HEAD with request headers.

Only these headers are proxied: If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, and Range.

func ProxyS3WithRequestHeaders

func ProxyS3WithRequestHeaders(ctx context.Context, client *s3.Client, method, bucket, key string, header http.Header, opts ...Opt) (events.APIGatewayV2HTTPResponse, error)

ProxyS3WithRequestHeaders is a variant of ProxyS3 that is given an extra http.Header whose values will be passed into the S3's respective requests if the action supports it.

func Start

func Start(handler Handler, options ...start.Option)

Start starts the Lambda runtime loop with the specified Handler.

Types

type Handler

Handler for API Gateway HTTP API requests using V2 payload request and response format.

type Opt

Modifiers to change the response such as adding headers thereto.

func SetCacheControlMaxAge

func SetCacheControlMaxAge(duration time.Duration) Opt

Sets the cache control header with the specified max age duration.

func SetHeader

func SetHeader(key, value string) Opt

Sets header to the specified value only if both key and value are non-empty strings.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL