forked from x-infra-lab/x-api-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
429 additions
and
727 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...teway/bootstrap/AbstractConfigurable.java → ...gateway/commons/AbstractConfigurable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...a/lab/gateway/bootstrap/Configurable.java → ...fra/lab/gateway/commons/Configurable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...i-gateway-server/src/main/java/io/github/xinfra/lab/gateway/endpoint/EndpointFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
x-api-gateway-server/src/main/java/io/github/xinfra/lab/gateway/endpoint/HttpEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.github.xinfra.lab.gateway.endpoint; | ||
|
||
import io.github.xinfra.lab.gateway.server.ServerWebExchange; | ||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.scheduler.Schedulers; | ||
import reactor.netty.http.client.HttpClient; | ||
import reactor.netty.http.server.HttpServerRequest; | ||
import reactor.netty.http.server.HttpServerResponse; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
import static io.github.xinfra.lab.gateway.commons.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR; | ||
import static io.github.xinfra.lab.gateway.commons.ServerWebExchangeUtils.markResponseCommitted; | ||
|
||
@Slf4j | ||
public class HttpEndpoint implements Endpoint<HttpEndpoint.Config> { | ||
|
||
@Data | ||
public static class Config { | ||
List<String> urls; | ||
List<Integer> weights; | ||
} | ||
|
||
private Config config; | ||
|
||
public HttpEndpoint(Config config) { | ||
this.config = config; | ||
} | ||
|
||
@Override | ||
public Config getConfig() { | ||
return this.config; | ||
} | ||
|
||
@Override | ||
public Mono<Void> invoke(ServerWebExchange exchange) { | ||
|
||
|
||
HttpServerRequest request = exchange.getRequest(); | ||
HttpServerResponse response = exchange.getResponse(); | ||
|
||
return HttpClient.create() | ||
.headers(headers -> { | ||
headers.add(request.requestHeaders()); | ||
}) | ||
.request(request.method()) | ||
.uri(URI.create(config.getUrls().get(0))) // TODO Chooser | ||
.send(request.receive()) | ||
.response((httpClientResponse, byteBufFlux) -> | ||
response.status(httpClientResponse.status()) | ||
.headers(httpClientResponse.responseHeaders()) | ||
.sendByteArray(byteBufFlux.asByteArray()) | ||
) | ||
.subscribeOn(Schedulers.boundedElastic()) | ||
.doOnError(t -> { | ||
log.error("fail invoke http endpoint:{}", | ||
exchange.getRequiredAttribute(GATEWAY_ROUTE_ATTR), t); | ||
}).doOnNext(v -> { | ||
markResponseCommitted(exchange); | ||
}).then(); | ||
} | ||
} |
57 changes: 9 additions & 48 deletions
57
...teway-server/src/main/java/io/github/xinfra/lab/gateway/endpoint/HttpEndpointFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,27 @@ | ||
package io.github.xinfra.lab.gateway.endpoint; | ||
|
||
|
||
import io.github.xinfra.lab.gateway.bootstrap.AbstractConfigurable; | ||
import lombok.Data; | ||
import io.github.xinfra.lab.gateway.commons.AbstractConfigurable; | ||
import lombok.extern.slf4j.Slf4j; | ||
import reactor.core.scheduler.Schedulers; | ||
import reactor.netty.http.client.HttpClient; | ||
import reactor.netty.http.server.HttpServerRequest; | ||
import reactor.netty.http.server.HttpServerResponse; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
import static io.github.xinfra.lab.gateway.commons.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR; | ||
import static io.github.xinfra.lab.gateway.commons.ServerWebExchangeUtils.markResponseCommitted; | ||
|
||
@Slf4j | ||
public class HttpEndpointFactory extends | ||
AbstractConfigurable<HttpEndpointFactory.Config> | ||
implements EndpointFactory<HttpEndpointFactory.Config> { | ||
AbstractConfigurable<HttpEndpoint.Config> | ||
implements EndpointFactory<HttpEndpoint.Config> { | ||
public static final String NAME = "Http"; | ||
|
||
public HttpEndpointFactory() { | ||
super(HttpEndpointFactory.Config.class); | ||
super(HttpEndpoint.Config.class); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "Http"; | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public Endpoint apply(Config config) { | ||
return exchange -> { | ||
|
||
HttpServerRequest request = exchange.getRequest(); | ||
HttpServerResponse response = exchange.getResponse(); | ||
|
||
return HttpClient.create() | ||
.headers(headers -> { | ||
headers.add(request.requestHeaders()); | ||
}) | ||
.request(request.method()) | ||
.uri(URI.create(config.getHosts().get(0))) // TODO Chooser | ||
.send(request.receive()) | ||
.response((httpClientResponse, byteBufFlux) -> | ||
response.status(httpClientResponse.status()) | ||
.headers(httpClientResponse.responseHeaders()) | ||
.sendByteArray(byteBufFlux.asByteArray()) | ||
) | ||
.subscribeOn(Schedulers.boundedElastic()) | ||
.doOnError(t -> { | ||
log.error("fail invoke http endpoint:{}", | ||
exchange.getRequiredAttribute(GATEWAY_ROUTE_ATTR), t); | ||
}).doOnNext(v -> { | ||
markResponseCommitted(exchange); | ||
}).then(); | ||
}; | ||
public Endpoint apply(HttpEndpoint.Config config) { | ||
return new HttpEndpoint(config); | ||
} | ||
|
||
|
||
@Data | ||
public static class Config { | ||
List<String> hosts; | ||
List<Integer> weights; | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
...rver/src/main/java/io/github/xinfra/lab/gateway/filter/AddHeaderGatewayFilterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ateway-server/src/main/java/io/github/xinfra/lab/gateway/filter/GatewayFilterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ay-server/src/main/java/io/github/xinfra/lab/gateway/predicate/RoutePredicateFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.