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
2023-11-13 00:59:18 +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:
2023-05-26 23:42:46 +00:00
angel3_framework: ^8.0.0
angel3_hot: ^8.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
2023-11-13 00:59:18 +00:00
This package is dependent on the Dart VM service, so you *must* run Dart with the `--observe` (or `--enable-vm-service` ) argument. 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 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
```
2023-11-13 02:08:33 +00:00
## Customising Response Header
The following code snippet removes `X-FRAME-OPTIONS` and adds `X-XSRF-TOKEN` to the response header.
```dart
import 'dart:io';
import 'package:angel3_hot/angel3_hot.dart';
import 'server.dart';
void main() async {
var hot = HotReloader(createServer, [
Directory('src'),
'server.dart',
// Also allowed: Platform.script,
Uri.parse('package:angel3_hot/angel3_hot.dart')
]);
var http = await hot.startServer('127.0.0.1', 3000);
// Remove 'X-FRAME-OPTIONS'
http.defaultResponseHeaders.remove('X-FRAME-OPTIONS', 'SAMEORIGIN');
2023-11-13 02:20:29 +00:00
// Add 'X-XSRF-TOKEN'
2023-11-13 02:08:33 +00:00
http.defaultResponseHeaders.add('X-XSRF-TOKEN',
'a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e');
}
```