podhmo / apikit

api toolkit (WIP)
MIT License
0 stars 0 forks source link

support lambda #147

Closed podhmo closed 2 years ago

podhmo commented 2 years ago

https://docs.aws.amazon.com/lambda/latest/dg/golang-handler.html

148, lambda handler is also not having path and query-string, so another handling way for the argument that treated as primitive and primitive-pointer is needed. this is same in the situation using cli.

memo

podhmo commented 2 years ago
type DoInput struct{}

func Do(logger *log.Logger, data DoInput, debug *bool, targetID string) (interface{}, error) {
    return nil, nil
}

generator code

c := g.Generate(pkg, action.Do, WithRenameField("targetID", "targetId"))

// generate main()
g.IncludePlugin(pkg, emitmain.Options{HandlerCode: c, GetProvider: "<code ?>"})

// generate init() and global variables (for state)

generated code

type DoEvent struct {
    Data     DoInput `json:"data"`
    Debug    *bool   `json:"debug"`
    TargetID string  `json:"targetId"` // rename to targetId ? (in json)
}

type Provider interface {
    Logger(ctx context.Context) (*log.Logger, error)
}

func DoHandler(getProvider func(ctx context.Context) (Provider, error)) func(ctx context.Context, event DoEvent) (interface{}, error) {
    return func(ctx context.Context, event DoEvent) (interface{}, error) {
        provider, err := getProvider(ctx)
        if err != nil {
            return nil, err // custom error handling?
        }
        var logger *log.Logger
        {
            var err error
            logger, err = provider.Logger(ctx)
            if err != nil {
                return nil, err
            }
        }
        return Do(logger, event.Data, event.Debug, event.TargetID)
    }
}

func main() {
    lambda.Start(DoHandler(getProvider))
}
podhmo commented 2 years ago

If you want to get terraform code?, generate it, as you like (For example, create terraform module)

podhmo commented 2 years ago

when this feature is convenient?

podhmo commented 2 years ago
podhmo commented 2 years ago

if func -> struct transformation automatically, cannot handle validation?

or something like below

g.Generate(pkg, action.Do, WithTag("targetId", `validate: regexp="^[a-f0-9]{24}"`))

or

type ObjectID string
func (id *ObjectId) TagString() string { return `<tag>` }
podhmo commented 2 years ago

api gateway

podhmo commented 2 years ago

integration

podhmo commented 2 years ago

https://github.com/seanrmurphy/lambda-openapi3-test

podhmo commented 2 years ago

https://github.com/awslabs/aws-lambda-go-api-proxy