diff --git a/bin/common.dart b/bin/common.dart index 683a997..0dee71d 100644 --- a/bin/common.dart +++ b/bin/common.dart @@ -20,9 +20,13 @@ startServer(args, {bool clustered: false, SendPort sendPort}) { port = 0; } - var server = - await new DiagnosticsServer(app, logFile).startServer(host, port); - sendPort?.send([server.address.address, server.port]); + await app.configure(logRequests(logFile)); + var server = await app.startServer(host, port); + + if (sendPort == null) { + print('Listening at http://${server.address.address}:${server.port}'); + } else + sendPort?.send([server.address.address, server.port]); }; } diff --git a/bin/multi_server.dart b/bin/multi_server.dart new file mode 100644 index 0000000..a227459 --- /dev/null +++ b/bin/multi_server.dart @@ -0,0 +1,36 @@ +#!/usr/bin/env dart +/// This is intended to replace Nginx in your web stack. +/// Either use this or another reverse proxy, but there is no real +/// response to use them together. +library angel.multiserver; + +import 'dart:io'; +import 'package:angel_compress/angel_compress.dart'; +import 'package:angel_multiserver/angel_multiserver.dart'; + +final Uri cluster = Platform.script.resolve('cluster.dart'); + +main() async { + var app = new LoadBalancer(); + // Or, for SSL: + // var app = new LoadBalancer.secure('', ''); + + // Response compression! + app.responseFinalizers.add(gzip()); + + // Cache static assets - just to lower response time + await app.configure(cacheResponses(filters: [new RegExp('images/\.*')])); + + // Start up 5 instances of our main application + await app.spawnIsolates(cluster, count: 5); + + app.onCrash.listen((_) async { + // Boot up a new instance on crash + await app.spawnIsolates(cluster); + }); + + var host = InternetAddress.ANY_IP_V4; + var port = 80; + var server = await app.startServer(host, port); + print('Listening at http://${server.address.address}:${server.port}'); +} diff --git a/lib/src/routes/routes.dart b/lib/src/routes/routes.dart index 0b42018..aa9b14b 100644 --- a/lib/src/routes/routes.dart +++ b/lib/src/routes/routes.dart @@ -46,7 +46,10 @@ configureAfter(Angel app) async { await app.configure(errors); // Compress via GZIP - app.responseFinalizers.add(gzip()); + // Ideally you'll run this on a `multiserver` instance, but if not, + // feel free to knock yourself out! + // + // app.responseFinalizers.add(gzip()); } configureServer(Angel app) async { diff --git a/lib/src/services/user.dart b/lib/src/services/user.dart index 8f44977..e1e1ad3 100644 --- a/lib/src/services/user.dart +++ b/lib/src/services/user.dart @@ -33,7 +33,7 @@ class UserService extends Service { index([Map params]) { if (params != null && params.containsKey('provider')) { // Nobody needs to see the entire user list except for the server. - throw new AngelHttpException.Forbidden(); + throw new AngelHttpException.forbidden(); } return _inner.index(params); @@ -43,7 +43,7 @@ class UserService extends Service { create(data, [Map params]) { if (params != null && params.containsKey('provider')) { // Deny creating users to the public - this should be done by the server only. - throw new AngelHttpException.Forbidden(); + throw new AngelHttpException.forbidden(); } try { @@ -52,7 +52,7 @@ class UserService extends Service { Validate.isEmail(data['email']); data['password'] = hashPassword(data['password']); } catch (e) { - throw new AngelHttpException.BadRequest( + throw new AngelHttpException.badRequest( message: 'User must have a username, e-mail address and password.'); }