ginadapter

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

README

Gin adapter for Function URL

A Gin adapter for API Gateway V1 and V2 are already available from https://github.com/awslabs/aws-lambda-go-api-proxy. This module provides an adapter specifically for Function URL events with both BUFFERED (which, technically, is no different from API Gateway V2/HTTP events) and RESPONSE_STREAM mode which uses https://github.com/aws/aws-lambda-go/tree/main/lambdaurl.

package main

import (
	"github.com/gin-gonic/gin"
	sessions "github.com/nguyengg/go-aws-commons/gin-sessions-dynamodb"
	ginadapter "github.com/nguyengg/go-aws-commons/lambda/functionurl/gin"
	"github.com/nguyengg/go-aws-commons/lambda/functionurl/gin/rules"
)

type Session struct {
	SessionId string `dynamodbav:"sessionId" tableName:"session"`
	User      *User  `dynamodbav:"user,omitempty"`
}

type User struct {
	Sub    string   `dynamodbav:"sub"`
	Groups []string `dynamodbav:"groups,stringset"`
}

func main() {
	r := gin.Default()

	// this example uses github.com/nguyengg/go-aws-commons/gin-sessions-dynamodb to provide session management.
	r.GET("/",
		sessions.Sessions[Session]("sid"),
		ginadapter.RequireGroupMembership(func(c *gin.Context) (authenticated bool, groups rules.Groups) {
			var s *Session = sessions.Get[Session](c)
			if s.User == nil {
				return false, nil
			}
			return true, s.User.Groups
		}, rules.AllOf("a", "b"), rules.OneOf("b", "c")))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BufferedResponse

func BufferedResponse(ctx context.Context) *events.LambdaFunctionURLResponse

BufferedResponse returns the pending events.LambdaFunctionURLResponse from context.

If the gin handlers passed to StartBuffered or StartStream need access to the pending response, it can be retrieved from context with this method.

func Request

Request returns the original events.LambdaFunctionURLRequest from context.

If the gin handlers passed to StartBuffered or StartStream need access to the original invocation, it can be retrieved from context with this method.

func RequireGroupMembership added in v0.1.3

func RequireGroupMembership(fn func(*gin.Context) (authenticated bool, groups rules.Groups), rule rules.Rule, more ...rules.Rule) gin.HandlerFunc

RequireGroupMembership returns a gin middleware that aborts the request with either http.StatusUnauthorized or http.StatusForbidden depending on whether there exists a user with the current session whose group membership satisfies the given rules.

The middleware must be given a function that can retrieve the user's group from the current request. The argument fn returns whether the session is authenticated and the groups associated with the user. If session is not authenticated then the request is aborted with http.StatusUnauthorized. If the session is authenticated but the groups do not satisfy the rules, the request is aborted with http.StatusForbidden. Otherwise, the request goes through.

func StartBuffered

func StartBuffered(r *gin.Engine, options ...awslambda.Option)

StartBuffered starts the Lambda loop in BUFFERED mode with the given Gin engine.

func StartStream

func StartStream(r *gin.Engine, options ...lambda.Option)

StartStream starts the Lambda loop in STREAM_RESPONSE mode with the given Gin engine.

func StreamResponse

StreamResponse returns the pending events.LambdaFunctionURLStreamingResponse from context.

If the gin handlers passed to StartBuffered or StartStream need access to the pending response, it can be retrieved from context with this method.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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