SwiftBox Configuration allows to pass type-safe configuration such as command line, environment variables and external providers (e.g Vault) by declaring one simple struct. Configuration can be inherited from multiple sources simultaneously.
Feed the configuration with:
Command line arguments: ./yourapp --config:simple=test --config:nested.number=1 --config:array.0=string
overriding (source that is declared later can override previous values)
inheritance
optionals
type-safety
nesting (structs and arrays)
environment variables prefixes (avoid conflicting with system variables)
Usage
1. Import
Import module:
import SwiftBoxConfig
2. Configuration structure
When you create your configuration, remember that it in order to be decoded properly, it must conform to the Configuration protocol.
struct Conf: Configuration {
let simple: String
let int: Int
let double: Double
let nested: NestedConf // nested configuration
let array: [String?] // array of optionals
let arraynested: [NestedConf] // nested array
struct NestedConf: Configuration {
let value: String? // optional
}
}
3. Bootstrap
Configuration must be bootstrapped before use. To do so, you need to conform to the ConfigManager protocol in the first place:
extension Conf: ConfigManager {
public static var configuration: Conf? = nil
}
Next, call bootstrap method on your ConfigManager and pass sources you want to use:
try Conf.bootstrap(from: [EnvSource()])
Remember that bootstrap must be called before using config and cannot be called more than once. Otherwise, fatalError will be thrown.
4. Usage
After completing all the previous steps you can finally use config in your application.
You can access the configuration instance via global property:
Prefix can be set for EnvSource, so it reads only variables which key starts with a given value.
Sample Configuration
struct Conf: Configuration {
let simple: String
let int: Int
let double: Double
let nested: NestedConf
let array: [String?]
let arraynested: [NestedConf]
struct NestedConf: Configuration {
let value: String?
}
}
Above example may be populated using following env variables:
If a prefix is set, only arguments which start with a given value will be read. Defaults to --config:
Sample Configuration
struct Conf: Configuration {
let simple: String
let int: Int
let double: Double
let null: String?
let nested: NestedConf
let array: [String?]
let arraynested: [NestedConf]
struct NestedConf: Configuration {
let value: String?
}
}
The example above may be populated using following command line arguments:
To create custom sources, you need to create a class that conforms to ConfigSource.
DictionarySource is the simplest working source that can be used as an example:
public typealias Storage = [String: Any?]
public class DictionarySource: ConfigSource {
let dataSource: Storage
public init(dataSource: Storage) {
self.dataSource = dataSource
}
public func getConfig() throws -> Storage {
return self.dataSource
}
}
SwiftBoxLogging
Logging system for Swift.
Usage
1. Import
import SwiftBoxLogging
2. Bootstrap
Logging should be bootstrapped before use (it defaults to PrintLogger).
Bootstrap requires one parameter which is the logger factory.
Logger factory must return LogHandler from swift-log package.
Logging.bootstrap { label in ElasticsearchLogHandler(label: name) }
2. Usage
Create a logger instance:
fileprivate var logger = Logging.make(#file)
Log a message using one of the available protocol methods:
Usage details can be found in official swift-metrics GitHub repository.
Handlers
LoggerMetricsHandler
Default metrics handler which sends gathered metrics to its logger.
StatsDMetricsHandler
StatsD Metrics Handler is responsible for sending gathered logs to StatsD server. Supports TCP and UDP protocols.
Metrics are sent in separate thread, so operation is non-blocking for application.
SwiftBox
SwiftBox is a SwiftNIO based package that helps building Swift/NIO-based microservices.
What’s included?
Running microservices with Swift requires:
Coming soon: Integration with https://github.com/apple/swift-log.
SwiftBox Configuration
SwiftBox Configuration allows to pass type-safe configuration such as command line, environment variables and external providers (e.g Vault) by declaring one simple struct. Configuration can be inherited from multiple sources simultaneously.
Feed the configuration with:
./yourapp --config:simple=test --config:nested.number=1 --config:array.0=string
SIMPLE=test NESTED_NUMBER=1 ARRAY_0=string ./yourapp
SwiftBox Configuration supports:
Usage
1. Import
Import module:
2. Configuration structure
When you create your configuration, remember that it in order to be decoded properly, it must conform to the
Configuration
protocol.3. Bootstrap
Configuration must be bootstrapped before use. To do so, you need to conform to the
ConfigManager
protocol in the first place:Next, call
bootstrap
method on yourConfigManager
and pass sources you want to use:Remember that bootstrap must be called before using config and cannot be called more than once. Otherwise,
fatalError
will be thrown.4. Usage
After completing all the previous steps you can finally use config in your application. You can access the configuration instance via
global
property:Sources
Configuration can be fed with multiple sources. Sources are passed into bootstrap function.
If you are using multiple sources, outputs are merged (structs are merged recursively, other values are overridden):
Dictionary source
Allows reading configuration from Dictionary, may be used to specify in-code defaults for configuration.
Example
JSON source
Allows reading configuration from JSON data.
Example
Environment source
Allows reading configuration data from environment.
Example
Prefix can be set for
EnvSource
, so it reads only variables which key starts with a given value.Sample Configuration
Above example may be populated using following env variables:
Value “null” is coerced to internal nil value
Command line source
Allows reading configuration data from environment.
Example
If a prefix is set, only arguments which start with a given value will be read. Defaults to
--config:
Sample Configuration
The example above may be populated using following command line arguments:
Value “null” is coerced to internal nil value
Custom sources
To create custom sources, you need to create a class that conforms to
ConfigSource
.DictionarySource
is the simplest working source that can be used as an example:SwiftBoxLogging
Logging system for Swift.
Usage
1. Import
2. Bootstrap
Logging should be bootstrapped before use (it defaults to
PrintLogger
). Bootstrap requires one parameter which is the logger factory.Logger factory must return
LogHandler
from swift-log package.2. Usage
Create a logger instance:
Log a message using one of the available protocol methods:
Custom Loggers
To create custom loggers your class must conform to
LoggerProtocol
.Vapor
You can use the same logging by overriding Vapor’s default log handler:
SwiftBoxMetrics
StatsD and Logger handlers for official swift-metrics API.
Supported metric types:
Usage
1. Import
2. Bootstrap
Metrics must be bootstraped with Handler, which conforms to
MetricsHandler
protocol:3. Usage
Usage details can be found in official swift-metrics GitHub repository.
Handlers
LoggerMetricsHandler
Default metrics handler which sends gathered metrics to its logger.
StatsDMetricsHandler
StatsD Metrics Handler is responsible for sending gathered logs to StatsD server. Supports TCP and UDP protocols. Metrics are sent in separate thread, so operation is non-blocking for application.
baseMetricPath
is a path that will be prepended to every metric sent via handler.client
is aTCPStatsDClient
orUDPStatsDClient
instance.Custom Handlers
To create custom handlers, conform to
MetricsHandler
class.