|
| 1 | +# RedisDemo |
| 2 | + |
| 3 | +[](https://swift.org/download/) [](https://swift.org/download/) |
| 4 | + |
| 5 | +This example shows the usage of the [LambdaSwiftSprinter](https://github.com/swift-sprinter/aws-lambda-swift-sprinter-core) framework and the plugin [LambdaSwiftSprinterNioPlugin](https://github.com/swift-sprinter/aws-lambda-swift-sprinter-nio-plugin) to build a lambda capable to perform an Redis request using |
| 6 | +[RediStack](https://gitlab.com/mordil/swift-redi-stack.git). |
| 7 | + |
| 8 | +## Swift code |
| 9 | + |
| 10 | +Define an Event and a Response as Codable. |
| 11 | +```swift |
| 12 | +import AsyncHTTPClient |
| 13 | +import Foundation |
| 14 | +#if canImport(FoundationNetworking) |
| 15 | +import FoundationNetworking |
| 16 | +#endif |
| 17 | +import LambdaSwiftSprinter |
| 18 | +import LambdaSwiftSprinterNioPlugin |
| 19 | +import Logging |
| 20 | +import NIO |
| 21 | +import NIOFoundationCompat |
| 22 | +import RediStack |
| 23 | + |
| 24 | +struct Event: Codable { |
| 25 | + let key: String |
| 26 | + let value: String |
| 27 | +} |
| 28 | + |
| 29 | +struct Response: Codable { |
| 30 | + let value: String |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +Add a loger: |
| 37 | +```swift |
| 38 | +let logger = Logger(label: "AWS.Lambda.Redis") |
| 39 | +``` |
| 40 | + |
| 41 | +Add a redis connection: |
| 42 | +```swift |
| 43 | +let elasticacheConfigEndpoint = "redis" |
| 44 | + |
| 45 | +let eventLoop = httpClient.eventLoopGroup.next() |
| 46 | +let connection = try? RedisConnection.connect( |
| 47 | + to: try .makeAddressResolvingHost(elasticacheConfigEndpoint, |
| 48 | + port: RedisConnection.defaultPort), |
| 49 | + on: eventLoop |
| 50 | + ).wait() |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +enum LambdaError: Error { |
| 55 | + case redisConnectionFailed |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +Define the Lambda: |
| 60 | +```swift |
| 61 | +let syncCodableNIOLambda: SyncCodableNIOLambda<Event, Response> = { (event, context) throws -> EventLoopFuture<Response> in |
| 62 | + |
| 63 | + guard let connection = connection else { |
| 64 | + throw LambdaError.redisConnectionFailed |
| 65 | + } |
| 66 | + |
| 67 | + let future = connection.set(event.key, to: event.value) |
| 68 | + .flatMap { |
| 69 | + return connection.get(event.key) |
| 70 | + } |
| 71 | + .map { content -> Response in |
| 72 | + return Response(value: content ?? "") |
| 73 | + } |
| 74 | + return future |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +If there are not error, the Event will be automatically decoded inside the lambda and then used to perform a `set` and a `get` to Redis. |
| 79 | +The value of the `get` is returned into the Response. |
| 80 | + |
| 81 | +Then use this boilerplate code to run the lambda: |
| 82 | +```swift |
| 83 | +do { |
| 84 | + |
| 85 | + let sprinter = try SprinterNIO() |
| 86 | + sprinter.register(handler: "setGet", lambda: syncCodableNIOLambda) |
| 87 | + |
| 88 | + try sprinter.run() |
| 89 | +} catch { |
| 90 | + logger.error("\(String(describing: error))") |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +This will initialize the Sprinter with a Sprinter based on NIO 2 library. |
| 95 | + |
| 96 | +Then the internal handler `setGet` is being registered. |
| 97 | + |
| 98 | +Finally the sprinter run. |
| 99 | + |
| 100 | +Any error will be logged. |
| 101 | + |
| 102 | +Note |
| 103 | + |
| 104 | +In this example we used [swift-log](https://github.com/apple/swift-log.git) to log the output. |
| 105 | + |
| 106 | +## Deployment |
| 107 | + |
| 108 | +To build this example make sure the following lines on the `Makefile` are not commented and the other example configurations are commented. |
| 109 | + |
| 110 | +``` |
| 111 | +... |
| 112 | +
|
| 113 | +# HelloWorld Example Configuration |
| 114 | +# SWIFT_EXECUTABLE=HelloWorld |
| 115 | +# SWIFT_PROJECT_PATH=Examples/HelloWorld |
| 116 | +# LAMBDA_FUNCTION_NAME=HelloWorld |
| 117 | +# LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).helloWorld |
| 118 | +
|
| 119 | +.... |
| 120 | +
|
| 121 | +# RedisDemo Example Configuration |
| 122 | +SWIFT_EXECUTABLE?=RedisDemo |
| 123 | +SWIFT_PROJECT_PATH?=Examples/RedisDemo |
| 124 | +LAMBDA_FUNCTION_NAME?=RedisDemo |
| 125 | +LAMBDA_HANDLER?=$(SWIFT_EXECUTABLE).setGet |
| 126 | +
|
| 127 | +... |
| 128 | +``` |
| 129 | + |
| 130 | +Then follow the main [README](https://github.com/swift-sprinter/aws-lambda-swift-sprinter) to build and test the code. |
| 131 | + |
| 132 | +## Test |
| 133 | + |
| 134 | +The test event is defined in the file `event.json` |
| 135 | +```json |
| 136 | +{ |
| 137 | + "key": "language", |
| 138 | + "value": "Swift" |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +expected response: |
| 143 | + |
| 144 | +```json |
| 145 | +{"value":"Swift"} |
| 146 | +``` |
| 147 | + |
| 148 | +Change it to try different output and error conditions. |
| 149 | + |
| 150 | +# LambdaSwiftSprinter |
| 151 | + |
| 152 | +To know more refer to [LambdaSwiftSprinter](https://github.com/swift-sprinter/aws-lambda-swift-sprinter-core). |
0 commit comments