2021-07-18 07:18:31 +00:00
# Angel3 Hot Reloader
2022-01-03 06:45:47 +00:00
![Pub Version (including pre-releases) ](https://img.shields.io/pub/v/angel3_hot?include_prereleases )
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)
2022-01-03 06:45:47 +00:00
[![License ](https://img.shields.io/github/license/dukefirehawk/angel )](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 )
2022-09-18 16:58:28 +00:00
Supports *hot reloading* of Angel3 servers on file changes. This is faster and more reliable than merely reactively restarting a `Process` . 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:
2022-09-18 16:58:28 +00:00
angel3_framework: ^7.0.0
angel3_hot: ^7.0.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';
2021-12-03 19:52:52 +00:00
import 'package:angel3_hot/angel3_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 {
2022-01-03 07:51:05 +00:00
var hot = HotReloader(createServer, [
2022-01-03 06:45:47 +00:00
Directory('src'),
2018-11-02 20:30:08 +00:00
'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
```