gatewayapi

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2020 License: AGPL-3.0, Apache-2.0 Imports: 20 Imported by: 0

README

genericapi

Provides common logic for Lambda functions which serve as a Lambda-proxy backend to API Gateway:

  • LambdaProxy to generate the main Lambda handler
  • GatewayClient for building an HTTP client that can sign requests for AWS_IAM authentication
  • MarshalResponse for serializing an API response model
    • ReplaceMapSliceNils for recursively replacing nil slices and maps with initialized versions

Example API Handler

package main

import (
	"context"

	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"
	"github.com/panther-labs/panther/pkg/gatewayapi"
)

var methodHandlers = map[string]gatewayapi.RequestHandler {
	"GET /orgs/{orgId}": getOrganization,
}

func getOrganization(request *events.APIGatewayProxyRequest) *events.APIGatewayProxyResponse {
	// The request contains the http method, path, path parameters, query parameters, body, etc.
	orgId := models.OrgID(request.PathParameters["orgId"])
	sortAscending := request.QueryStringParameters["asc"]

	// models is the auto-generated package from swagger
	result := &models.ListOrganizationsResponse{}
	return gatewayapi.MarshalResponse(result)
}

func main() {
	lambda.Start(gatewayapi.LambdaProxy(methodHandlers))
}

Example Invocation

package main

import (
	"github.com/aws/aws-sdk-go/aws/session"

	"github.com/panther-labs/panther/pkg/gatewayapi"
)

var (
	awsSession = session.Must(session.NewSession())
	httpClient = gatewayapi.GatewayClient(awsSession)
)

func main() {
	// client is the auto-generated package from swagger
	config := client.DefaultTransportConfig().
		WithBasePath("/v1").
		WithHost("l4ekvgdy92.execute-api.us-west-2.amazonaws.com")  // replace with your endpoint
	apiclient := client.NewHTTPClientWithConfig(nil, config)

	result, err := apiclient.Operations.ListOrganizations(
		&operations.AddResourceParams{
			// ...
			HTTPClient: httpClient,
		})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GatewayClient

func GatewayClient(s *session.Session) *http.Client

GatewayClient generates an http client that can invoke API Gateway endpoints with AWS_IAM authentication.

func LambdaProxy

LambdaProxy generates a handler function for API Gateway lambda-proxy backends.

Note: The returned error is always nil. All errors should be reported in the status code of the response.

func MarshalResponse

func MarshalResponse(response interface{}, statusCode int) *events.APIGatewayProxyResponse

MarshalResponse replaces nil maps + slices and serializes a response model.

response is a pointer to a struct and statusCode is the http status to return

func ReplaceMapSliceNils

func ReplaceMapSliceNils(val interface{})

ReplaceMapSliceNils replaces nil slices and maps with initialized versions.

For example, struct{Tags []string} would serialize as "tags: []" instead of "tags: null" The input must be a pointer to a struct.

Types

type RequestHandler

RequestHandler is a function which handles an HTTP request for a single method/resource pair.

Jump to

Keyboard shortcuts

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