Last active
September 18, 2020 09:05
-
-
Save carchrae/7466073 to your computer and use it in GitHub Desktop.
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
ClientGetter playClientGetter = new ClientGetter() { | |
private int playPort; | |
private String playHost; | |
{ | |
String portString = System.getenv("PLAY_PORT"); | |
if (portString == null) | |
portString = "9000"; | |
playPort = Integer.parseInt(portString); | |
playHost = System.getenv("PLAY_HOST"); | |
if (playHost == null) | |
playHost = "localhost"; | |
// container.logger().info( | |
// "Using play proxy: " + playHost + ":" + playPort); | |
} | |
@Override | |
public HttpClient get() { | |
HttpClient client = vertx.createHttpClient().setHost(playHost) | |
.setPort(playPort); | |
client.exceptionHandler(new Handler<Throwable>() { | |
@Override | |
public void handle(Throwable event) { | |
event.printStackTrace(); | |
} | |
}); | |
client.setConnectTimeout(500); | |
return client; | |
} | |
}; |
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
package app; | |
import java.util.Map.Entry; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.logging.Logger; | |
import org.vertx.java.core.Handler; | |
import org.vertx.java.core.buffer.Buffer; | |
import org.vertx.java.core.http.HttpClient; | |
import org.vertx.java.core.http.HttpClientRequest; | |
import org.vertx.java.core.http.HttpClientResponse; | |
import org.vertx.java.core.http.HttpServerRequest; | |
public class Proxy implements Handler<HttpServerRequest> { | |
private static final boolean DEBUG = false; | |
private static Logger logger = Logger.getLogger(Proxy.class.getName()); | |
private ConcurrentHashMap<String, String> requests; | |
private ClientGetter clients; | |
public interface ClientGetter { | |
HttpClient get(); | |
} | |
public Proxy(Routes routes, ClientGetter clients) { | |
routes.all("/data", this); | |
routes.all("/ds", this); | |
routes.get("/", this); | |
routes.noMatch(this); | |
if (DEBUG) | |
requests = new ConcurrentHashMap<>(); | |
this.clients = clients; | |
} | |
@Override | |
public void handle(final HttpServerRequest request) { | |
final String path = request.uri(); | |
execute(path, request); | |
} | |
public void execute(final String path, final HttpServerRequest request) { | |
if (DEBUG) { | |
logger.info("proxy -> " + path); | |
requests.put(getKey(request), path); | |
} | |
Handler<Throwable> exceptionHandler = createExceptionHandler(path, | |
request); | |
boolean isGet = false; | |
HttpClient client = clients.get(); | |
final HttpClientRequest proxyRequest = client.request(request.method(), path, | |
createProxyResponseHandler(path, request)); | |
//logger.info("executing " + request.method() + " " + path); | |
proxyRequest.headers().add("X-Vertx",request.absoluteURI().toString()); | |
proxyRequest.headers().add(request.headers()); | |
proxyRequest.exceptionHandler(exceptionHandler); | |
proxyRequest.setTimeout(30000); | |
if (isGet) { | |
proxyRequest.end(); | |
if (DEBUG) | |
requests.put(getKey(request), " executed get"); | |
} else { | |
if (DEBUG) | |
logger.info("not get - so copying body for " + path); | |
request.bodyHandler(new Handler<Buffer>() { | |
@Override | |
public void handle(Buffer event) { | |
proxyRequest.end(event); | |
if (DEBUG) { | |
logger.info("proxy -> request data : " | |
+ event.length()); | |
requests.put( | |
getKey(request), | |
"wrote body to proxyRequest " | |
+ event.length()); | |
} | |
} | |
}); | |
} | |
} | |
public Handler<Throwable> createExceptionHandler(final String path, | |
final HttpServerRequest request) { | |
Handler<Throwable> exceptionHandler = new Handler<Throwable>() { | |
@Override | |
public void handle(Throwable event) { | |
//if (DEBUG) | |
logger.warning("proxy error " + path + " " | |
+ event.getMessage()); | |
request.response().setStatusCode(500).end(event.getMessage()); | |
showRequestsOnEnd(request); | |
} | |
}; | |
return exceptionHandler; | |
} | |
public Handler<HttpClientResponse> createProxyResponseHandler( | |
final String path, final HttpServerRequest request) { | |
Handler<HttpClientResponse> responseHandler = new Handler<HttpClientResponse>() { | |
@Override | |
public void handle(HttpClientResponse proxyResponse) { | |
if (DEBUG) | |
logger.info("proxy <- response " | |
+ proxyResponse.statusMessage() + " " + path); | |
request.response().headers().add(proxyResponse.headers()); | |
request.response().setStatusCode(proxyResponse.statusCode()); | |
request.response().setStatusMessage( | |
proxyResponse.statusMessage()); | |
proxyResponse.bodyHandler(new Handler<Buffer>() { | |
@Override | |
public void handle(Buffer data) { | |
if (DEBUG) | |
logger.info("proxy <- data " + data.length() + " " | |
+ path); | |
request.response().end(data); | |
showRequestsOnEnd(request); | |
} | |
}); | |
if (DEBUG) { | |
requests.put(getKey(request), | |
"got response " + proxyResponse.statusCode() + " " | |
+ proxyResponse.statusMessage()); | |
showRequests(requests); | |
} | |
} | |
}; | |
return responseHandler; | |
} | |
public String getKey(final HttpServerRequest request) { | |
return request.method() + "@" + request.hashCode() + " " | |
+ request.path(); | |
} | |
protected void showRequests(ConcurrentHashMap<String, String> requests) { | |
if (DEBUG) | |
logger.info("showRequests : " + requests.size()); | |
for (Entry<String, String> e : requests.entrySet()) { | |
if (DEBUG) | |
logger.info(" " + e.getKey() + " : " + e.getValue()); | |
} | |
if (DEBUG) | |
logger.info("showRequests ----- "); | |
} | |
public void showRequestsOnEnd(final HttpServerRequest request) { | |
if (DEBUG) { | |
String key = getKey(request); | |
String value = requests.remove(key); | |
logger.info("finished " + key + " : " + value); | |
showRequests(requests); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the getKey/showRequests stuff is just for debugging