2018-06-05 23:51:20 +00:00
|
|
|
import 'dart:io' hide FileMode;
|
2017-10-19 21:53:33 +00:00
|
|
|
import 'dart:isolate';
|
|
|
|
import 'package:angel/angel.dart';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
2018-06-05 23:51:20 +00:00
|
|
|
import 'package:dart2_constant/io.dart';
|
2017-10-19 21:53:33 +00:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
|
|
|
const String hostname = '127.0.0.1';
|
|
|
|
const int port = 3000;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
// Start a server instance in multiple isolates.
|
|
|
|
|
|
|
|
for (int id = 0; id < Platform.numberOfProcessors; id++)
|
|
|
|
Isolate.spawn(isolateMain, id);
|
|
|
|
|
|
|
|
isolateMain(Platform.numberOfProcessors);
|
|
|
|
}
|
|
|
|
|
|
|
|
void isolateMain(int id) {
|
2018-03-29 23:55:28 +00:00
|
|
|
var app = new Angel();
|
2017-10-19 21:53:33 +00:00
|
|
|
|
|
|
|
app.configure(configureServer).then((_) async {
|
|
|
|
// In production, we'll want to log errors to a file.
|
|
|
|
// Alternatives include sending logs to a service like Sentry.
|
|
|
|
app.logger = new Logger('angel')
|
|
|
|
..onRecord.listen((rec) {
|
|
|
|
if (rec.error != null) {
|
2018-06-05 23:51:20 +00:00
|
|
|
var err = rec.error;
|
|
|
|
if (err is AngelHttpException && err.statusCode != 500) return;
|
|
|
|
var now = new DateTime.now().toUtc();
|
|
|
|
var filename = 'server_log_' + now.toString() + '.txt';
|
|
|
|
var sink = new File(filename).openWrite(mode: FileMode.append);
|
2017-10-19 21:53:33 +00:00
|
|
|
sink
|
|
|
|
..writeln(rec.error)
|
|
|
|
..writeln(rec.stackTrace)
|
|
|
|
..close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-29 23:55:28 +00:00
|
|
|
// Passing `startShared` to the constructor allows us to start multiple
|
|
|
|
// instances of our application concurrently, listening on a single port.
|
|
|
|
//
|
|
|
|
// This effectively lets us multi-thread the application.
|
|
|
|
var http = new AngelHttp.custom(app, startShared);
|
|
|
|
var server = await http.startServer(hostname, port);
|
2017-10-19 21:53:33 +00:00
|
|
|
print(
|
|
|
|
'Instance #$id listening at http://${server.address.address}:${server.port}');
|
|
|
|
});
|
|
|
|
}
|