platform/packages/hot
2024-10-12 19:17:24 -07:00
..
example Refactor: changing namespace, imports, re-branding 2024-10-12 18:45:27 -07:00
lib Refactor: changing namespace, imports, re-branding 2024-10-12 18:45:27 -07:00
screenshots Updated hot reloader 2021-07-18 15:18:31 +08:00
.gitignore Publish angel3_configuration 2021-05-14 19:27:54 +08:00
analysis_options.yaml Updated hot 2021-09-11 11:18:35 +08:00
AUTHORS.md Publish angel3_configuration 2021-05-14 19:27:54 +08:00
CHANGELOG.md Refactor: changing namespace, imports, re-branding 2024-10-12 03:41:18 -07:00
LICENSE Updated hot 2021-09-11 11:18:35 +08:00
pubspec.yaml Refactor: changing namespace, imports, re-branding 2024-10-12 18:45:27 -07:00
README.md Refactor: changing namespace, imports, re-branding 2024-10-12 19:17:24 -07:00

Protevus Hot Reloader

Pub Version (including pre-releases) Null Safety Discord License

Screenshot of terminal

Supports hot reloading of Protevus servers on file changes. This is faster and more reliable than merely reactively restarting a Process. This package only works with the Protevus framework.

Not recommended to use in production, unless you are specifically intending for a "hot code push" in production.

Installation

In your pubspec.yaml:

dependencies:
  protevus_framework: ^8.0.0
  protevus_hot: ^8.0.0

Usage

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 Protevus 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 Protevus instance within a separate function, conventionally named createServer.

You can watch:

  • Files
  • Directories
  • Globs
  • URI's
  • package: URI's
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:protevus_framework/protevus_framework.dart';
import 'package:protevus_hot/protevus_hot.dart';
import 'package:logging/logging.dart';
import 'src/foo.dart';

main() async {
  var hot = HotReloader(createServer, [
    Directory('src'),
    'main.dart',
    Uri.parse('package:protevus_hot/protevus_hot.dart')
  ]);
  await hot.startServer('127.0.0.1', 3000);
}

Future<Protevus> createServer() async {
  var app = Protevus()..serializer = json.encode;

  // Edit this line, and then refresh the page in your browser!
  app.get('/', (req, res) => {'hello': 'hot world!'});
  app.get('/foo', (req, res) => Foo(bar: 'baz'));

  app.fallback((req, res) => throw ProtevusHttpException.notFound());

  app.encoders.addAll({
    'gzip': gzip.encoder,
    'deflate': zlib.encoder,
  });

  app.logger = Logger('protevus')
    ..onRecord.listen((rec) {
      print(rec);
      if (rec.error != null) {
        print(rec.error);
        print(rec.stackTrace);
      }
    });

  return app;
}

Customising Response Header

The following code snippet removes X-FRAME-OPTIONS and adds X-XSRF-TOKEN to the response header.

import 'dart:io';
import 'package:protevus_hot/protevus_hot.dart';
import 'server.dart';

void main() async {
  var hot = HotReloader(createServer, [
    Directory('src'),
    'server.dart',
    // Also allowed: Platform.script,
    Uri.parse('package:protevus_hot/protevus_hot.dart')
  ]);
  var http = await hot.startServer('127.0.0.1', 3000);

  // Remove 'X-FRAME-OPTIONS'
  http.defaultResponseHeaders.remove('X-FRAME-OPTIONS', 'SAMEORIGIN');

  // Add 'X-XSRF-TOKEN'
  http.defaultResponseHeaders.add('X-XSRF-TOKEN',
      'a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e');
}