2017-09-25 13:50:56 +00:00
|
|
|
# configuration
|
2016-11-23 20:51:20 +00:00
|
|
|
|
2017-06-14 18:48:13 +00:00
|
|
|
[![Pub](https://img.shields.io/pub/v/angel_configuration.svg)](https://pub.dartlang.org/packages/angel_configuration)
|
2017-06-14 18:48:27 +00:00
|
|
|
[![build status](https://travis-ci.org/angel-dart/configuration.svg)](https://travis-ci.org/angel-dart/configuration)
|
2016-11-23 20:51:20 +00:00
|
|
|
|
2017-06-14 18:48:13 +00:00
|
|
|
Automatic YAML configuration loader for Angel.
|
2016-04-22 01:17:31 +00:00
|
|
|
|
|
|
|
# About
|
|
|
|
Any web app needs different configuration for development and production. This plugin will search
|
2017-09-25 13:50:56 +00:00
|
|
|
for a `config/default.yaml` file. If it is found, configuration from it is loaded into `app.configuration`.
|
2016-04-22 01:17:31 +00:00
|
|
|
Then, it will look for a `config/$ANGEL_ENV` file. (i.e. config/development.yaml). If this found, all of its
|
|
|
|
configuration be loaded, and will override anything loaded from the `default.yaml` file. This allows for your
|
|
|
|
app to work under different conditions without you re-coding anything. :)
|
|
|
|
|
|
|
|
# Installation
|
|
|
|
In `pubspec.yaml`:
|
|
|
|
|
2016-09-19 11:19:22 +00:00
|
|
|
```yaml
|
|
|
|
dependencies:
|
2017-03-08 22:47:58 +00:00
|
|
|
angel_configuration: ^1.0.0
|
2016-09-19 11:19:22 +00:00
|
|
|
```
|
2016-04-22 01:17:31 +00:00
|
|
|
|
|
|
|
# Usage
|
|
|
|
|
2017-06-14 18:48:13 +00:00
|
|
|
**Example Configuration**
|
|
|
|
```yaml
|
|
|
|
# Define normal YAML objects
|
|
|
|
some_key: foo
|
|
|
|
this_is_a_map:
|
|
|
|
a_string: "string"
|
|
|
|
another_string: "string"
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
You can also load configuration from the environment:
|
|
|
|
```yaml
|
|
|
|
# Loaded from the environment
|
|
|
|
system_path: $PATH
|
|
|
|
```
|
|
|
|
|
|
|
|
If a `.env` file is present in your configuration directory, then it will be loaded before
|
|
|
|
applying YAML configuration.
|
|
|
|
|
2016-09-19 20:31:32 +00:00
|
|
|
**Server-side**
|
2017-09-25 13:50:56 +00:00
|
|
|
Call `configuration()`. The loaded configuration will be available in your application's
|
|
|
|
`configuration` map, which means you can access them like normal instance members.
|
2017-06-14 18:48:13 +00:00
|
|
|
|
|
|
|
```dart
|
|
|
|
main() {
|
2017-09-25 13:50:56 +00:00
|
|
|
print(app.foo == app.configuration['foo']); // true
|
2017-06-14 18:48:13 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
An instance of `Configuration` will also be injected to your application, and it works
|
|
|
|
the same way:
|
2016-09-19 20:31:32 +00:00
|
|
|
|
2016-04-22 01:17:31 +00:00
|
|
|
```dart
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
|
|
|
import 'package:angel_configuration/angel_configuration.dart';
|
2017-09-25 13:50:56 +00:00
|
|
|
import 'package:file/local.dart';
|
2016-04-22 01:17:31 +00:00
|
|
|
|
|
|
|
main() async {
|
2017-09-25 13:50:56 +00:00
|
|
|
var app = new Angel();
|
|
|
|
var fileSystem = const LocalFileSystem();
|
|
|
|
|
|
|
|
await app.configure(configuration(fileSystem)); // It's that easy!
|
2016-11-23 20:51:20 +00:00
|
|
|
|
|
|
|
app.get('/foo', (Configuration config) {
|
|
|
|
return config.some_key;
|
|
|
|
});
|
2016-04-22 01:17:31 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2017-09-25 13:50:56 +00:00
|
|
|
`configuration` also accepts a `sourceDirectory` or `overrideEnvironmentName` parameter.
|
2016-04-22 01:17:31 +00:00
|
|
|
The former will allow you to search in a directory other than `config`, and the latter lets you
|
2017-06-14 18:48:13 +00:00
|
|
|
override `$ANGEL_ENV` by specifying a specific configuration name to look for (i.e. `production`).
|
2016-09-19 20:31:32 +00:00
|
|
|
|
2017-08-16 00:29:13 +00:00
|
|
|
This package uses
|
|
|
|
[`package:merge_map`](https://github.com/thosakwe/merge_map)
|
|
|
|
internally, so existing configurations can be deeply merged.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
# default.yaml
|
|
|
|
foo:
|
|
|
|
bar: baz
|
|
|
|
quux: hello
|
|
|
|
|
|
|
|
# production.yaml
|
|
|
|
foo:
|
|
|
|
quux: goodbye
|
|
|
|
yellow: submarine
|
|
|
|
|
|
|
|
# Propagates to:
|
|
|
|
foo:
|
|
|
|
bar: baz
|
|
|
|
quux: goodbye
|
|
|
|
yellow: submarine
|
|
|
|
```
|
|
|
|
|
2016-09-19 20:31:32 +00:00
|
|
|
**In the Browser**
|
|
|
|
|
|
|
|
You can easily load configuration values within your client-side app,
|
|
|
|
and they will be automatically replaced by a Barback transformer.
|
|
|
|
|
2016-09-19 20:38:29 +00:00
|
|
|
In your `pubspec.yaml`:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
transformers:
|
|
|
|
- angel_configuration
|
|
|
|
```
|
|
|
|
|
|
|
|
In your app:
|
|
|
|
|
2016-09-19 20:31:32 +00:00
|
|
|
```dart
|
|
|
|
import 'package:angel_configuration/browser.dart';
|
|
|
|
|
|
|
|
main() async {
|
|
|
|
print(config("some_key.other.nested_key"));
|
|
|
|
}
|
|
|
|
```
|
2016-09-19 20:33:01 +00:00
|
|
|
|
|
|
|
You can also provide a `dir` or `env` argument, corresponding to
|
|
|
|
the ones on the server-side.
|