Library provides mechanism for reading configuration files.
You need to add library to Package.swift file:
- add package to dependencies:
.package(url: "https://github.com/Mikroservices/ExtendedConfiguration.git", from: "1.0.0")- and add product to your target:
.target(name: "App", dependencies: [
.product(name: "Vapor", package: "vapor"),
.product(name: "ExtendedConfiguration", package: "ExtendedConfiguration")
])Then you can add configuration loading during startup Vapor project:
try app.settings.load([
.jsonFile("appsettings.json", optional: false),
.jsonFile("appsettings.\(self.environment.name).json", optional: true),
.environmentVariables(.withPrefix("smtp"))
])Each configuration item will override items from previous files. Your appsettings.json file can look like on below snippet:
{
"smtp": {
"fromName": "Mikroservice",
"fromEmail": "info@server.com",
"hostname": "smtp@server.com",
"port": 465,
"username": "username",
"password": "P@ssword",
"secure": "none"
}
}Now you can read configuration:
let variable = request.application.settings.getString(for: "smtp.fromEmail")Download the source code and run in command line:
$ git clone https://github.com/Mikroservices/ExtendedConfiguration.git
$ swift package update
$ swift buildRun the following command if you want to open project in Xcode:
$ open Package.swiftYou can fork and clone repository. Do your changes and pull a request.