38 lines
1.1 KiB
Dart
38 lines
1.1 KiB
Dart
import 'package:grpc/grpc.dart';
|
||
|
||
import '../gen/notes/v1/notes.pbgrpc.dart';
|
||
|
||
/// Singleton gRPC клиент.
|
||
/// Подключается к Go бекенду на localhost:50051.
|
||
class GrpcClient {
|
||
static GrpcClient? _instance;
|
||
late final ClientChannel _channel;
|
||
late final NoteServiceClient noteService;
|
||
|
||
GrpcClient._() {
|
||
_channel = ClientChannel(
|
||
'localhost',
|
||
port: 50051,
|
||
options: const ChannelOptions(
|
||
credentials: ChannelCredentials.insecure(),
|
||
),
|
||
);
|
||
|
||
// noteService — типизированный клиент.
|
||
// Все методы (createNote, listNotes и т.д.)
|
||
// сгенерированы из proto и принимают/возвращают
|
||
// типизированные объекты. Невозможно передать
|
||
// неверный тип — ошибка компиляции Dart.
|
||
noteService = NoteServiceClient(_channel);
|
||
}
|
||
|
||
static GrpcClient get instance {
|
||
_instance ??= GrpcClient._();
|
||
return _instance!;
|
||
}
|
||
|
||
Future<void> shutdown() async {
|
||
await _channel.shutdown();
|
||
}
|
||
}
|