engine

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 4 Imported by: 0

README

engine

Decoupled and generic lambda event manager that helps to build lambda functions in a more structured way.

Installation

go get github.com/Drafteame/engine

Usage

Plain lambda
package main

import (
	"context"
	"fmt"
	
	"github.com/Drafteame/engine"
	"github.com/Drafteame/engine/decorators"
)

type Request struct {
	Name string `json:"name"`
}

type Response struct {
	Message string `json:"message"`
}

func handler(ctx context.Context, req Request) (Response, error) {
	fmt.Println("Hello, world!")
	return Response{Message: "Hello, " + req.Name + "!"}, nil
}

func main() {
	engine.New(handler).
		Use(decorators.PanicRecover[Request, Response]()).
		Run()
}
SQS lambda
package main

import (
    "context"
    "fmt"
	
    "github.com/Drafteame/engine"
    "github.com/Drafteame/engine/decorators"
	
    "github.com/aws/aws-lambda-go/events"
)

func handler(ctx context.Context, event events.SQSEvent) (events.SQSEventResponse, error) {
    for _, record := range event.Records {
        fmt.Println(record.Body)
    }
    return events.SQSEventResponse{}, nil
}

func main() {
    engine.New(handler).
        Use(decorators.PanicRecover[events.SQSEvent, events.SQSEventResponse]()).
        Run()
}
API gateway V1 lambda
package main

import (
	"fmt"
	"net/http"
	
	"github.com/Drafteame/engine"
	"github.com/Drafteame/engine/decorators"
	"github.com/Drafteame/engine/handlers/apigatewayv1"
)

var s *http.ServerMux

func init() {
	s = http.NewServeMux()
	s.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Hello, world!")
	})
}

func main() {
	engine.New(apigatewayv1.NewHandler(s)).
		Use(decorators.PanicRecover[apigatewayv1.HTTPRequest, apigatewayv1.HTTPResponse]()).
		Run()
}
API gateway V2 lambda
package main

import (
	"fmt"
	"net/http"
    
    "github.com/Drafteame/engine"
    "github.com/Drafteame/engine/decorators"
    "github.com/Drafteame/engine/handlers/apigatewayv2"
)

var s *http.ServerMux

func init() {
	s = http.NewServeMux()
	s.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Hello, world!")
	})
}

func main() {
    engine.New(apigatewayv2.NewHandler(s)).
        Use(decorators.PanicRecover[apigatewayv2.HTTPRequest, apigatewayv2.HTTPResponse]()).
        Run()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decorator

type Decorator[T, R any] func(handler Handler[T, R]) Handler[T, R]

Decorator is a function that receives a handler and returns a new handler wrapping the original one to add functionality.

type Engine

type Engine[T, R any] struct {
	// contains filtered or unexported fields
}

Engine is a struct that holds the handler and the decorators to be applied to the handler.

func New

func New[T, R any](handler Handler[T, R]) *Engine[T, R]

New creates a new Engine with the given handler.

func (*Engine[T, R]) Run

func (e *Engine[T, R]) Run()

Run starts the engine.

func (*Engine[T, R]) Use

func (e *Engine[T, R]) Use(decorators ...Decorator[T, R]) *Engine[T, R]

Use adds decorators to the engine.

type Handler

type Handler[T, R any] func(context.Context, T) (R, error)

Handler is a function that is complaint to with the golang aws-lambda-go sdk. Where "T" and "R" are types compatible with the "encoding/json" standard library. See https://golang.org/pkg/encoding/json/#Unmarshal for how deserialization behaves

Directories

Path Synopsis
handlers
internal
test

Jump to

Keyboard shortcuts

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