protevus/bin/multi_server.dart

44 lines
1.4 KiB
Dart
Raw Normal View History

2017-01-14 23:52:25 +00:00
#!/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
2017-01-14 23:52:57 +00:00
/// reason to use them together.
///
/// In most cases, you should run `scaled_server.dart` instead of this file.
library angel.multi_server;
2017-01-14 23:52:25 +00:00
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');
/// The number of isolates to spawn. You might consider starting one instance
/// per processor core on your machine.
final int nInstances = Platform.numberOfProcessors;
2017-01-14 23:52:25 +00:00
main() async {
var app = new LoadBalancer();
// Or, for SSL:
// var app = new LoadBalancer.secure('<server-chain>', '<server-key>');
// Response compression!
app.responseFinalizers.add(gzip());
// Cache static assets - just to lower response time
2017-06-10 18:04:32 +00:00
await app.configure(cacheResponses(filters: [new RegExp(r'images/.*')]));
2017-01-14 23:52:25 +00:00
// Start up multiple instances of our main application.
await app.spawnIsolates(cluster, count: nInstances);
2017-01-14 23:52:25 +00:00
app.onCrash.listen((_) async {
// Boot up a new instance on crash
await app.spawnIsolates(cluster);
});
var host = InternetAddress.ANY_IP_V4;
2017-01-15 00:25:33 +00:00
var port = 3000;
2017-01-14 23:52:25 +00:00
var server = await app.startServer(host, port);
print('Listening at http://${server.address.address}:${server.port}');
print('Load-balancing $nInstances instance(s)');
2017-01-14 23:52:25 +00:00
}