platform/packages/hot/README.md

87 lines
2.6 KiB
Markdown
Raw Normal View History

2021-07-18 07:18:31 +00:00
# Angel3 Hot Reloader
[![version](https://img.shields.io/badge/pub-v4.1.1-brightgreen)](https://pub.dartlang.org/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)
[![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/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`.
This package only works with the [Angel3 framework](https://github.com/dukefirehawk/angel).
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-05-15 13:17:18 +00:00
angel3_framework: ^4.0.0
angel3_hot: ^4.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';
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
```