ginlambda

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2021 License: MIT Imports: 9 Imported by: 0

README

ginlambda

A tiny adapter that enables Gin for Lambda

Quick Start

go get -u github.com/dgravesa/ginlambda

Set up your Gin routes as you normally would, and instead of calling lambda.Start() just pass your Gin engine instance to ginlambda.Start().

package main

import (
    "net/http"

    "github.com/dgravesa/ginlambda"
    "github.com/gin-gonic/gin"
)

var r *gin.Engine

func init() {
    r = gin.Default()
    r.GET("/greeting", func(c *gin.Context) {
        c.String(http.StatusOK, "Hello, World!")
    })
}

func main() {
    // equivalent to lambda.Start(handler)
    ginlambda.Start(r)
}

Also Check Out

AWS Labs Framework Adaptors - This is an AWS-supported package that does essentially the same job. Plus, it provides adaptors for a bunch of other frameworks as well.

There are two benefits to using ginlambda instead. First and foremost, ginlambda has a much smaller dependency tree, as it's only meant to support Gin. The AWS Labs module has indirect dependencies from a plethora of frameworks, so for example you're depending on Iris and Negroni and their dependencies even if you aren't using them.

Second, ginlambda can save you two lines of code by using ginlambda.Start(). This translates to roughly 200 bytes, saving you precious disk space.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Start

func Start(r *gin.Engine)

Start is analogous to lambda.Start() but takes a *gin.Engine argument instead of a handler function. The engine should have any desired routes initialized but should not be run.

Example
package main

import (
	"net/http"

	"github.com/dgravesa/ginlambda"
	"github.com/gin-gonic/gin"
)

var r *gin.Engine

func init() {
	r = gin.Default()
	r.GET("/greeting", func(c *gin.Context) {
		c.String(http.StatusOK, "Hello, World!")
	})
}

func main() {
	ginlambda.Start(r)
}
Output:

Types

type HandlerFunc

HandlerFunc is the signature for the Lambda handler function.

func NewHandler

func NewHandler(r *gin.Engine) HandlerFunc

NewHandler creates a new Lambda handler function from a *gin.Engine instance. This handler may be passed as the handler argument to lambda.Start().

Example
package main

import (
	"context"
	"fmt"
	"net/http"

	"github.com/aws/aws-lambda-go/events"
	"github.com/dgravesa/ginlambda"
	"github.com/gin-gonic/gin"
)

func main() {
	// initialize gin route
	r := gin.Default()
	r.GET("/greeting/:userName", func(c *gin.Context) {
		userName := c.Param("userName")
		c.String(http.StatusOK, "Hello, %s!", userName)
	})

	// test request
	request := events.APIGatewayProxyRequest{
		HTTPMethod: "GET",
		Path:       "/greeting/Bruce",
	}

	// construct lambda handler from gin engine
	handler := ginlambda.NewHandler(r)

	// execute request
	response, _ := handler(context.Background(), request)

	fmt.Println(response.StatusCode, response.Body)
}
Output:

200 Hello, Bruce!

Jump to

Keyboard shortcuts

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