4
4
import io .netty .handler .codec .http .cors .CorsConfigBuilder ;
5
5
import io .netty .handler .codec .http .cors .CorsHandler ;
6
6
import io .scalecube .services .Address ;
7
+ import io .scalecube .services .ServiceCall ;
7
8
import io .scalecube .services .exceptions .DefaultErrorMapper ;
8
9
import io .scalecube .services .exceptions .ServiceProviderErrorMapper ;
9
10
import io .scalecube .services .gateway .Gateway ;
10
- import io .scalecube .services .gateway . GatewayOptions ;
11
+ import io .scalecube .services .registry . api . ServiceRegistry ;
11
12
import java .net .InetSocketAddress ;
12
- import java .util .StringJoiner ;
13
13
import java .util .function .Consumer ;
14
+ import java .util .function .Function ;
14
15
import reactor .netty .DisposableServer ;
15
16
import reactor .netty .http .server .HttpServer ;
16
17
import reactor .netty .resources .LoopResources ;
17
18
18
19
public class HttpGateway implements Gateway {
19
20
20
- private final GatewayOptions options ;
21
+ private final String id ;
22
+ private final int port ;
23
+ private final Function <ServiceCall , ServiceCall > callFactory ;
21
24
private final ServiceProviderErrorMapper errorMapper ;
22
25
private final boolean corsEnabled ;
23
26
private final CorsConfigBuilder corsConfigBuilder ;
@@ -26,28 +29,35 @@ public class HttpGateway implements Gateway {
26
29
private LoopResources loopResources ;
27
30
28
31
private HttpGateway (Builder builder ) {
29
- this .options = builder .options ;
32
+ this .id = builder .id ;
33
+ this .port = builder .port ;
34
+ this .callFactory = builder .callFactory ;
30
35
this .errorMapper = builder .errorMapper ;
31
36
this .corsEnabled = builder .corsEnabled ;
32
37
this .corsConfigBuilder = builder .corsConfigBuilder ;
33
38
}
34
39
35
40
@ Override
36
41
public String id () {
37
- return options . id () ;
42
+ return id ;
38
43
}
39
44
40
45
@ Override
41
- public Gateway start () {
42
- HttpGatewayAcceptor gatewayAcceptor = new HttpGatewayAcceptor (options .call (), errorMapper );
43
-
46
+ public Gateway start (ServiceCall call , ServiceRegistry serviceRegistry ) {
44
47
loopResources =
45
- LoopResources .create (
46
- options .id () + ":" + options .port (), LoopResources .DEFAULT_IO_WORKER_COUNT , true );
48
+ LoopResources .create (id + ":" + port , LoopResources .DEFAULT_IO_WORKER_COUNT , true );
47
49
48
50
try {
49
- prepareHttpServer (loopResources , options .port ())
50
- .handle (gatewayAcceptor )
51
+ HttpServer .create ()
52
+ .runOn (loopResources )
53
+ .bindAddress (() -> new InetSocketAddress (port ))
54
+ .doOnConnection (
55
+ connection -> {
56
+ if (corsEnabled ) {
57
+ connection .addHandlerLast (new CorsHandler (corsConfigBuilder .build ()));
58
+ }
59
+ })
60
+ .handle (new HttpGatewayAcceptor (callFactory .apply (call ), errorMapper ))
51
61
.bind ()
52
62
.doOnSuccess (server -> this .server = server )
53
63
.toFuture ()
@@ -59,23 +69,6 @@ public Gateway start() {
59
69
return this ;
60
70
}
61
71
62
- private HttpServer prepareHttpServer (LoopResources loopResources , int port ) {
63
- HttpServer httpServer = HttpServer .create ();
64
-
65
- if (loopResources != null ) {
66
- httpServer = httpServer .runOn (loopResources );
67
- }
68
-
69
- return httpServer
70
- .bindAddress (() -> new InetSocketAddress (port ))
71
- .doOnConnection (
72
- connection -> {
73
- if (corsEnabled ) {
74
- connection .addHandlerLast (new CorsHandler (corsConfigBuilder .build ()));
75
- }
76
- });
77
- }
78
-
79
72
@ Override
80
73
public Address address () {
81
74
InetSocketAddress address = (InetSocketAddress ) server .address ();
@@ -100,21 +93,11 @@ private void shutdownLoopResources(LoopResources loopResources) {
100
93
}
101
94
}
102
95
103
- @ Override
104
- public String toString () {
105
- return new StringJoiner (", " , HttpGateway .class .getSimpleName () + "[" , "]" )
106
- .add ("options=" + options )
107
- .add ("errorMapper=" + errorMapper )
108
- .add ("corsEnabled=" + corsEnabled )
109
- .add ("corsConfigBuilder=" + corsConfigBuilder )
110
- .add ("server=" + server )
111
- .add ("loopResources=" + loopResources )
112
- .toString ();
113
- }
114
-
115
96
public static class Builder {
116
97
117
- private GatewayOptions options ;
98
+ private String id = "http@" + Integer .toHexString (hashCode ());
99
+ private int port ;
100
+ private Function <ServiceCall , ServiceCall > callFactory = call -> call ;
118
101
private ServiceProviderErrorMapper errorMapper = DefaultErrorMapper .INSTANCE ;
119
102
private boolean corsEnabled = false ;
120
103
private CorsConfigBuilder corsConfigBuilder =
@@ -125,12 +108,26 @@ public static class Builder {
125
108
126
109
public Builder () {}
127
110
128
- public GatewayOptions options () {
129
- return options ;
111
+ public String id () {
112
+ return id ;
113
+ }
114
+
115
+ public Builder id (String id ) {
116
+ this .id = id ;
117
+ return this ;
118
+ }
119
+
120
+ public int port () {
121
+ return port ;
122
+ }
123
+
124
+ public Builder port (int port ) {
125
+ this .port = port ;
126
+ return this ;
130
127
}
131
128
132
- public Builder options ( GatewayOptions options ) {
133
- this . options = options ;
129
+ public Builder serviceCall ( Function < ServiceCall , ServiceCall > operator ) {
130
+ callFactory = callFactory . andThen ( operator ) ;
134
131
return this ;
135
132
}
136
133
0 commit comments