Code Monkey home page Code Monkey logo

engine's Introduction

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()
}

engine's People

Contributors

danteay avatar draftea-bot avatar

Stargazers

 avatar

Watchers

Joe Cohen avatar Ian Butelmann avatar Gabriel Browarnik avatar Ignacio Daniel Alvarez avatar Agustin de Sautu Riestra avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.