Skip to content

Commit 9916cf2

Browse files
committed
Update README
1 parent b2bdce9 commit 9916cf2

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

Examples/quoteapi/Sources/QuoteAPI/QuoteService.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi {
2828
static func main() async throws {
2929

3030
// when you just need to run the Lambda function, call Self.run()
31-
// try await Self.run()
31+
try await Self.run()
3232

3333
// =============================
3434

@@ -57,19 +57,20 @@ struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi {
5757
// When you need full control on this struct lifecycle, for example to inject dependencies,
5858
// you can create your own instance of `OpenAPILambda`
5959
// 1. add your custom init() function
60-
// 2. create the class
61-
// 3. create the lambda handler and pass this class to the initializer
62-
63-
// let openAPIService = QuoteServiceImpl(i: 42)
64-
// let lambda = try OpenAPILambdaHandler(service: openAPIService)
65-
// let lambdaHandler = lambda.handler
66-
// let lambdaRuntime = LambdaRuntime(body: lambdaHandler)
60+
// 2. create an instance of this struct, inject any required dependencies,
61+
// such as a database connection
62+
// 3. create the lambda handler and pass the instance to its initializer
63+
// 4. create the lambda runtime and pass the handler to its initializer
64+
65+
// let openAPIService = QuoteServiceImpl(i: 42) // 2.
66+
// let lambda = try OpenAPILambdaHandler(service: openAPIService) // 3.
67+
// let lambdaRuntime = LambdaRuntime(body: lambda.handler) // 4.
6768
// try await lambdaRuntime.run()
6869
}
6970

7071
// example of dependency injection when using a custom initializer
7172
// let i: Int
72-
// init(i: Int) {
73+
// init(i: Int) { // 1.
7374
// self.i = i
7475
// }
7576
// init() { self.i = 0 } // to comply with protocol

README.md

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
7474
struct 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
186194
import ServiceLifecycle
187195

188-
// In your OpenAPI service, explicitley create and manage the LambdaRuntime
196+
// In your OpenAPI service, explicitly create and manage the LambdaRuntime
189197
static 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

Comments
 (0)