open method
Opens a connection using the parameters resolved by the referenced connection resolver and creates a GRPC server (service) using the set options and parameters.
correlationId
(optional) transaction id to trace execution through call chain. Return Future the opening process is complete. Throws 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');
try {
await _connectionResolver.register(correlationId);
// Perform registration for adds all services before create server
_performRegistrations();
if (_interceptors != null && _interceptors.isNotEmpty) {
_server = grpc.Server(_services, _interceptors);
} else {
_server = grpc.Server(_services);
}
if (connection.getAsString('uri') == 'https') {
var sslKeyFile = connection.getAsNullableString('ssl_key_file');
var sslCrtFile = connection.getAsNullableString('ssl_crt_file');
final certificate = File(sslCrtFile!).readAsBytes();
final privateKey = File(sslKeyFile!).readAsBytes();
var tlsCredentials = grpc.ServerTlsCredentials(
certificate: await certificate, privateKey: await privateKey);
await _server!.serve(
address: connection.getAsString('host'),
port: connection.getAsInteger('port'),
security: tlsCredentials);
} else {
await _server!.serve(
address: connection.getAsString('host'),
port: connection.getAsInteger('port'));
}
_logger.debug(correlationId, 'Opened GRPC service at %s', [_uri]);
} catch (ex) {
_server = null;
throw ConnectionException(
correlationId, 'CANNOT_CONNECT', 'Opening GRPC service failed')
.wrap(ex)
.withDetails('url', _uri);
}
}