From 02d378dedfae3129b996f74000d0834376b7a7b5 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sat, 24 Dec 2016 11:12:50 -0500 Subject: [PATCH] Scalable? --- bin/cluster.dart | 11 +++++++++ bin/common.dart | 34 ++++++++++++++++++++++++++++ bin/server.dart | 25 ++------------------ lib/src/routes/controllers/auth.dart | 6 ++--- 4 files changed, 50 insertions(+), 26 deletions(-) create mode 100644 bin/cluster.dart create mode 100644 bin/common.dart diff --git a/bin/cluster.dart b/bin/cluster.dart new file mode 100644 index 0000000..8403f8c --- /dev/null +++ b/bin/cluster.dart @@ -0,0 +1,11 @@ +/// This should be used with `multiserver` as an entry +/// point for `spawnIsolates`. +library angel.cluster; + +import 'dart:async'; +import 'common.dart'; + +main() async { + runZoned(startServer(clustered: true), onError: onError); +} + diff --git a/bin/common.dart b/bin/common.dart new file mode 100644 index 0000000..6c59228 --- /dev/null +++ b/bin/common.dart @@ -0,0 +1,34 @@ +import 'dart:io'; +import 'dart:isolate'; +import 'package:angel/angel.dart'; +import 'package:angel_diagnostics/angel_diagnostics.dart'; +import 'package:intl/intl.dart'; + +startServer({bool clustered: false}) { + return (args, [SendPort sendPort]) async { + var app = await createServer(); + var dateFormat = new DateFormat("y-MM-dd"); + var logFile = new File("logs/${dateFormat.format(new DateTime.now())}.txt"); + InternetAddress host; + int port; + + if (!clustered) { + host = new InternetAddress(app.properties['host']); + port = app.properties['port']; + } else { + host = InternetAddress.LOOPBACK_IP_V4; + port = 0; + } + + var server = + await new DiagnosticsServer(app, logFile).startServer(host, port); + sendPort?.send([server.address.address, server.port]); + }; +} + +onError(error, [StackTrace stackTrace]) { + stderr.writeln("Unhandled error occurred: $error"); + if (stackTrace != null) { + stderr.writeln(stackTrace); + } +} diff --git a/bin/server.dart b/bin/server.dart index 48d5fdb..fd46b52 100755 --- a/bin/server.dart +++ b/bin/server.dart @@ -1,28 +1,7 @@ #!/usr/bin/env dart - import 'dart:async'; -import 'dart:io'; -import 'package:angel/angel.dart'; -import 'package:angel_diagnostics/angel_diagnostics.dart'; -import 'package:intl/intl.dart'; +import 'common.dart'; main() async { - runZoned(startServer, onError: onError); -} - -startServer() async { - var app = await createServer(); - var dateFormat = new DateFormat("y-MM-dd"); - var logFile = new File("logs/${dateFormat.format(new DateTime.now())}.txt"); - var host = new InternetAddress(app.properties['host']); - var port = app.properties['port']; - - await new DiagnosticsServer(app, logFile).startServer(host, port); -} - -onError(error, [StackTrace stackTrace]) { - stderr.writeln("Unhandled error occurred: $error"); - if (stackTrace != null) { - stderr.writeln(stackTrace); - } + runZoned(startServer(), onError: onError); } diff --git a/lib/src/routes/controllers/auth.dart b/lib/src/routes/controllers/auth.dart index 3fdca86..292590b 100644 --- a/lib/src/routes/controllers/auth.dart +++ b/lib/src/routes/controllers/auth.dart @@ -28,9 +28,9 @@ class AuthController extends Controller { // Wire up local authentication, connected to our User service auth = new AngelAuth(jwtKey: app.jwt_secret) ..serializer = serializer - ..deserializer = deserializer; - ..strategies - .add(new LocalAuthStrategy(verifier(app.container.make(UserService)))); + ..deserializer = deserializer + ..strategies.add( + new LocalAuthStrategy(verifier(app.container.make(UserService)))); await super.call(app); await app.configure(auth);