Skip to content

Commit 647bb03

Browse files
feat(microservices): change the whole approach to grpc microservices
1 parent 3e598b1 commit 647bb03

10 files changed

Lines changed: 225 additions & 326 deletions

File tree

examples/grpc_example/bin/client.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ Future<void> main() async {
2222
]),
2323
);
2424
print('Greeting: ${response.message}');
25+
final bidiResponse = stub.bidiHello(
26+
Stream.fromIterable([
27+
HelloRequest()..name = 'Alice',
28+
HelloRequest()..name = 'Bob',
29+
]),
30+
);
31+
await for (final reply in bidiResponse) {
32+
print('Bidi Greeting: ${reply.message}');
33+
}
34+
final unaryResponse = await stub.sayHello(HelloRequest()..name = 'Charlie');
35+
print('Unary Greeting: ${unaryResponse.message}');
2536
} catch (e) {
2637
print('Caught error: $e');
2738
}

examples/grpc_example/bin/echo.dart

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,41 @@ import 'dart:io';
44
import 'package:grpc/grpc.dart';
55
import 'package:grpc_example/generated/helloworld.pbgrpc.dart';
66
import 'package:serinus/serinus.dart';
7-
import 'package:serinus_microservices/transporters/grpc/grpc_controller.dart';
8-
import 'package:serinus_microservices/transporters/grpc/grpc_transport.dart';
7+
import 'package:serinus_microservices/serinus_microservices.dart';
98

109
class GreeterService extends GreeterServiceBase {
1110
@override
1211
Stream<HelloReply> bidiHello(ServiceCall call, Stream<HelloRequest> request) {
13-
// TODO: implement bidiHello
14-
throw UnimplementedError();
12+
final greeting = call.context.use<GreeterProvider>();
13+
return request.asyncMap((helloRequest) {
14+
final message = greeting.getGreeting(helloRequest.name);
15+
return HelloReply()..message = message;
16+
});
1517
}
1618

1719
@override
1820
Future<HelloReply> lotsOfGreetings(ServiceCall call, Stream<HelloRequest> request) {
19-
// TODO: implement lotsOfGreetings
20-
throw UnimplementedError();
21+
final greeting = call.context.use<GreeterProvider>();
22+
return request
23+
.asyncMap((helloRequest) => greeting.getGreeting(helloRequest.name))
24+
.toList()
25+
.then((greetings) => HelloReply()..message = greetings.join(', '));
2126
}
2227

2328
@override
2429
Future<HelloReply> sayHello(ServiceCall call, HelloRequest request) {
25-
// TODO: implement sayHello
26-
throw UnimplementedError();
30+
final greeting = call.context.use<GreeterProvider>();
31+
final message = greeting.getGreeting(request.name);
32+
return Future.value(HelloReply()..message = message);
2733
}
2834

2935
}
3036

31-
class AppController extends Controller with GrpcController {
37+
class AppController extends GrpcServiceController {
3238

33-
AppController() : super('/') {
34-
grpcStream<HelloRequest, HelloReply>(
35-
GrpcRoute(GreeterService, 'LotsOfGreetings'),
36-
(call, request, context) async* {
37-
yield* request.map((req) {
38-
final provider = context.use<GreeterProvider>();
39-
final reply = provider.getGreeting(req.name);
40-
return HelloReply()..message = reply;
41-
});
42-
}
43-
);
44-
grpc<HelloRequest, HelloReply>(
45-
GrpcRoute(GreeterService, 'SayHello'),
46-
(call, request, context) async {
47-
final provider = context.use<GreeterProvider>();
48-
final reply = provider.getGreeting(request.name);
49-
return HelloReply()..message = reply;
50-
}
51-
);
52-
grpcStream<HelloRequest, HelloReply>(
53-
GrpcRoute(GreeterService, 'BidiHello'),
54-
(call, request, context) async* {
55-
await for (final req in request) {
56-
final provider = context.use<GreeterProvider>();
57-
final reply = provider.getGreeting(req.name);
58-
yield HelloReply()..message = reply;
59-
}
60-
}
61-
);
62-
}
39+
AppController({
40+
required super.service,
41+
});
6342

6443
}
6544

@@ -75,7 +54,7 @@ class AppModule extends Module {
7554
GreeterProvider(),
7655
],
7756
controllers: [
78-
AppController(),
57+
AppController(service: GreeterService()),
7958
],
8059
);
8160
}
@@ -87,15 +66,12 @@ Future<void> main(List<String> arguments) async {
8766
GrpcOptions(
8867
port: 50051,
8968
host: InternetAddress.loopbackIPv4,
90-
services: [
91-
GreeterService(),
92-
],
9369
codecRegistry: CodecRegistry(codecs: const [
9470
GzipCodec(),
9571
IdentityCodec(),
96-
],
97-
)
98-
))
72+
]),
73+
),
74+
),
9975
);
10076
await microservice.serve();
10177
}

examples/grpc_example/lib/generated/helloworld.pb.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// ignore_for_file: constant_identifier_names
99
// ignore_for_file: curly_braces_in_flow_control_structures
1010
// ignore_for_file: deprecated_member_use_from_same_package, library_prefixes
11-
// ignore_for_file: non_constant_identifier_names
11+
// ignore_for_file: non_constant_identifier_names, prefer_relative_imports
1212

1313
import 'dart:core' as $core;
1414

@@ -56,8 +56,6 @@ class HelloRequest extends $pb.GeneratedMessage {
5656
static HelloRequest create() => HelloRequest._();
5757
@$core.override
5858
HelloRequest createEmptyInstance() => create();
59-
static $pb.PbList<HelloRequest> createRepeated() =>
60-
$pb.PbList<HelloRequest>();
6159
@$core.pragma('dart2js:noInline')
6260
static HelloRequest getDefault() => _defaultInstance ??=
6361
$pb.GeneratedMessage.$_defaultFor<HelloRequest>(create);
@@ -112,7 +110,6 @@ class HelloReply extends $pb.GeneratedMessage {
112110
static HelloReply create() => HelloReply._();
113111
@$core.override
114112
HelloReply createEmptyInstance() => create();
115-
static $pb.PbList<HelloReply> createRepeated() => $pb.PbList<HelloReply>();
116113
@$core.pragma('dart2js:noInline')
117114
static HelloReply getDefault() => _defaultInstance ??=
118115
$pb.GeneratedMessage.$_defaultFor<HelloReply>(create);

examples/grpc_example/lib/generated/helloworld.pbenum.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
// ignore_for_file: constant_identifier_names
99
// ignore_for_file: curly_braces_in_flow_control_structures
1010
// ignore_for_file: deprecated_member_use_from_same_package, library_prefixes
11-
// ignore_for_file: non_constant_identifier_names
11+
// ignore_for_file: non_constant_identifier_names, prefer_relative_imports

examples/grpc_example/lib/generated/helloworld.pbgrpc.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// ignore_for_file: constant_identifier_names
99
// ignore_for_file: curly_braces_in_flow_control_structures
1010
// ignore_for_file: deprecated_member_use_from_same_package, library_prefixes
11-
// ignore_for_file: non_constant_identifier_names
11+
// ignore_for_file: non_constant_identifier_names, prefer_relative_imports
1212

1313
import 'dart:async' as $async;
1414
import 'dart:core' as $core;

examples/grpc_example/lib/generated/helloworld.pbjson.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
// ignore_for_file: constant_identifier_names
99
// ignore_for_file: curly_braces_in_flow_control_structures
1010
// ignore_for_file: deprecated_member_use_from_same_package, library_prefixes
11-
// ignore_for_file: non_constant_identifier_names, unused_import
11+
// ignore_for_file: non_constant_identifier_names, prefer_relative_imports
12+
// ignore_for_file: unused_import
1213

1314
import 'dart:convert' as $convert;
1415
import 'dart:core' as $core;

examples/grpc_example/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ publish_to: none
55
environment:
66
sdk: '>=3.9.0 <4.0.0'
77

8-
resolution: workspace
98

109
dependencies:
1110
serinus: ^2.0.0
1211
dio: ^5.7.0
1312
protobuf: ^6.0.0
1413
grpc: ^5.1.0
15-
serinus_microservices: ^0.1.0
14+
serinus_microservices:
15+
path: ../../packages/serinus_microservices
1616
dev_dependencies:
1717
build_runner: ^2.4.12
1818

packages/serinus_microservices/lib/transporters/grpc/grpc_controller.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ import 'package:grpc/grpc.dart';
22
import 'package:protobuf/protobuf.dart';
33
import 'package:serinus/serinus.dart';
44

5+
/// The [GrpcServiceController] allows defining gRPC services as Serinus Controllers.
6+
///
7+
/// It extends the base [Controller] class and includes a reference to the gRPC [Service] it represents. This controller can be used to group related gRPC methods together, making it easier to manage and organize your gRPC services within the Serinus framework.
8+
class GrpcServiceController extends Controller {
9+
10+
/// The [service] property contains the gRPC service definition that this controller represents.
11+
final Service service;
12+
13+
/// Creates a gRPC service controller.
14+
GrpcServiceController({
15+
required this.service,
16+
}): super(service.$name);
17+
}
18+
519
/// The [GrpcRoute] class is the gRPC
620
class GrpcRoute extends Route {
721
/// The [serviceName] property contains the name of the gRPC service.

0 commit comments

Comments
 (0)