protevus/bin/common.dart

52 lines
1.5 KiB
Dart
Raw Normal View History

2016-12-24 16:12:50 +00:00
import 'dart:io';
import 'dart:isolate';
import 'package:angel/angel.dart';
import 'package:angel_diagnostics/angel_diagnostics.dart';
2017-06-06 12:30:13 +00:00
import 'package:angel_hot/angel_hot.dart';
2016-12-24 16:12:50 +00:00
import 'package:intl/intl.dart';
2016-12-24 16:16:54 +00:00
startServer(args, {bool clustered: false, SendPort sendPort}) {
return () async {
2016-12-24 16:12:50 +00:00
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;
}
2017-01-14 23:52:25 +00:00
await app.configure(logRequests(logFile));
2017-06-06 12:30:13 +00:00
HttpServer server;
// Use `package:angel_hot` in any case, EXCEPT if starting in production mode.
if (Platform.environment['ANGEL_ENV'] == 'production')
server = await app.startServer(host, port);
else {
var hot = new HotReloader(createServer, [
new Directory('bin'),
new Directory('config'),
new Directory('lib')
]);
server = await hot.startServer(host, port);
}
2017-01-14 23:52:25 +00:00
if (sendPort == null) {
print('Listening at http://${server.address.address}:${server.port}');
} else
sendPort?.send([server.address.address, server.port]);
2016-12-24 16:12:50 +00:00
};
}
onError(error, [StackTrace stackTrace]) {
stderr.writeln("Unhandled error occurred: $error");
if (stackTrace != null) {
stderr.writeln(stackTrace);
}
}