@@ -39,7 +39,7 @@ dependencies: [
3939 .package (url : " https://github.com/apple/swift-openapi-runtime.git" , from : " 1.8.2" ),
4040
4141 // add these three dependencies
42- .package (url : " https://github.com/swift-server/swift-aws-lambda-runtime.git" , from : " 2.0.0-beta.1 " ),
42+ .package (url : " https://github.com/swift-server/swift-aws-lambda-runtime.git" , from : " 2.0.0-beta.3 " ),
4343 .package (url : " https://github.com/swift-server/swift-aws-lambda-events.git" , from : " 1.2.0" ),
4444 .package (url : " https://github.com/swift-server/swift-openapi-lambda.git" , from : " 2.0.0" ),
4545],
@@ -74,7 +74,7 @@ import OpenAPILambda // <-- 1. import this library
7474struct QuoteServiceImpl : APIProtocol , OpenAPILambdaHttpApi { // <-- 3. add the OpenAPILambdaHttpApi protocol
7575
7676 // The registration of your OpenAPI handlers
77- init (transport : OpenAPILambdaTransport) throws { // <-- 4. add this constructor (don't remove the call to ` registerHandlers(on:)` )
77+ func register (transport : OpenAPILambdaTransport) throws { // <-- 4. add this method (calls registerHandlers)
7878 try self .registerHandlers (on : transport)
7979 }
8080
@@ -85,8 +85,16 @@ struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi { // <-- 3. add the O
8585
8686 // Your existing OpenAPI implementation
8787 func getQuote (_ input : Operations.getQuote.Input) async throws -> Operations.getQuote.Output {
88- ...
89- return .ok (.init (body : .json (result)))
88+ let symbol = input.path .symbol
89+ let price = Components.Schemas .quote (
90+ symbol : symbol,
91+ price : Double .random (in : 100 ..< 150 ).rounded (),
92+ change : Double .random (in : -5 ..< 5 ).rounded (),
93+ changePercent : Double .random (in : -0.05 ..< 0.05 ),
94+ volume : Double .random (in : 10000 ..< 100000 ).rounded (),
95+ timestamp : Date ()
96+ )
97+ return .ok (.init (body : .json (price)))
9098 }
9199}
92100```
@@ -165,7 +173,7 @@ struct CustomServiceLambda: OpenAPILambda {
165173 typealias Event = YourCustomEvent
166174 typealias Output = YourCustomResponse
167175
168- init (transport : OpenAPILambdaTransport) throws {
176+ func register (transport : OpenAPILambdaTransport) throws {
169177 let handler = YourServiceImpl ()
170178 try handler.registerHandlers (on : transport)
171179 }
@@ -185,18 +193,46 @@ struct CustomServiceLambda: OpenAPILambda {
185193``` swift
186194import ServiceLifecycle
187195
188- // In your OpenAPI service, explicitley create and manage the LambdaRuntime
196+ // In your OpenAPI service, explicitly create and manage the LambdaRuntime
189197static func main () async throws {
190198 let lambdaRuntime = try LambdaRuntime (body : Self .handler ())
191199 let serviceGroup = ServiceGroup (
192200 services : [lambdaRuntime],
193201 gracefulShutdownSignals : [.sigterm ],
194- cancellationSignals : [.sigint ]
202+ cancellationSignals : [.sigint ],
203+ logger : Logger (label : " ServiceGroup" )
195204 )
196205 try await serviceGroup.run ()
197206}
198207```
199208
209+ ### Dependency Injection
210+
211+ For advanced use cases requiring dependency injection:
212+
213+ ``` swift
214+ @main
215+ struct QuoteServiceImpl : APIProtocol , OpenAPILambdaHttpApi {
216+ let customDependency: Int
217+
218+ init (customDependency : Int = 0 ) {
219+ self .customDependency = customDependency
220+ }
221+
222+ // the entry point can be in another file / struct as well.
223+ static func main () async throws {
224+ let service = QuoteServiceImpl (customDependency : 42 )
225+ let lambda = try OpenAPILambdaHandler (service : service)
226+ let lambdaRuntime = LambdaRuntime (body : lambda.handler )
227+ try await lambdaRuntime.run ()
228+ }
229+
230+ func register (transport : OpenAPILambdaTransport) throws {
231+ try self .registerHandlers (on : transport)
232+ }
233+ }
234+ ```
235+
200236## References
201237
202238- [ Swift OpenAPI Generator] ( https://swiftpackageindex.com/apple/swift-openapi-generator/documentation ) - Complete documentation and tutorials
0 commit comments