lambda

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

README

caddy-lambda

Event-Based Function Execution (Lambda) Plugin for Caddy v2.

Table of Contents

Overview

The caddy-lambda triggers execution of a function when it is invoked. It is a terminal plugin, i.e. the plugin writes response headers and body.

Getting Started

The Caddyfile config follows:

localhost {
	route /api/* {
		lambda {
			name hello_world
			runtime python
			python_executable {$HOME}/path/to/venv/bin/python
			entrypoint assets/scripts/api/hello_world/app/index.py
			function handler
		}
	}
	route {
		respond "OK"
	}
}

The assets/scripts/api/hello_world/app/index.py follows:

import json

def handler(event: dict) -> dict:
    print(f"event: {event}")
    response = {
        "body": json.dumps({"message": "hello world!"}),
        "status_code": 200,
    }
    return response

The response dictionary is mandatory for a handler. he status_code and body are mandatory fields of the response. The plugin writes status_code and body back to the requestor.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FunctionExecutor

type FunctionExecutor struct {
	// Name stores the name associated with the function.
	Name string `json:"name,omitempty"`
	// Runtime stores the runtime of the function, e.g. python.
	Runtime string `json:"runtime,omitempty"`
	// EntrypointPath stores the path to the function's entrypoint, e.g. python script path.
	EntrypointPath string `json:"entrypoint_path,omitempty"`
	// EntrypointHandler stores the name of the function to invoke at the Entrypoint. e.g handler.
	EntrypointHandler string `json:"entrypoint_handler,omitempty"`
	// PythonExecutable stores the path to the python executable.
	PythonExecutable string `json:"python_executable,omitempty"`
	// MaxWorkersCount stores the max number of concurrent runtimes.
	MaxWorkersCount uint `json:"workers,omitempty"`
	// WorkerTimeout stores the maximum number of seconds a function would run.
	WorkerTimeout int `json:"worker_timeout,omitempty"`
	// If URIFilter is not empty, then only the plugin
	// intercepts only the pages matching the regular expression
	// in the filter
	URIFilter string `json:"uri_filter,omitempty"`
	// contains filtered or unexported fields
}

FunctionExecutor is a middleware which triggers execution of a function when it is invoked.

func (FunctionExecutor) CaddyModule

func (FunctionExecutor) CaddyModule() caddy.ModuleInfo

CaddyModule returns the Caddy module information.

func (*FunctionExecutor) Cleanup

func (fex *FunctionExecutor) Cleanup() error

Cleanup implements caddy.CleanerUpper and terminates running processes.

func (*FunctionExecutor) Provision

func (fex *FunctionExecutor) Provision(ctx caddy.Context) error

Provision sets up FunctionExecutor.

func (FunctionExecutor) ServeHTTP

func (fex FunctionExecutor) ServeHTTP(resp http.ResponseWriter, req *http.Request, next caddyhttp.Handler) error

func (*FunctionExecutor) UnmarshalCaddyfile

func (fex *FunctionExecutor) UnmarshalCaddyfile(d *caddyfile.Dispenser) error

UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax:

	lambda [<matcher>] {
     name <name>
     runtime <name>
     entrypoint <path>
     function <name>
	}

Jump to

Keyboard shortcuts

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