open method
Opens a connection using the parameters resolved by the referenced connection resolver and creates a REST server (service) using the set options and parameters.
correlationId
(optional) transaction id to trace execution through call chain. Return Future when the opening process is complete. Will be called with an error if one is raised.
Implementation
@override
Future open(String? correlationId) async {
if (isOpen()) {
return null;
}
var connection = await _connectionResolver.resolve(correlationId);
_uri = connection?.getAsString('uri');
var host = connection!.getAsString('host');
var port = connection.getAsInteger('port');
try {
_app = Router();
var startServer;
var serverContext;
if (connection.getAsStringWithDefault('protocol', 'http') == 'https') {
var sslKeyFile = connection.getAsString('ssl_key_file');
var sslCrtFile = connection.getAsString('ssl_crt_file');
var certificateChain = Platform.script.resolve(sslCrtFile).toFilePath();
var serverKey = Platform.script.resolve(sslKeyFile).toFilePath();
serverContext = SecurityContext();
serverContext.useCertificateChain(certificateChain);
serverContext.usePrivateKey(serverKey);
// todo add ssl_ca_file
}
startServer = () async {
_server = await shelf_io.serve(
Pipeline().addMiddleware(_handler()).addHandler(_app!), host, port,
securityContext: serverContext);
};
_performRegistrations();
await startServer();
await _connectionResolver.register(correlationId);
_logger.debug(correlationId, 'Opened REST service at %s', [_uri]);
} catch (ex) {
_server = null;
_app = null;
print(ex);
var err = ConnectionException(
correlationId, 'CANNOT_CONNECT', 'Opening REST service failed')
.wrap(ex)
.withDetails('url', _uri);
throw err;
}
}