platform/packages/production
thomashii@dukefirehawk.com 85a5c45a13 Updated response header
2023-11-13 09:02:46 +08:00
..
example Updated framework and production 2023-11-13 08:59:18 +08:00
lib Updated response header 2023-11-13 09:02:46 +08:00
.pubignore Removed dev.pem and dev.key 2021-12-19 10:21:21 +08:00
analysis_options.yaml Updated pkg:angel3_production to use lints 2021-09-11 08:41:00 +08:00
angel3-screenshot.png Updated production 2021-07-18 13:36:33 +08:00
AUTHORS.md Updated authors and license 2021-05-15 16:48:20 +08:00
CHANGELOG.md Updated framework and production 2023-11-13 08:59:18 +08:00
LICENSE Updated pkg:angel3_production to use lints 2021-09-11 08:41:00 +08:00
melos_angel3_production.iml Added melos 2022-03-19 09:37:28 +08:00
pubspec.yaml Updated framework and production 2023-11-13 08:59:18 +08:00
README.md Updated framework and production 2023-11-13 08:59:18 +08:00
screenshot.png Add 'packages/production/' from commit '5ac875c504cd98a5075e0316da9c153b313fd3f9' 2020-02-15 18:28:31 -05:00

Angel3 Production Runner

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

Helpers for concurrency, message-passing, rotating loggers, and other production functionality in Angel3 framework.

Screenshot

This will become the de-facto way to run Angel3 applications in deployed environments, as it takes care of inter-isolate communication, respawning dead processes, and other housekeeping for you automatically. Most users will want to use the Runner class.

Runner

Runner is a utility, powered by package:args, that is intended to be the entry point of your application.

Instantiate it as follows, and your file will become a command-line executable that spawns multiple instances of your application:

import 'dart:async';
import 'dart:isolate';
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_production/angel3_production.dart';

void main(List<String> args) => Runner('example', configureServer).run(args);

Future configureServer(Angel app) async {
  app.get('/', (req, res) => 'Hello, production world!');

  app.get('/crash', (req, res) {
    // We'll crash this instance deliberately, but the Runner will auto-respawn for us.
    Timer(const Duration(seconds: 3), Isolate.current.kill);
    return 'Crashing in 3s...';
  });
}

Runner will automatically re-spawn crashed instances, unless --no-respawn is passed. This can prevent your server from entirely going down at the first error, and adds a layer of fault tolerance to your infrastructure.

When combined with systemd, deploying Angel3 applications on Linux can be very simple.

Message Passing

The Runner class uses belatuk_pub_sub to coordinate message passing between isolates. When one isolate sends a message, all other isolates will receive the same message, except for the isolate that sent it. It is injected into your application's Container as pub_sub.Client, so you can use it as follows:

// Use the injected `pub_sub.Client` to send messages.
var client = app.container.make<Client>();

// We can listen for an event to perform some behavior.
//
// Here, we use message passing to synchronize some common state.
var onGreetingChanged = await client.subscribe('user_upgraded');
onGreetingChanged
    .cast<User>()
    .listen((user) {
      // Do something...
    });

Customising Response Header

Additional parameters can be passed to the Runner class to:

  1. Remove headers from HTTP response.
  2. Add headers to HTTP response.

For example, the following code snippet removes X-FRAME-OPTIONS and adds X-XSRF_TOKEN to the response header.


void main(List<String> args) {
  // Remove default
  var removeHeader = {'X-FRAME-OPTIONS': 'SAMEORIGIN'};
  var customHeader = {
    'X-XSRF_TOKEN':
        'a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e'
  };

  Runner('example', configureServer,
          removeResponseHeaders: removeHeader, responseHeaders: customHeader)
      .run(args);
}

Run-time Metadata

At run-time, you may want to know information about the currently-running instance, for example, which number instance. For this, the InstanceInfo class is injected into each instance:

var instanceInfo = app.container.make<InstanceInfo>();
print('This is instance #${instanceInfo.id}');

Command-line Options

The Runner class supplies options like the following:

appuser$ dart example/main.dart --help
     _    _   _  ____ _____ _     _____ 
    / \  | \ | |/ ___| ____| |   |___ / 
   / _ \ |  \| | |  _|  _| | |     |_ \ 
  / ___ \| |\  | |_| | |___| |___ ___) |
 /_/   \_\_| \_|\____|_____|_____|____/                                                                                 

A batteries-included, full-featured, full-stack framework in Dart.

https://angel3-framework.web.app

Options:
-h, --help                    Print this help information.
    --[no-]respawn            Automatically respawn crashed application instances.
                              (defaults to on)

    --use-zone                Create a new Zone for each request.
    --quiet                   Completely mute logging.
    --ssl                     Listen for HTTPS instead of HTTP.
    --http2                   Listen for HTTP/2 instead of HTTP/1.1.
-a, --address                 The address to listen on.
                              (defaults to "127.0.0.1")

-j, --concurrency             The number of isolates to spawn.
                              (defaults to "4")

-p, --port                    The port to listen on.
                              (defaults to "3000")

    --certificate-file        The PEM certificate file to read.
    --certificate-password    The PEM certificate file password.
    --key-file                The PEM key file to read.
    --key-password            The PEM key file password.