Initial commit
This commit is contained in:
37
frontend/lib/services/grpc_client.dart
Normal file
37
frontend/lib/services/grpc_client.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user