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';
|
|
|
|
|
2017-06-10 15:24:36 +00:00
|
|
|
/// Start a single instance of this application.
|
|
|
|
///
|
|
|
|
/// If a [sendPort] is provided, then the URL of the mounted server will be sent through the port.
|
|
|
|
/// Use this if you are starting multiple instances of your server.
|
|
|
|
startServer(args, {SendPort sendPort}) {
|
2016-12-24 16:16:54 +00:00
|
|
|
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;
|
|
|
|
|
2017-06-10 15:24:36 +00:00
|
|
|
// Load the right host and port from application config.
|
|
|
|
host = new InternetAddress(app.properties['host']);
|
|
|
|
|
|
|
|
// Listen on port 0 if we are using the load balancer.
|
|
|
|
port = sendPort != null ? 0 : app.properties['port'];
|
2016-12-24 16:12:50 +00:00
|
|
|
|
2017-06-10 15:24:36 +00:00
|
|
|
// Log requests and errors to a log file.
|
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.
|
2017-06-10 15:24:36 +00:00
|
|
|
//
|
|
|
|
// With hot-reloading, our server will automatically reload in-place on file changes,
|
|
|
|
// for a faster development cycle. :)
|
2017-06-06 12:30:13 +00:00
|
|
|
if (Platform.environment['ANGEL_ENV'] == 'production')
|
|
|
|
server = await app.startServer(host, port);
|
|
|
|
else {
|
2017-06-06 13:16:01 +00:00
|
|
|
var hot = new HotReloader(() async {
|
2017-06-10 15:24:36 +00:00
|
|
|
// If we are hot-reloading, we need to provide a callback
|
|
|
|
// to use to start a fresh instance on-the-fly.
|
2017-06-06 13:16:01 +00:00
|
|
|
var app = await createServer();
|
|
|
|
await app.configure(logRequests(logFile));
|
|
|
|
return app;
|
2017-06-10 15:24:36 +00:00
|
|
|
},
|
|
|
|
// Paths we might want to listen for changes on...
|
|
|
|
[
|
|
|
|
new Directory('config'),
|
|
|
|
new Directory('lib'),
|
|
|
|
new Directory('views')
|
|
|
|
]);
|
2017-06-06 12:30:13 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|