Skip to content

Commit 61a2932

Browse files
author
Andrea Scuderi
committed
Update test with configuration injection
1 parent b3cec46 commit 61a2932

32 files changed

+260
-204
lines changed

Sources/BreezeDemoApplication/BreezeDemoApplication.swift

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-serverless
1+
// Copyright 2024 (c) Andrea Scuderi - https://github.com/swift-serverless
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -15,17 +15,43 @@
1515
import BreezeLambdaAPI
1616
import BreezeDynamoDBService
1717

18-
struct Message: BreezeCodable {
19-
var key: String
20-
let message: String
21-
var createdAt: String?
22-
var updatedAt: String?
18+
struct Item: Codable {
19+
public var key: String
20+
public let name: String
21+
public let description: String
22+
public var createdAt: String?
23+
public var updatedAt: String?
24+
25+
enum CodingKeys: String, CodingKey {
26+
case key = "itemKey"
27+
case name
28+
case description
29+
case createdAt
30+
case updatedAt
31+
}
32+
}
33+
34+
extension Item: BreezeCodable { }
35+
36+
struct APIConfiguration: APIConfiguring {
37+
let dbTimeout: Int64 = 30
38+
func operation() throws -> BreezeOperation {
39+
.list
40+
}
41+
42+
func getConfig() throws -> BreezeDynamoDBConfig {
43+
BreezeDynamoDBConfig(region: .useast1, tableName: "Breeze", keyName: "itemKey", endpoint: "http://127.0.0.1:4566")
44+
}
2345
}
2446

2547
@main
2648
struct BreezeDemoApplication {
2749
static func main() async throws {
28-
let lambdaAPIService = try BreezeLambdaAPIService<Message>(dbTimeout: 30)
29-
try await lambdaAPIService.run()
50+
do {
51+
let lambdaAPIService = try BreezeLambdaAPI<Item>(apiConfig: APIConfiguration())
52+
try await lambdaAPIService.run()
53+
} catch {
54+
print(error.localizedDescription)
55+
}
3056
}
3157
}

Sources/BreezeDynamoDBService/BreezeCodable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-serverless
1+
// Copyright 2024 (c) Andrea Scuderi - https://github.com/swift-serverless
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

Sources/BreezeDynamoDBService/BreezeDynamoDBConfig.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-serverless
1+
// Copyright 2024 (c) Andrea Scuderi - https://github.com/swift-serverless
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

Sources/BreezeDynamoDBService/BreezeDynamoDBManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-serverless
1+
// Copyright 2024 (c) Andrea Scuderi - https://github.com/swift-serverless
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

Sources/BreezeDynamoDBService/BreezeDynamoDBManaging.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-serverless
1+
// Copyright 2024 (c) Andrea Scuderi - https://github.com/swift-serverless
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

Sources/BreezeDynamoDBService/BreezeDynamoDBService.swift

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-serverless
1+
// Copyright 2024 (c) Andrea Scuderi - https://github.com/swift-serverless
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -18,16 +18,37 @@ import BreezeHTTPClientService
1818
import Logging
1919

2020
public protocol BreezeDynamoDBServing: Actor, Service {
21-
var dbManager: BreezeDynamoDBManaging? { get }
21+
func dbManager() async -> BreezeDynamoDBManaging
2222
}
2323

2424
public actor BreezeDynamoDBService: BreezeDynamoDBServing {
25-
26-
public var dbManager: BreezeDynamoDBManaging?
25+
26+
private var _dbManager: BreezeDynamoDBManaging?
2727
private let config: BreezeDynamoDBConfig
2828
private let serviceConfig: BreezeClientServiceConfig
2929
private let DBManagingType: BreezeDynamoDBManaging.Type
3030

31+
public func dbManager() async -> BreezeDynamoDBManaging {
32+
if let _dbManager {
33+
return _dbManager
34+
}
35+
let httpClient = await serviceConfig.httpClientService.httpClient
36+
let awsClient = AWSClient(httpClient: httpClient)
37+
self.awsClient = awsClient
38+
let db = SotoDynamoDB.DynamoDB(
39+
client: awsClient,
40+
region: config.region,
41+
endpoint: config.endpoint
42+
)
43+
let dbManager = DBManagingType.init(
44+
db: db,
45+
tableName: config.tableName,
46+
keyName: config.keyName
47+
)
48+
_dbManager = dbManager
49+
return dbManager
50+
}
51+
3152
public init(
3253
config: BreezeDynamoDBConfig,
3354
serviceConfig: BreezeClientServiceConfig,
@@ -46,29 +67,15 @@ public actor BreezeDynamoDBService: BreezeDynamoDBServing {
4667

4768
public func run() async throws {
4869
logger.info("Starting DynamoDBService...")
49-
let httpClient = await serviceConfig.httpClientService.httpClient
50-
let awsClient = AWSClient(httpClient: httpClient)
51-
self.awsClient = awsClient
52-
let db = SotoDynamoDB.DynamoDB(
53-
client: awsClient,
54-
region: config.region,
55-
endpoint: config.endpoint
56-
)
57-
self.dbManager = DBManagingType.init(
58-
db: db,
59-
tableName: config.tableName,
60-
keyName: config.keyName
61-
)
62-
6370
logger.info("DynamoDBService is running with config...")
6471
logger.info("region: \(config.region)")
6572
logger.info("tableName: \(config.tableName)")
6673
logger.info("keyName: \(config.keyName)")
6774

6875
try await gracefulShutdown()
6976

70-
logger.info("Shutting down DynamoDBService...")
71-
try await awsClient.shutdown()
77+
logger.info("Stopping DynamoDBService...")
78+
try await awsClient?.shutdown()
7279
self.awsClient = nil
7380
logger.info("DynamoDBService is stopped.")
7481
}

Sources/BreezeDynamoDBService/Foundation+Extension.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-serverless
1+
// Copyright 2024 (c) Andrea Scuderi - https://github.com/swift-serverless
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

Sources/BreezeDynamoDBService/ListResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-serverless
1+
// Copyright 2024 (c) Andrea Scuderi - https://github.com/swift-serverless
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

Sources/BreezeHTTPClientService/BreezeClientServiceConfig.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-serverless
1+
// Copyright 2024 (c) Andrea Scuderi - https://github.com/swift-serverless
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

Sources/BreezeHTTPClientService/BreezeHTTPClientService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-serverless
1+
// Copyright 2024 (c) Andrea Scuderi - https://github.com/swift-serverless
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -45,7 +45,7 @@ public actor BreezeHTTPClientService: BreezeHTTPClientServing {
4545
logger.info("HTTPClientService started...")
4646
try await gracefulShutdown()
4747

48-
logger.info("Shutting down HTTPClientService...")
48+
logger.info("Stopping HTTPClientService...")
4949
try await httpClient.shutdown()
5050
logger.info("HTTPClientService shutdown completed.")
5151
}

0 commit comments

Comments
 (0)