Skip to content

Commit

Permalink
Support launching a HBMixedLambdaApplication as either HTTP or Lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
Joannis committed Feb 5, 2024
1 parent c7dc7a8 commit 0e60d57
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 5 deletions.
25 changes: 23 additions & 2 deletions Sources/HBLambdaTest/maths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,24 @@ struct DebugMiddleware: HBMiddlewareProtocol {
next: (HBRequest, Context) async throws -> Output
) async throws -> Output {
context.logger.debug("\(request.method) \(request.uri)")
context.logger.debug("\(context.event)")

switch context {
case .lambda(let context):
context.logger.debug("\(context.event)")
case .http(let context):
context.logger.debug("HTTP Event \(context.id)")
}

return try await next(request, context)
}
}

@main
struct MathsHandler: HBAPIGatewayLambda {
struct MathsHandler: HBMixedLambdaApplication {
typealias Event = APIGatewayRequest
typealias Output = APIGatewayResponse
typealias Context = HBBasicMixedLambdaContext<Event>

struct Operands: Decodable {
let lhs: Double
let rhs: Double
Expand All @@ -44,7 +54,18 @@ struct MathsHandler: HBAPIGatewayLambda {
let result: Double
}

static var hostingMode: HostingMode {
get throws {
if try HBEnvironment.dotEnv().get("SERVE_HTTP", as: Bool.self) == true {
return .server
} else {
return .lambda
}
}
}

init(context: LambdaInitializationContext) {}
init() {}

func buildResponder() -> some HBResponder<Context> {
let router = HBRouter(context: Context.self)
Expand Down
3 changes: 0 additions & 3 deletions Sources/HummingbirdLambda/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,4 @@ extension HBLambda {
}

public func shutdown() async throws {}

/// default configuration
public var configuration: HBApplicationConfiguration { .init() }
}
66 changes: 66 additions & 0 deletions Sources/HummingbirdLambda/MixedLambdaApplication.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Hummingbird
import AWSLambdaRuntime
import Logging
import NIOCore
import NIOPosix

public enum HBBasicMixedLambdaContext<Event: Sendable>: HBLambdaRequestContext, HBRequestContext {
case lambda(HBBasicLambdaRequestContext<Event>)
case http(HBBasicRequestContext)

public var coreContext: HBCoreRequestContext {
get {
switch self {
case .lambda(let context):
return context.coreContext
case .http(let context):
return context.coreContext
}
}
set {
switch self {
case .lambda(var context):
context.coreContext = newValue
self = .lambda(context)
case .http(var context):
context.coreContext = newValue
self = .http(context)
}
}

Check warning on line 29 in Sources/HummingbirdLambda/MixedLambdaApplication.swift

View check run for this annotation

Codecov / codecov/patch

Sources/HummingbirdLambda/MixedLambdaApplication.swift#L12-L29

Added lines #L12 - L29 were not covered by tests
}

public init(_ event: Event, lambdaContext: LambdaContext) {
self = .lambda(HBBasicLambdaRequestContext(event, lambdaContext: lambdaContext))
}

Check warning on line 34 in Sources/HummingbirdLambda/MixedLambdaApplication.swift

View check run for this annotation

Codecov / codecov/patch

Sources/HummingbirdLambda/MixedLambdaApplication.swift#L32-L34

Added lines #L32 - L34 were not covered by tests

public init(allocator: ByteBufferAllocator, logger: Logger) {
self = .http(HBBasicRequestContext(allocator: allocator, logger: logger))
}

Check warning on line 38 in Sources/HummingbirdLambda/MixedLambdaApplication.swift

View check run for this annotation

Codecov / codecov/patch

Sources/HummingbirdLambda/MixedLambdaApplication.swift#L36-L38

Added lines #L36 - L38 were not covered by tests

public init(channel: any Channel, logger: Logger) {
self = .http(HBBasicRequestContext(channel: channel, logger: logger))
}

Check warning on line 42 in Sources/HummingbirdLambda/MixedLambdaApplication.swift

View check run for this annotation

Codecov / codecov/patch

Sources/HummingbirdLambda/MixedLambdaApplication.swift#L40-L42

Added lines #L40 - L42 were not covered by tests
}

public protocol HBMixedLambdaApplication: HBLambda, HBApplicationProtocol where Context: HBLambdaRequestContext<Event> & HBRequestContext {
static var hostingMode: HostingMode { get async throws }

init()
}

public enum HostingMode {
case lambda, server
}

extension HBMixedLambdaApplication {
public static func main() async throws {
switch try await Self.hostingMode {
case .lambda:
HBLambdaHandler<Self>.main()
case .server:
try await Self().runService()
}
}

Check warning on line 63 in Sources/HummingbirdLambda/MixedLambdaApplication.swift

View check run for this annotation

Codecov / codecov/patch

Sources/HummingbirdLambda/MixedLambdaApplication.swift#L56-L63

Added lines #L56 - L63 were not covered by tests

public var responder: Responder { buildResponder() }

Check warning on line 65 in Sources/HummingbirdLambda/MixedLambdaApplication.swift

View check run for this annotation

Codecov / codecov/patch

Sources/HummingbirdLambda/MixedLambdaApplication.swift#L65

Added line #L65 was not covered by tests
}

0 comments on commit 0e60d57

Please sign in to comment.