@@ -39,7 +39,7 @@ dependencies: [
39
39
.package (url : " https://github.com/apple/swift-openapi-runtime.git" , from : " 1.8.2" ),
40
40
41
41
// 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 " ),
43
43
.package (url : " https://github.com/swift-server/swift-aws-lambda-events.git" , from : " 1.2.0" ),
44
44
.package (url : " https://github.com/swift-server/swift-openapi-lambda.git" , from : " 2.0.0" ),
45
45
],
@@ -74,7 +74,7 @@ import OpenAPILambda // <-- 1. import this library
74
74
struct QuoteServiceImpl : APIProtocol , OpenAPILambdaHttpApi { // <-- 3. add the OpenAPILambdaHttpApi protocol
75
75
76
76
// 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)
78
78
try self .registerHandlers (on : transport)
79
79
}
80
80
@@ -85,8 +85,16 @@ struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi { // <-- 3. add the O
85
85
86
86
// Your existing OpenAPI implementation
87
87
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)))
90
98
}
91
99
}
92
100
```
@@ -165,7 +173,7 @@ struct CustomServiceLambda: OpenAPILambda {
165
173
typealias Event = YourCustomEvent
166
174
typealias Output = YourCustomResponse
167
175
168
- init (transport : OpenAPILambdaTransport) throws {
176
+ func register (transport : OpenAPILambdaTransport) throws {
169
177
let handler = YourServiceImpl ()
170
178
try handler.registerHandlers (on : transport)
171
179
}
@@ -185,18 +193,46 @@ struct CustomServiceLambda: OpenAPILambda {
185
193
``` swift
186
194
import ServiceLifecycle
187
195
188
- // In your OpenAPI service, explicitley create and manage the LambdaRuntime
196
+ // In your OpenAPI service, explicitly create and manage the LambdaRuntime
189
197
static func main () async throws {
190
198
let lambdaRuntime = try LambdaRuntime (body : Self .handler ())
191
199
let serviceGroup = ServiceGroup (
192
200
services : [lambdaRuntime],
193
201
gracefulShutdownSignals : [.sigterm ],
194
- cancellationSignals : [.sigint ]
202
+ cancellationSignals : [.sigint ],
203
+ logger : Logger (label : " ServiceGroup" )
195
204
)
196
205
try await serviceGroup.run ()
197
206
}
198
207
```
199
208
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
+
200
236
## References
201
237
202
238
- [ Swift OpenAPI Generator] ( https://swiftpackageindex.com/apple/swift-openapi-generator/documentation ) - Complete documentation and tutorials
0 commit comments