package io.conducive.server.wiring.command;
import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.common.io.Resources;
import com.yammer.dropwizard.Service;
import com.yammer.dropwizard.cli.EnvironmentCommand;
import com.yammer.dropwizard.config.Environment;
import com.yammer.dropwizard.config.HttpConfiguration;
import com.yammer.dropwizard.config.ServerFactory;
import com.yammer.dropwizard.lifecycle.ServerLifecycleListener;
import net.sourceforge.argparse4j.inf.Namespace;
import org.eclipse.jetty.server.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
* A dropwizard command. When called instead of "server", this creates a proxy on port 8090 (most likely; see DebugServer implementation)
* which routes between the dropwizard backend (running on port 8080) and gwt in hosted mode on the front end. This allows
* developers to use gwt:run (to view changes to the UI quickly, without recompiling) and gwt:debug (to step through
* the UI in their IDE's debugger).
*
* @author Reuben Firmin
*/
public class Debug<T extends ConduciveConfiguration> extends EnvironmentCommand<T> {
private final Logger logger = LoggerFactory.getLogger(getClass());
private Class<T> clazz;
public Debug(Service<T> service, Class<T> clazz) {
super(service, "debug", "Runs the Dropwizard service as an HTTP server");
this.clazz = clazz;
}
/*
* Since we don't subclass ServerCommand, we need a concrete reference to the configuration
* class.
*/
@Override
protected Class<T> getConfigurationClass() {
return clazz;
}
@Override
protected void run(Environment environment, Namespace namespace, T configuration) throws Exception {
Preconditions.checkArgument(!configuration.getHttpConfiguration().getRootPath().equals("/"), "Dropwizard must be running on a non-root path");
final HttpConfiguration nonSSLConfig = new HttpConfiguration();
nonSSLConfig.setPort(8080);
nonSSLConfig.setRootPath(configuration.getHttpConfiguration().getRootPath());
nonSSLConfig.setConnectorType(HttpConfiguration.ConnectorType.NONBLOCKING);
final DebugServer debug = new DebugServer(configuration, nonSSLConfig.getPort(), nonSSLConfig.getRootPath());
final int debugPort = debug.run();
final Server server = new ServerFactory(nonSSLConfig,
environment.getName()).buildServer(environment);
logBanner(environment.getName(), logger);
try {
server.start();
for (ServerLifecycleListener listener : environment.getServerListeners()) {
listener.serverStarted(server);
}
} catch (Exception e) {
logger.error("Unable to start server, shutting down", e);
server.stop();
debug.stop();
}
logger.info("====> Running in Hybrid Debug mode - port " + debugPort + " will proxy between GWT and DropWizard; port "
+ nonSSLConfig.getPort() + " will hit regular compiled app");
logger.info("Now start gwt in hosted mode using gwt:run or gwt:debug");
}
private void logBanner(String name, Logger logger) {
try {
final String banner = Resources.toString(Resources.getResource("banner.txt"),
Charsets.UTF_8);
logger.info("Starting {}\n{}", name, banner);
} catch (IllegalArgumentException ignored) {
// don't display the banner if there isn't one
logger.info("Starting {}", name);
} catch (IOException ignored) {
logger.info("Starting {}", name);
}
}
}