swift-server / swift-aws-lambda-runtime

Swift implementation of AWS Lambda Runtime
Apache License 2.0
1.14k stars 102 forks source link

Allow custom initialisation of the HandlerType of the LambdaRuntime. #308

Closed tachyonics closed 10 months ago

tachyonics commented 1 year ago

Allow custom initialisation of the HandlerType of the LambdaRuntime.

Motivation:

Provide the flexibility for custom initialisation of the HandlerType as this will often be required by higher level frameworks.

Modifications:

Modify the LambdaRuntime type to accept a closure to provide the handler rather than requiring that it is provided by a static method on the Handler type. This is achieved by a new base protocol NonFactoryByteBufferLambdaHandler that does not have the makeHandler function requirement.

Also provide a new base protocol NonFactoryLambdaHandler that doesn't have the initialisation requirements of either LambdaHandler or SimpleLambdaHandler. This will allow higher level frameworks to initialise their own handlers that however can take advantage of the Codable conveniences of this package.

@Sendable
func handlerProvider(context: LambdaInitializationContext) 
-> EventLoopFuture<some NonFactoryByteBufferLambdaHandler> {
    let underlyingHandler = <initialise a type conforming to NonFactoryLambdaHandler>
    let codableHandler = underlyingHandler.withWrappingCodableHandler(allocator: context.allocator)

    return context.eventLoop.makeSuceededFuture(codableHandler)
}

Result:

Existing high level use cases (LambdaHandler and SimpleLambdaHandler) will continue to be provided but a lower level integration will be possible aimed at higher level frameworks.

tomerd commented 11 months ago

thanks for putting together @tachyonics, I finally had time to review and came up with a slightly more minimal change in https://github.com/swift-server/swift-aws-lambda-runtime/pull/310. If the minimalistic approach is good enough for your use case we can update #308 to that end and close #310 or vice versa.

tachyonics commented 10 months ago

Closing as this has been replaced by #310