1
1
package io .adven .grpc .wiremock ;
2
2
3
+ import io .adven .grpc .wiremock .properties .GrpcProperties ;
4
+ import io .adven .grpc .wiremock .properties .WiremockProperties ;
3
5
import io .grpc .BindableService ;
4
6
import io .grpc .Server ;
5
7
import io .grpc .netty .NettyServerBuilder ;
21
23
import static java .util .stream .Collectors .joining ;
22
24
23
25
@ SpringBootApplication
24
- @ EnableConfigurationProperties ({ServerProperties .class , WiremockProperties .class })
26
+ @ EnableConfigurationProperties ({GrpcProperties .class , WiremockProperties .class })
25
27
public class GrpcWiremock implements CommandLineRunner {
26
28
private static final Logger LOG = LoggerFactory .getLogger (GrpcWiremock .class );
27
29
private final GrpcServer server ;
@@ -37,22 +39,22 @@ public void run(String... args) throws Exception {
37
39
38
40
@ Service
39
41
public static class GrpcServer {
40
- private final ServerProperties serverProperties ;
42
+ private final GrpcProperties grpcProperties ;
41
43
private final List <BindableService > services ;
42
44
private Server server ;
43
- private CodecRegistry codecRegistry ;
45
+ private final CodecRegistry codecRegistry ;
44
46
private final CountDownLatch latch ;
45
47
46
- public GrpcServer (ServerProperties serverProperties , CodecRegistry codecRegistry , List <BindableService > services ) {
47
- this .serverProperties = serverProperties ;
48
+ public GrpcServer (GrpcProperties grpcProperties , CodecRegistry codecRegistry , List <BindableService > services ) {
49
+ this .grpcProperties = grpcProperties ;
48
50
this .codecRegistry = codecRegistry ;
49
51
this .services = services ;
50
52
this .latch = new CountDownLatch (1 );
51
53
}
52
54
53
55
public void start (int port ) throws IOException {
54
56
NettyServerBuilder builder = NettyServerBuilder .forPort (port )
55
- .intercept (new ExceptionHandler ())
57
+ .intercept (new ExceptionHandler (grpcProperties . getErrorCodeBy () ))
56
58
.compressorRegistry (codecRegistry .compressorRegistry ())
57
59
.decompressorRegistry (codecRegistry .decompressorRegistry ())
58
60
.addService (ProtoReflectionService .newInstance ());
@@ -65,31 +67,34 @@ public void start(int port) throws IOException {
65
67
}
66
68
67
69
private void setProperties (NettyServerBuilder builder ) {
68
- if (serverProperties .getMaxHeaderListSize () != null ) {
69
- int val = Math .toIntExact (serverProperties .getMaxHeaderListSize ().toBytes ());
70
- LOG .info ("Set maxHeaderListSize = {}" , val );
71
- builder .maxHeaderListSize (val );
72
- }
73
- if (serverProperties .getMaxMessageSize () != null ) {
74
- int val = Math .toIntExact (serverProperties .getMaxMessageSize ().toBytes ());
75
- LOG .info ("Set maxMessageSize = {}" , val );
76
- builder .maxMessageSize (val );
77
- }
78
- if (serverProperties .getMaxInboundMetadataSize () != null ) {
79
- int val = Math .toIntExact (serverProperties .getMaxInboundMetadataSize ().toBytes ());
80
- LOG .info ("Set maxInboundMetadataSize = {}" , val );
81
- builder .maxInboundMetadataSize (val );
82
- }
83
- if (serverProperties .getMaxInboundMessageSize () != null ) {
84
- int val = Math .toIntExact (serverProperties .getMaxInboundMessageSize ().toBytes ());
85
- LOG .info ("Set maxInboundMessageSize = {}" , val );
86
- builder .maxInboundMessageSize (val );
70
+ GrpcProperties .ServerProperties server = grpcProperties .getServer ();
71
+ if (server != null ) {
72
+ if (server .getMaxHeaderListSize () != null ) {
73
+ int val = Math .toIntExact (server .getMaxHeaderListSize ().toBytes ());
74
+ LOG .info ("Set maxHeaderListSize = {}" , val );
75
+ builder .maxHeaderListSize (val );
76
+ }
77
+ if (server .getMaxMessageSize () != null ) {
78
+ int val = Math .toIntExact (server .getMaxMessageSize ().toBytes ());
79
+ LOG .info ("Set maxMessageSize = {}" , val );
80
+ builder .maxMessageSize (val );
81
+ }
82
+ if (server .getMaxInboundMetadataSize () != null ) {
83
+ int val = Math .toIntExact (server .getMaxInboundMetadataSize ().toBytes ());
84
+ LOG .info ("Set maxInboundMetadataSize = {}" , val );
85
+ builder .maxInboundMetadataSize (val );
86
+ }
87
+ if (server .getMaxInboundMessageSize () != null ) {
88
+ int val = Math .toIntExact (server .getMaxInboundMessageSize ().toBytes ());
89
+ LOG .info ("Set maxInboundMessageSize = {}" , val );
90
+ builder .maxInboundMessageSize (val );
91
+ }
87
92
}
88
93
}
89
94
90
95
private String summary (Server server ) {
91
96
return "Started " + server + "\n Registered services:\n " +
92
- server .getServices ().stream ().map (s -> " * " + s .getServiceDescriptor ().getName ()).collect (joining ("\n " ));
97
+ server .getServices ().stream ().map (s -> " * " + s .getServiceDescriptor ().getName ()).collect (joining ("\n " ));
93
98
}
94
99
95
100
private void startDaemonAwaitThread () {
0 commit comments