77
88import SotoDynamoDB
99import BreezeDynamoDBService
10- import AWSLambdaRuntime
10+ import Configuration
1111
1212/// Defines the configuration for the Breeze Lambda API.
13- public protocol APIConfiguring {
13+ public protocol APIConfiguring : Sendable {
1414 var dbTimeout : Int64 { get }
1515 func operation( ) throws -> BreezeOperation
1616 func getConfig( ) throws -> BreezeDynamoDBConfig
@@ -26,11 +26,30 @@ public protocol APIConfiguring {
2626/// - `DYNAMO_DB_TABLE_NAME`: The name of the DynamoDB table.
2727/// - `DYNAMO_DB_KEY`: The name of the primary key in the DynamoDB table.
2828public struct BreezeAPIConfiguration : APIConfiguring {
29+ private enum Keys {
30+ static let handler : ConfigKey = " _HANDLER "
31+ static let awsRegion : ConfigKey = " AWS_REGION "
32+ static let tableName : ConfigKey = " DYNAMO_DB_TABLE_NAME "
33+ static let keyName : ConfigKey = " DYNAMO_DB_KEY "
34+ static let localstackEndpoint : ConfigKey = " LOCALSTACK_ENDPOINT "
35+ static let dbTimeout : ConfigKey = " BREEZE_DB_TIMEOUT "
36+ }
2937
30- public init ( ) { }
38+ private let reader : ConfigReader
3139
32- /// Timeout for database operations in seconds.
33- public let dbTimeout : Int64 = 30
40+ /// Creates the configuration using the supplied providers. Defaults to environment variables.
41+ public init (
42+ reader: ConfigReader = ConfigReader (
43+ providers: [ EnvironmentVariablesProvider ( ) ]
44+ )
45+ ) {
46+ self . reader = reader
47+ }
48+
49+ /// Timeout for database operations in seconds, configurable via `BREEZE_DB_TIMEOUT`.
50+ public var dbTimeout : Int64 {
51+ Int64 ( reader. int ( forKey: Keys . dbTimeout, default: 30 ) )
52+ }
3453
3554 /// The operation handler for Breeze operations.
3655 ///
@@ -43,7 +62,7 @@ public struct BreezeAPIConfiguration: APIConfiguring {
4362 ///
4463 /// See BreezeOperation for more details.
4564 public func operation( ) throws -> BreezeOperation {
46- guard let handler = Lambda . env ( " _HANDLER " ) ,
65+ guard let handler = reader . string ( forKey : Keys . handler ) ,
4766 let operation = BreezeOperation ( handler: handler)
4867 else {
4968 throw BreezeLambdaAPIError . invalidHandler
@@ -78,12 +97,10 @@ public struct BreezeAPIConfiguration: APIConfiguring {
7897 ///
7998 /// This method is used to determine the AWS region where the DynamoDB table is located.
8099 func currentRegion( ) -> Region {
81- if let awsRegion = Lambda . env ( " AWS_REGION " ) {
82- let value = Region ( rawValue: awsRegion)
83- return value
84- } else {
85- return . useast1
100+ if let awsRegion = reader. string ( forKey: Keys . awsRegion) {
101+ return Region ( rawValue: awsRegion)
86102 }
103+ return . useast1
87104 }
88105
89106 /// Returns the DynamoDB table name from the `DYNAMO_DB_TABLE_NAME` environment variable.
@@ -92,7 +109,7 @@ public struct BreezeAPIConfiguration: APIConfiguring {
92109 /// This method is used to retrieve the name of the DynamoDB table that will be used by the Breeze Lambda API.
93110 /// - Important: The table name is essential for performing operations on the DynamoDB table.
94111 func tableName( ) throws -> String {
95- guard let tableName = Lambda . env ( " DYNAMO_DB_TABLE_NAME " ) else {
112+ guard let tableName = reader . string ( forKey : Keys . tableName ) else {
96113 throw BreezeLambdaAPIError . tableNameNotFound
97114 }
98115 return tableName
@@ -104,7 +121,7 @@ public struct BreezeAPIConfiguration: APIConfiguring {
104121 /// This method is used to retrieve the name of the primary key in the DynamoDB table that will be used by the Breeze Lambda API.
105122 /// - Important: The key name is essential for identifying items in the DynamoDB table.
106123 func keyName( ) throws -> String {
107- guard let keyName = Lambda . env ( " DYNAMO_DB_KEY " ) else {
124+ guard let keyName = reader . string ( forKey : Keys . keyName ) else {
108125 throw BreezeLambdaAPIError . keyNameNotFound
109126 }
110127 return keyName
@@ -120,7 +137,7 @@ public struct BreezeAPIConfiguration: APIConfiguring {
120137 /// - To set it you need to set the `LOCALSTACK_ENDPOINT` environment variable to the URL of your LocalStack instance.
121138 /// - The Default LocalStack endpoint is `http://localhost:4566`
122139 func endpoint( ) -> String ? {
123- if let localstack = Lambda . env ( " LOCALSTACK_ENDPOINT " ) ,
140+ if let localstack = reader . string ( forKey : Keys . localstackEndpoint ) ,
124141 !localstack. isEmpty {
125142 return localstack
126143 }
0 commit comments