From b091b435e4471b1e7e368b062a84450174654523 Mon Sep 17 00:00:00 2001 From: "thomashii@dukefirehawk.com" Date: Sat, 1 May 2021 10:03:46 +0800 Subject: [PATCH] Migrated production --- CHANGELOG.md | 4 +- packages/production/example/main.dart | 8 +-- .../production/lib/src/instance_info.dart | 2 +- packages/production/lib/src/options.dart | 24 +++---- packages/production/lib/src/runner.dart | 62 ++++++++++--------- packages/production/pubspec.yaml | 18 +++--- 6 files changed, 61 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25e98b6e..68872a60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,8 +22,8 @@ * Migrated jael to 4.0.0 (20/20 tests passed) * Migrated jael_preprocessor to 3.0.0 (5/5 tests passed) * Migrated angel_jael to 4.0.0 (1/1 test passed) -* Updated pub_sub to 4.0.0 (16/16 tests passed) -* Updated production to 2.0.0 (in progress) +* Migrated pub_sub to 4.0.0 (16/16 tests passed) +* Migrated production to 3.0.0 (0/0 tests passed) * Updated hot to 3.0.0 (in progress) * Updated static to 3.0.0 (in progress) * Update basic-sdk-2.12.x boilerplate (in progress) diff --git a/packages/production/example/main.dart b/packages/production/example/main.dart index 30164481..0dc2a115 100644 --- a/packages/production/example/main.dart +++ b/packages/production/example/main.dart @@ -4,12 +4,12 @@ import 'package:angel_framework/angel_framework.dart'; import 'package:angel_production/angel_production.dart'; import 'package:pub_sub/pub_sub.dart' as pub_sub; -main(List args) => Runner('example', configureServer).run(args); +void main(List args) => Runner('example', configureServer).run(args); Future configureServer(Angel app) async { // Use the injected `pub_sub.Client` to send messages. - var client = app.container.make(); - var greeting = 'Hello! This is the default greeting.'; + var client = app.container!.make()!; + String? greeting = 'Hello! This is the default greeting.'; // We can listen for an event to perform some behavior. // @@ -32,7 +32,7 @@ Future configureServer(Angel app) async { // This route will push a new value for `greeting`. app.get('/change_greeting/:newGreeting', (req, res) { - greeting = req.params['newGreeting'] as String; + greeting = req.params['newGreeting'] as String?; client.publish('greeting_changed', greeting); return 'Changed greeting -> $greeting'; }); diff --git a/packages/production/lib/src/instance_info.dart b/packages/production/lib/src/instance_info.dart index 07aec844..1564ea5b 100644 --- a/packages/production/lib/src/instance_info.dart +++ b/packages/production/lib/src/instance_info.dart @@ -1,6 +1,6 @@ /// Information about the currently-running instance. class InstanceInfo { - final int id; + final int? id; const InstanceInfo({this.id}); } diff --git a/packages/production/lib/src/options.dart b/packages/production/lib/src/options.dart index 8f3c1892..8d3fb40a 100644 --- a/packages/production/lib/src/options.dart +++ b/packages/production/lib/src/options.dart @@ -30,13 +30,13 @@ class RunnerOptions { ..addOption('key-file', help: 'The PEM key file to read.') ..addOption('key-password', help: 'The PEM key file password.'); - final String hostname, + final String? hostname, certificateFile, keyFile, certificatePassword, keyPassword; final int concurrency, port; - final bool useZone, respawn, quiet, ssl, http2; + final bool? useZone, respawn, quiet, ssl, http2; RunnerOptions( {this.hostname = '127.0.0.1', @@ -54,25 +54,25 @@ class RunnerOptions { factory RunnerOptions.fromArgResults(ArgResults argResults) { return RunnerOptions( - hostname: argResults['address'] as String, + hostname: argResults['address'] as String?, port: int.parse(argResults['port'] as String), concurrency: int.parse(argResults['concurrency'] as String), - useZone: argResults['use-zone'] as bool, - respawn: argResults['respawn'] as bool, - quiet: argResults['quiet'] as bool, + useZone: argResults['use-zone'] as bool?, + respawn: argResults['respawn'] as bool?, + quiet: argResults['quiet'] as bool?, certificateFile: argResults.wasParsed('certificate-file') - ? argResults['certificate-file'] as String + ? argResults['certificate-file'] as String? : null, keyFile: argResults.wasParsed('key-file') - ? argResults['key-file'] as String + ? argResults['key-file'] as String? : null, - ssl: argResults['ssl'] as bool, - http2: argResults['http2'] as bool, + ssl: argResults['ssl'] as bool?, + http2: argResults['http2'] as bool?, certificatePassword: argResults.wasParsed('certificate-password') - ? argResults['certificate-password'] as String + ? argResults['certificate-password'] as String? : null, keyPassword: argResults.wasParsed('key-password') - ? argResults['key-password'] as String + ? argResults['key-password'] as String? : null, ); } diff --git a/packages/production/lib/src/runner.dart b/packages/production/lib/src/runner.dart index 48e5ae32..adbd242e 100644 --- a/packages/production/lib/src/runner.dart +++ b/packages/production/lib/src/runner.dart @@ -35,9 +35,9 @@ _ ___ | /| / / /_/ / _ /___ _ /___ '''; - static void handleLogRecord(LogRecord record, RunnerOptions options) { - if (options.quiet) return; - var code = chooseLogColor(record.level); + static void handleLogRecord(LogRecord? record, RunnerOptions options) { + if (options.quiet!) return; + var code = chooseLogColor(record!.level); if (record.error == null) print(code.wrap(record.toString())); @@ -55,15 +55,17 @@ _ ___ | /| / / /_/ / _ /___ _ /___ /// Chooses a color based on the logger [level]. static AnsiCode chooseLogColor(Level level) { - if (level == Level.SHOUT) + if (level == Level.SHOUT) { return backgroundRed; - else if (level == Level.SEVERE) + } else if (level == Level.SEVERE) { return red; - else if (level == Level.WARNING) + } else if (level == Level.WARNING) { return yellow; - else if (level == Level.INFO) + } else if (level == Level.INFO) { return cyan; - else if (level == Level.FINER || level == Level.FINEST) return lightGray; + } else if (level == Level.FINER || level == Level.FINEST) { + return lightGray; + } return resetAll; } @@ -94,11 +96,12 @@ _ ___ | /| / / /_/ / _ /___ _ /___ .then((isolate) {}) .catchError(c.completeError); - onLogRecord.listen((msg) => handleLogRecord(msg as LogRecord, options)); + onLogRecord.listen((msg) => handleLogRecord(msg as LogRecord?, options)); onError.listen((msg) { if (msg is List) { - var e = msg[0], st = StackTrace.fromString(msg[1].toString()); + dynamic e = msg[0]; + var st = StackTrace.fromString(msg[1].toString()); handleLogRecord( LogRecord( Level.SEVERE, 'Fatal error', runnerArgs.loggerName, e, st), @@ -111,7 +114,7 @@ _ ___ | /| / / /_/ / _ /___ _ /___ }); onExit.listen((_) { - if (options.respawn) { + if (options.respawn!) { handleLogRecord( LogRecord( Level.WARNING, @@ -132,13 +135,13 @@ _ ___ | /| / / /_/ / _ /___ _ /___ /// Starts a number of isolates, running identical instances of an Angel application. Future run(List args) async { - pub_sub.Server server; + late pub_sub.Server server; try { var argResults = RunnerOptions.argParser.parse(args); var options = RunnerOptions.fromArgResults(argResults); - if (options.ssl || options.http2) { + if (options.ssl! || options.http2!) { if (options.certificateFile == null) { throw ArgParserException('Missing --certificate-file option.'); } else if (options.keyFile == null) { @@ -148,7 +151,7 @@ _ ___ | /| / / /_/ / _ /___ _ /___ print(darkGray.wrap(asciiArt.trim() + '\n\n' + - "A batteries-included, full-featured, full-stack framework in Dart." + + 'A batteries-included, full-featured, full-stack framework in Dart.' + '\n\n' + 'https://angel-dart.github.io\n')); @@ -157,13 +160,13 @@ _ ___ | /| / / /_/ / _ /___ _ /___ return; } - print('Starting `${name}` application...'); + print('Starting `$name` application...'); var adapter = pub_sub.IsolateAdapter(); server = pub_sub.Server([adapter]); // Register clients - for (int i = 0; i < Platform.numberOfProcessors; i++) { + for (var i = 0; i < Platform.numberOfProcessors; i++) { server.registerClient(pub_sub.ClientInfo('client$i')); } @@ -184,7 +187,7 @@ _ ___ | /| / / /_/ / _ /___ _ /___ ..writeln(red.wrap(st.toString())); exitCode = 1; } finally { - await server?.close(); + await server.close(); } } @@ -203,8 +206,8 @@ _ ___ | /| / / /_/ / _ /___ _ /___ pub_sub.IsolateClient('client${argsWithId.id}', args.pubSubSendPort); var app = Angel(reflector: args.reflector) - ..container.registerSingleton(client) - ..container.registerSingleton(InstanceInfo(id: argsWithId.id)); + ..container!.registerSingleton(client) + ..container!.registerSingleton(InstanceInfo(id: argsWithId.id)); app.shutdownHooks.add((_) => client.close()); @@ -216,31 +219,31 @@ _ ___ | /| / / /_/ / _ /___ _ /___ } AngelHttp http; - SecurityContext securityContext; + late SecurityContext securityContext; Uri serverUrl; - if (args.options.ssl || args.options.http2) { + if (args.options.ssl! || args.options.http2!) { securityContext = SecurityContext(); - securityContext.useCertificateChain(args.options.certificateFile, + securityContext.useCertificateChain(args.options.certificateFile!, password: args.options.certificatePassword); - securityContext.usePrivateKey(args.options.keyFile, + securityContext.usePrivateKey(args.options.keyFile!, password: args.options.keyPassword); } - if (args.options.ssl) { + if (args.options.ssl!) { http = AngelHttp.custom(app, startSharedSecure(securityContext), - useZone: args.options.useZone); + useZone: args.options.useZone!); } else { http = - AngelHttp.custom(app, startShared, useZone: args.options.useZone); + AngelHttp.custom(app, startShared, useZone: args.options.useZone!); } Driver driver; - if (args.options.http2) { + if (args.options.http2!) { securityContext.setAlpnProtocols(['h2'], true); var http2 = AngelHttp2.custom(app, securityContext, startSharedHttp2, - useZone: args.options.useZone); + useZone: args.options.useZone!); http2.onHttp1.listen(http.handleRequest); driver = http2; } else { @@ -249,8 +252,9 @@ _ ___ | /| / / /_/ / _ /___ _ /___ await driver.startServer(args.options.hostname, args.options.port); serverUrl = driver.uri; - if (args.options.ssl || args.options.http2) + if (args.options.ssl! || args.options.http2!) { serverUrl = serverUrl.replace(scheme: 'https'); + } print('Instance #${argsWithId.id} listening at $serverUrl'); }); } diff --git a/packages/production/pubspec.yaml b/packages/production/pubspec.yaml index 2b983a2a..6066a898 100644 --- a/packages/production/pubspec.yaml +++ b/packages/production/pubspec.yaml @@ -1,29 +1,29 @@ name: angel_production -version: 2.0.0 +version: 3.0.0 description: Helpers for concurrency, message-passing, rotating loggers, and other production functionality in Angel. author: Tobe O homepage: https://github.com/angel-dart/production publish_to: none environment: - sdk: ">=2.10.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' dependencies: angel_container: git: url: https://github.com/dukefirehawk/angel.git - ref: sdk-2.12.x + ref: sdk-2.12.x_nnbd path: packages/container/angel_container angel_framework: git: url: https://github.com/dukefirehawk/angel.git - ref: sdk-2.12.x + ref: sdk-2.12.x_nnbd path: packages/framework pub_sub: git: url: https://github.com/dukefirehawk/angel.git - ref: sdk-2.12.x + ref: sdk-2.12.x_nnbd path: packages/pub_sub - args: ^2.0.0 - io: ^0.3.5 - logging: ^1.0.0 + args: ^2.1.0 + io: ^1.0.0 + logging: ^1.0.1 dev_dependencies: - pedantic: ^1.0.0 \ No newline at end of file + pedantic: ^1.11.0 \ No newline at end of file