golamb

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2022 License: MIT Imports: 11 Imported by: 0

README

Golamb

Go Reference codecov

Golamb makes it easier to write AWS Lambda functions in Go that are invoked by API Gateway Http APIs.

Documentation

For full documentation see pkg.go.dev.

Usage

Basic
package main

import (
	"net/http"

	"github.com/aws/aws-sdk-go/service/dynamodb"
	"github.com/twharmon/golamb"
)

func handler(c golamb.Context) golamb.Responder {
	// Get a query parameter
	foo := c.Request().Query("foo")

	// Get a path parameter
	bar := c.Request().Path("bar")

	return c.Response(http.StatusOK, output.Item)
}

func main() {
	golamb.Start(handler)
}

Contribute

Make a pull request.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Start

func Start(handlerFunc Handler, config ...*Config)

Start takes in a Handler function. This is similar to lambda.StartHandler. An optional Config can be passed in.

Types

type Config

type Config struct {
	PanicHandler func(c Context, err error) Responder
	LogLevel     LogLevel
	Logger       Logger
}

Config provides service configuration for service clients.

type Context

type Context interface {
	// Request returns the http request.
	Request() Request

	// Response returns an http response with the given status code.
	// An options response body can be provided as the second
	// argument.
	Response(status int, body ...interface{}) Responder

	// LogDebug logs the given message. Arguments are handled in the
	// manner of fmt.Printf.
	LogDebug(message string, args ...interface{})

	// LogInfo logs the given message. Arguments are handled in the
	// manner of fmt.Printf.
	LogInfo(message string, args ...interface{})

	// LogNotice logs the given message. Arguments are handled in the
	// manner of fmt.Printf.
	LogNotice(message string, args ...interface{})

	// LogWarning logs the given message. Arguments are handled in
	// the manner of fmt.Printf.
	LogWarning(message string, args ...interface{})

	// LogError logs the given message. Arguments are handled in the
	// manner of fmt.Printf.
	LogError(message string, args ...interface{})

	// LogCritical logs the given message. Arguments are handled in
	// the manner of fmt.Printf.
	LogCritical(message string, args ...interface{})

	// LogAlert logs the given message. Arguments are handled in the
	// manner of fmt.Printf.
	LogAlert(message string, args ...interface{})

	// LogEmergency logs the given message. Arguments are handled in
	// the manner of fmt.Printf.
	LogEmergency(message string, args ...interface{})
}

Context is the central piece of golamb. It allows one to access a requests query parameters, path parameters, body, headers, and cookies. Messages can be logged to the provided logger according to the provided log level.

type DefaultLogger added in v0.1.5

type DefaultLogger struct {
	// contains filtered or unexported fields
}

DefaultLogger logs messages to os.Stdout, which is sent to CloudWatch logs.

func (*DefaultLogger) Log added in v0.1.5

func (l *DefaultLogger) Log(level LogLevel, message string)

Log implements the Logger interface.

type Handler added in v0.1.5

type Handler func(c Context) Responder

Handler is invoked when API Gateway invokes your lambda.

type LogLevel added in v0.1.5

type LogLevel int

LogLevel as defined in the RFC 5424 specification.

const (
	// LogLevelDebug as defined in the RFC 5424 specification.
	LogLevelDebug LogLevel = 0

	// LogLevelInfo as defined in the RFC 5424 specification.
	LogLevelInfo LogLevel = 1

	// LogLevelNotice as defined in the RFC 5424 specification.
	LogLevelNotice LogLevel = 2

	// LogLevelWarning as defined in the RFC 5424 specification.
	LogLevelWarning LogLevel = 3

	// LogLevelError as defined in the RFC 5424 specification.
	LogLevelError LogLevel = 4

	// LogLevelCritical as defined in the RFC 5424 specification.
	LogLevelCritical LogLevel = 5

	// LogLevelAlert as defined in the RFC 5424 specification.
	LogLevelAlert LogLevel = 6

	// LogLevelEmergency as defined in the RFC 5424 specification.
	LogLevelEmergency LogLevel = 7

	// LogLevelSilent logs nothing.
	LogLevelSilent LogLevel = math.MaxInt
)

func (LogLevel) String added in v0.1.5

func (l LogLevel) String() string

type Logger added in v0.1.5

type Logger interface {
	Log(level LogLevel, message string)
}

Logger is used to log messages.

func NewDefaultLogger added in v0.1.5

func NewDefaultLogger() Logger

NewDefaultLogger returns the default logger.

type Request

type Request interface {
	// Query returns the query parameter with the given key.
	Query(key string) string

	// Path returns the path parameter with the given key.
	Path(key string) string

	// Body parses the JSON-encoded data and stores the result in the
	// value pointed to by v.
	Body(v interface{}) error

	// Cookie returns the cookie with the given name.
	Cookie(name string) *http.Cookie

	// Header returns the header value with the given key.
	Header(key string) string

	// Headers returns a map of all the headers.
	Headers() map[string]string

	// RawPath returns the raw path.
	RawPath() string
}

Request holds the http request data.

type Responder

type Responder interface {
	// Respond responds to the http request.
	Respond() (*events.APIGatewayV2HTTPResponse, error)

	// SetHeader sets a response header with the given key and value.
	SetHeader(key string, value string) Responder

	// SetCookie sets a response cookie with the given key and value.
	SetCookie(cookie *http.Cookie) Responder
}

Responder responds to the request.

Directories

Path Synopsis
Package fakes provides a way to fake or stub Context, Request, Response, and AWSServiceProvider.
Package fakes provides a way to fake or stub Context, Request, Response, and AWSServiceProvider.

Jump to

Keyboard shortcuts

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