ginlambda

package module
v0.1.0-pre1 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

ginlambda

A tiny adapter that enables Gin for Lambda

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

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