delta

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2022 License: MIT Imports: 13 Imported by: 0

README

δelta

Use golang http handler in AWS Lambda

usage

func helloHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Add("x-powered-by", "delta")
	fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/hello/", helloHandler)

	// start lambda handling if it runs on lambda
	// otherwise start http server on port 3000
	delta.ServeOrStartLambda(":3000", mux)
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure

func Configure(conf *Configuration)

Configure delta

func GetLambdaEvent

func GetLambdaEvent(ctx context.Context) (*events.APIGatewayProxyRequest, error)

GetLambdaEvent from context

func GetLambdaEventV2 added in v0.2.0

func GetLambdaEventV2(ctx context.Context) (*events.APIGatewayV2HTTPRequest, error)

GetLambdaEventV2 from context

func NewErrorResponse

func NewErrorResponse(err error) *events.APIGatewayProxyResponse

NewErrorResponse create API Gateway Proxy Response contains error message

func NewRequest

NewRequest create http.Request based on API Gateway Proxy request

func NewRequestV2 added in v0.2.0

NewRequestV2 create http.Request based on API Gateway Proxy request

func ServeOrStartLambda

func ServeOrStartLambda(addr string, h http.Handler) error

ServeOrStartLambda will start http server if it's not in lambda environment, otherwise it starts handling lambda

func SetBase64Encoding

func SetBase64Encoding(w http.ResponseWriter, enabled bool) error

SetBase64Encoding overrides base64 encoding for this response see Configuration.SetBase64Encoding

func Start

func Start(h http.Handler)

Start lambda server

Example: mux := http.NewServeMux() mux.Handle("/", handeHandler) delta.Start(nil, mux)

Types

type APIGWProxyHeader

type APIGWProxyHeader map[string]string

APIGWProxyHeader is format that used by API Gateway to store http header

type Configuration

type Configuration struct {
	EncodeResponse bool
}

Configuration customize delta behaviour

type Header struct {
	http.Header
}

Header wrap normal http.Header and add some converter

func HeaderFromAPIGWProxyHeader

func HeaderFromAPIGWProxyHeader(ph APIGWProxyHeader) *Header

HeaderFromAPIGWProxyHeader creates new Header from APIGWProxyHeader

func (*Header) ToAPIGWProxyHeader

func (h *Header) ToAPIGWProxyHeader() APIGWProxyHeader

ToAPIGWProxyHeader convert http.Header to LambdaHeader

type LambdaHandler

LambdaHandler func type for lambda.Start()

func CreateLambdaHandler

func CreateLambdaHandler(conf *Configuration, h http.Handler) LambdaHandler

CreateLambdaHandler create lambda handler

type LambdaHandlerV2 added in v0.2.0

LambdaHandlerV2 func type for lambda.Start()

func CreateLambdaHandlerV2 added in v0.2.0

func CreateLambdaHandlerV2(conf *Configuration, h http.Handler) LambdaHandlerV2

CreateLambdaHandlerV2 create lambda handler

type ResponseWriter

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

ResponseWriter implements http.ResponseWriter used for buffering response

func NewResponseWriter

func NewResponseWriter() *ResponseWriter

NewResponseWriter creates new empty ResponseWriter

func (*ResponseWriter) Header

func (r *ResponseWriter) Header() http.Header

Header returns http.Header. You can modify it to send response header

func (*ResponseWriter) ToAPIGWProxyResponse

func (r *ResponseWriter) ToAPIGWProxyResponse() *events.APIGatewayProxyResponse

ToAPIGWProxyResponse convert it to events.APIGatewayProxyResponse

func (*ResponseWriter) Write

func (r *ResponseWriter) Write(chunk []byte) (int, error)

Write appends chunk to response body

func (*ResponseWriter) WriteHeader

func (r *ResponseWriter) WriteHeader(statusCode int)

WriteHeader set status code of current request

Jump to

Keyboard shortcuts

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