2021-07-18 07:18:31 +00:00
# Angel3 Hot Reloader
2021-09-12 04:54:37 +00:00
[![version ](https://img.shields.io/badge/pub-v4.2.1-brightgreen )](https://pub.dev/packages/angel3_hot)
2021-05-15 13:17:18 +00:00
[![Null Safety ](https://img.shields.io/badge/null-safety-brightgreen )](https://dart.dev/null-safety)
[![Gitter ](https://img.shields.io/gitter/room/angel_dart/discussion )](https://gitter.im/angel_dart/discussion)
2021-11-15 23:49:11 +00:00
[![License ](https://img.shields.io/github/license/dart-backend/belatuk-common-utilities )](https://github.com/dukefirehawk/angel/tree/master/packages/hot/LICENSE)
2017-06-06 12:07:59 +00:00
2021-07-18 07:18:31 +00:00
![Screenshot of terminal ](screenshots/angel3-screenshot.png )
Supports *hot reloading* of Angel3 servers on file changes. This is faster and more reliable than merely reactively restarting a `Process` .
2021-09-11 03:18:35 +00:00
This package only works with the [Angel3 framework ](https://pub.dev/packages/angel3_framework ).
2018-10-02 16:13:47 +00:00
2021-07-18 07:18:31 +00:00
**Not recommended to use in production, unless you are specifically intending for a "hot code push" in production..**
2017-06-06 12:07:59 +00:00
2021-07-18 07:18:31 +00:00
## Installation
2017-06-06 12:07:59 +00:00
In your `pubspec.yaml` :
```yaml
dependencies:
2021-09-11 03:18:35 +00:00
angel3_framework: ^4.1.0
angel3_hot: ^4.2.0
2017-06-06 12:07:59 +00:00
```
2021-07-18 07:18:31 +00:00
## Usage
2017-06-06 12:07:59 +00:00
2021-07-18 07:18:31 +00:00
This package is dependent on the Dart VM service, so you *must* run Dart with the `--observe` (or `--enable-vm-service` ) argument!!!
2017-06-06 12:07:59 +00:00
2021-07-18 07:18:31 +00:00
Usage is fairly simple. Pass a function that creates an `Angel` server, along with a collection of paths to watch, to the `HotReloader` constructor. The rest is history!!!
2018-10-02 16:13:47 +00:00
2021-07-18 07:18:31 +00:00
The recommended pattern is to only use hot-reloading in your application entry point. Create your `Angel` instance within a separate function, conventionally named `createServer` .
2017-06-06 12:07:59 +00:00
You can watch:
2021-07-18 07:18:31 +00:00
* Files
* Directories
* Globs
* URI's
* `package:` URI's
2017-06-06 12:07:59 +00:00
```dart
import 'dart:async';
import 'dart:convert';
import 'dart:io';
2021-07-18 07:18:31 +00:00
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_hot/angel_hot.dart';
2018-11-02 20:30:08 +00:00
import 'package:logging/logging.dart';
2017-06-06 12:07:59 +00:00
import 'src/foo.dart';
main() async {
var hot = new HotReloader(createServer, [
new Directory('src'),
2018-11-02 20:30:08 +00:00
new Directory('src'),
'main.dart',
2021-07-18 07:18:31 +00:00
Uri.parse('package:angel3_hot/angel3_hot.dart')
2017-06-06 12:07:59 +00:00
]);
2018-11-02 20:30:08 +00:00
await hot.startServer('127.0.0.1', 3000);
2017-06-06 12:07:59 +00:00
}
Future< Angel > createServer() async {
2021-07-18 07:18:31 +00:00
var app = Angel()..serializer = json.encode;
2017-06-06 12:07:59 +00:00
2018-11-02 20:30:08 +00:00
// Edit this line, and then refresh the page in your browser!
2018-11-02 20:27:05 +00:00
app.get('/', (req, res) => {'hello': 'hot world!'});
2021-07-18 07:18:31 +00:00
app.get('/foo', (req, res) => Foo(bar: 'baz'));
2017-06-06 12:07:59 +00:00
2021-07-18 07:18:31 +00:00
app.fallback((req, res) => throw AngelHttpException.notFound());
2018-11-02 20:30:08 +00:00
app.encoders.addAll({
'gzip': gzip.encoder,
'deflate': zlib.encoder,
});
2021-07-18 07:18:31 +00:00
app.logger = Logger('angel')
2018-11-02 20:30:08 +00:00
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) {
print(rec.error);
print(rec.stackTrace);
}
});
2017-06-06 12:07:59 +00:00
return app;
}
2018-11-02 20:27:05 +00:00
```