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

+34-8
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+27-20
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

+1-1
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

+1-1
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

+1-1
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

+2-2
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
}

Sources/BreezeLambdaAPI/APIGatewayV2Request+Extensions.swift

+1-1
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/BreezeLambdaAPI/APIGatewayV2Response+Extensions.swift

+1-1
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// BreezeAPIConfiguration.swift
3+
// BreezeLambdaDynamoDBAPI
4+
//
5+
// Created by Andrea Scuderi on 24/12/2024.
6+
//
7+
8+
import SotoDynamoDB
9+
import BreezeDynamoDBService
10+
import AWSLambdaRuntime
11+
12+
public protocol APIConfiguring {
13+
var dbTimeout: Int64 { get }
14+
func operation() throws -> BreezeOperation
15+
func getConfig() throws -> BreezeDynamoDBConfig
16+
}
17+
18+
public struct BreezeAPIConfiguration: APIConfiguring {
19+
20+
public init() {}
21+
22+
public let dbTimeout: Int64 = 30
23+
24+
public func operation() throws -> BreezeOperation {
25+
guard let handler = Lambda.env("_HANDLER"),
26+
let operation = BreezeOperation(handler: handler)
27+
else {
28+
throw BreezeLambdaAPIError.invalidHandler
29+
}
30+
return operation
31+
}
32+
33+
public func getConfig() throws -> BreezeDynamoDBConfig {
34+
BreezeDynamoDBConfig(
35+
region: currentRegion(),
36+
tableName: try tableName(),
37+
keyName: try keyName(),
38+
endpoint: endpoint()
39+
)
40+
}
41+
42+
func currentRegion() -> Region {
43+
if let awsRegion = Lambda.env("AWS_REGION") {
44+
let value = Region(rawValue: awsRegion)
45+
return value
46+
} else {
47+
return .useast1
48+
}
49+
}
50+
51+
func tableName() throws -> String {
52+
guard let tableName = Lambda.env("DYNAMO_DB_TABLE_NAME") else {
53+
throw BreezeLambdaAPIError.tableNameNotFound
54+
}
55+
return tableName
56+
}
57+
58+
func keyName() throws -> String {
59+
guard let tableName = Lambda.env("DYNAMO_DB_KEY") else {
60+
throw BreezeLambdaAPIError.keyNameNotFound
61+
}
62+
return tableName
63+
}
64+
65+
func endpoint() -> String? {
66+
if let localstack = Lambda.env("LOCALSTACK_ENDPOINT"),
67+
!localstack.isEmpty {
68+
return localstack
69+
}
70+
return nil
71+
}
72+
}

Sources/BreezeLambdaAPI/BreezeEmptyResponse.swift

+1-1
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/BreezeLambdaAPI/BreezeLambdaAPIService.swift renamed to Sources/BreezeLambdaAPI/BreezeLambdaAPI.swift

+11-54
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,76 +18,33 @@ import BreezeDynamoDBService
1818
import BreezeHTTPClientService
1919
import AWSLambdaRuntime
2020

21-
public actor BreezeLambdaAPIService<T: BreezeCodable>: Service {
21+
public actor BreezeLambdaAPI<T: BreezeCodable>: Service {
2222

23-
let logger = Logger(label: "service-group")
23+
let logger = Logger(label: "service-group-breeze-lambda-api")
2424
let timeout: TimeAmount
2525
let httpClientService: BreezeHTTPClientServing
2626
let dynamoDBService: BreezeDynamoDBServing
2727
let breezeLambdaService: BreezeLambdaService<T>
2828
private let serviceGroup: ServiceGroup
29+
private let apiConfig: any APIConfiguring
2930

30-
static func currentRegion() -> Region {
31-
if let awsRegion = Lambda.env("AWS_REGION") {
32-
let value = Region(rawValue: awsRegion)
33-
return value
34-
} else {
35-
return .useast1
36-
}
37-
}
38-
39-
static func tableName() throws -> String {
40-
guard let tableName = Lambda.env("DYNAMO_DB_TABLE_NAME") else {
41-
throw BreezeLambdaAPIError.tableNameNotFound
42-
}
43-
return tableName
44-
}
45-
46-
static func keyName() throws -> String {
47-
guard let tableName = Lambda.env("DYNAMO_DB_KEY") else {
48-
throw BreezeLambdaAPIError.keyNameNotFound
49-
}
50-
return tableName
51-
}
52-
53-
static func endpoint() -> String? {
54-
if let localstack = Lambda.env("LOCALSTACK_ENDPOINT"),
55-
!localstack.isEmpty {
56-
return localstack
57-
}
58-
return nil
59-
}
60-
61-
static func operation() throws -> BreezeOperation {
62-
guard let handler = Lambda.env("_HANDLER"),
63-
let operation = BreezeOperation(handler: handler)
64-
else {
65-
throw BreezeLambdaAPIError.invalidHandler
66-
}
67-
return operation
68-
}
69-
70-
public init(dbTimeout: Int64 = 30) throws {
31+
public init(apiConfig: APIConfiguring = BreezeAPIConfiguration()) throws {
7132
do {
72-
self.timeout = .seconds(dbTimeout)
33+
self.apiConfig = apiConfig
34+
self.timeout = .seconds(apiConfig.dbTimeout)
7335
self.httpClientService = BreezeHTTPClientService(
7436
timeout: timeout,
7537
logger: logger
7638
)
77-
let config = BreezeDynamoDBConfig(
78-
region: Self.currentRegion(),
79-
tableName: try Self.tableName(),
80-
keyName: try Self.keyName(),
81-
endpoint: Self.endpoint()
82-
)
39+
let config = try apiConfig.getConfig()
8340
let serviceConfig = BreezeClientServiceConfig(
8441
httpClientService: httpClientService,
8542
logger: logger
8643
)
8744
self.dynamoDBService = BreezeDynamoDBService(config: config, serviceConfig: serviceConfig)
8845
self.breezeLambdaService = BreezeLambdaService<T>(
8946
dynamoDBService: dynamoDBService,
90-
operation: try Self.operation(),
47+
operation: try apiConfig.operation(),
9148
logger: logger
9249
)
9350
self.serviceGroup = ServiceGroup(
@@ -114,14 +71,14 @@ public actor BreezeLambdaAPIService<T: BreezeCodable>: Service {
11471
)
11572
} catch {
11673
logger.error("\(error.localizedDescription)")
117-
fatalError(error.localizedDescription)
74+
throw error
11875
}
11976
}
12077

12178
public func run() async throws {
12279
logger.info("Starting BreezeLambdaAPIService...")
12380
try await serviceGroup.run()
124-
logger.info("Shutting down BreezeLambdaAPIService...")
81+
logger.info("Stopping BreezeLambdaAPIService...")
12582
try await gracefulShutdown()
12683
logger.info("BreezeLambdaAPIService is stopped.")
12784
}

Sources/BreezeLambdaAPI/BreezeLambdaAPIError.swift

+1-1
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.

0 commit comments

Comments
 (0)