Skip to content

Commit

Permalink
Add js-plugins pre-built support (#3056)
Browse files Browse the repository at this point in the history
Co-authored-by: Colin Alworth <[email protected]>
  • Loading branch information
devinrsmith and niloc132 authored Nov 3, 2022
1 parent 34857ec commit 751a963
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.util.resource.Resource;
Expand All @@ -49,7 +51,7 @@

import static io.grpc.servlet.web.websocket.MultiplexedWebSocketServerStream.GRPC_WEBSOCKETS_MULTIPLEX_PROTOCOL;
import static io.grpc.servlet.web.websocket.WebSocketServerStream.GRPC_WEBSOCKETS_PROTOCOL;
import static org.eclipse.jetty.servlet.ServletContextHandler.SESSIONS;
import static org.eclipse.jetty.servlet.ServletContextHandler.NO_SESSIONS;

public class JettyBackedGrpcServer implements GrpcServer {

Expand All @@ -63,7 +65,7 @@ public JettyBackedGrpcServer(
jetty.addConnector(createConnector(jetty, config));

final WebAppContext context =
new WebAppContext(null, "/", null, null, null, new ErrorPageErrorHandler(), SESSIONS);
new WebAppContext(null, "/", null, null, null, new ErrorPageErrorHandler(), NO_SESSIONS);
try {
String knownFile = "/ide/index.html";
URL ide = JettyBackedGrpcServer.class.getResource(knownFile);
Expand All @@ -72,6 +74,7 @@ public JettyBackedGrpcServer(
} catch (IOException ioException) {
throw new UncheckedIOException(ioException);
}
context.setInitParameter(DefaultServlet.CONTEXT_INIT + "dirAllowed", "false");

// For the Web UI, cache everything in the static folder
// https://create-react-app.dev/docs/production-build/#static-file-caching
Expand All @@ -85,9 +88,6 @@ public JettyBackedGrpcServer(
// Add an extra filter to redirect from / to /ide/
context.addFilter(HomeFilter.class, "/", EnumSet.noneOf(DispatcherType.class));

// Direct jetty all use this configuration as the root application
context.setContextPath("/");

// Handle grpc-web connections, translate to vanilla grpc
context.addFilter(new FilterHolder(new GrpcWebFilter()), "/*", EnumSet.noneOf(DispatcherType.class));

Expand Down Expand Up @@ -123,7 +123,14 @@ public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationExc
);
});
}
jetty.setHandler(context);

// Note: handler order matters due to pathSpec order
HandlerCollection handlers = new HandlerCollection();
// Set up /js-plugins/*
JsPlugins.maybeAdd(handlers::addHandler);
// Set up /*
handlers.addHandler(context);
jetty.setHandler(handlers);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.deephaven.server.jetty;

import io.deephaven.configuration.Configuration;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.function.Consumer;

import static org.eclipse.jetty.servlet.ServletContextHandler.NO_SESSIONS;

class JsPlugins {

public static void maybeAdd(Consumer<Handler> addHandler) {
// Note: this would probably be better to live in JettyConfig - but until we establish more formal expectations
// for js plugin configuration and workflows, we'll keep this here.
final String resourceBase =
Configuration.getInstance().getStringWithDefault("deephaven.jsPlugins.resourceBase", null);
if (resourceBase == null) {
return;
}
try {
Resource resource = ControlledCacheResource.wrap(Resource.newResource(resourceBase));
WebAppContext context =
new WebAppContext(null, "/js-plugins/", null, null, null, new ErrorPageErrorHandler(), NO_SESSIONS);
context.setBaseResource(resource);
context.setInitParameter(DefaultServlet.CONTEXT_INIT + "dirAllowed", "false");
// Suppress warnings about security handlers
context.setSecurityHandler(new ConstraintSecurityHandler());
addHandler.accept(context);
} catch (IOException e) {
throw new IllegalStateException(String.format("Unable to resolve resourceBase '%s'", resourceBase), e);
}
}
}

0 comments on commit 751a963

Please sign in to comment.