platform/README.md

100 lines
2.7 KiB
Markdown
Raw Normal View History

2016-04-22 01:17:31 +00:00
# Angel 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)
[![build status](https://travis-ci.org/angel-dart/configuration.svg)](https://travis-ci.org/angel-dart/configuration.svg)
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-06-14 18:48:13 +00:00
for a `config/default.yaml` file. If it is found, configuration from it is loaded into `app.properties`.
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-06-14 18:48:13 +00:00
Call `loadConfigurationFile()`. The loaded properties will be available in your application's
`properties` map, which means you can access them like normal instance members.
```dart
main() {
print(app.foo == app.properties['foo']); // true
}
```
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';
main() async {
Angel angel = new Angel();
2016-09-19 20:31:32 +00:00
angel.configure(loadConfigurationFile()); // 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
}
```
`loadConfigurationFile` also accepts a `sourceDirectory` or `overrideEnvironmentName` parameter.
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
**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.