diff --git a/hypertrace-core-graphql b/hypertrace-core-graphql index 47b1e94d..25000d16 160000 --- a/hypertrace-core-graphql +++ b/hypertrace-core-graphql @@ -1 +1 @@ -Subproject commit 47b1e94d3aff49e9d44dc9ecb6b1199bcb10f53a +Subproject commit 25000d16a4d095a2ada91a6158ee538745449945 diff --git a/hypertrace-graphql-impl/build.gradle.kts b/hypertrace-graphql-impl/build.gradle.kts index 8d3c5c14..420ff0c9 100644 --- a/hypertrace-graphql-impl/build.gradle.kts +++ b/hypertrace-graphql-impl/build.gradle.kts @@ -36,7 +36,6 @@ dependencies { implementation("org.slf4j:slf4j-api") implementation("com.google.inject:guice") - runtimeOnly("io.grpc:grpc-netty") testImplementation("org.junit.jupiter:junit-jupiter") testImplementation("org.mockito:mockito-core") diff --git a/hypertrace-graphql-service/build.gradle.kts b/hypertrace-graphql-service/build.gradle.kts index 41873252..50ad108b 100644 --- a/hypertrace-graphql-service/build.gradle.kts +++ b/hypertrace-graphql-service/build.gradle.kts @@ -7,12 +7,10 @@ plugins { dependencies { implementation("com.typesafe:config") - implementation("org.hypertrace.core.serviceframework:platform-service-framework:0.1.21") + implementation("org.hypertrace.core.serviceframework:platform-service-framework:0.1.42") implementation("org.slf4j:slf4j-api") - implementation("org.eclipse.jetty:jetty-server:9.4.42.v20210604") - implementation("org.eclipse.jetty:jetty-servlet:9.4.42.v20210604") - implementation("org.eclipse.jetty:jetty-servlets:9.4.42.v20210604") + implementation("org.hypertrace.core.serviceframework:platform-http-service-framework:0.1.42") implementation("com.graphql-java-kickstart:graphql-java-servlet") implementation(project(":hypertrace-graphql-impl")) @@ -21,6 +19,7 @@ dependencies { annotationProcessor("org.projectlombok:lombok") compileOnly("org.projectlombok:lombok") + runtimeOnly("io.grpc:grpc-netty") runtimeOnly("org.apache.logging.log4j:log4j-slf4j-impl") annotationProcessor(platform(project(":hypertrace-graphql-platform"))) diff --git a/hypertrace-graphql-service/src/main/java/org/hypertrace/graphql/service/GraphQlService.java b/hypertrace-graphql-service/src/main/java/org/hypertrace/graphql/service/GraphQlService.java index 431f3ddf..b5a1a9f3 100644 --- a/hypertrace-graphql-service/src/main/java/org/hypertrace/graphql/service/GraphQlService.java +++ b/hypertrace-graphql-service/src/main/java/org/hypertrace/graphql/service/GraphQlService.java @@ -1,69 +1,18 @@ package org.hypertrace.graphql.service; -import org.eclipse.jetty.server.Server; -import org.hypertrace.core.serviceframework.PlatformService; +import java.util.List; import org.hypertrace.core.serviceframework.config.ConfigClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.hypertrace.core.serviceframework.http.HttpHandlerFactory; +import org.hypertrace.core.serviceframework.http.StandAloneHttpPlatformServiceContainer; -public class GraphQlService extends PlatformService { - private static final Logger LOG = LoggerFactory.getLogger(GraphQlService.class); - - private Server server; - private GraphQlServiceImpl graphQlServiceImpl; +public class GraphQlService extends StandAloneHttpPlatformServiceContainer { public GraphQlService(ConfigClient configClient) { super(configClient); } @Override - protected void doInit() { - graphQlServiceImpl = new GraphQlServiceImpl(this.getAppConfig()); - server = new Server(graphQlServiceImpl.getGraphQlServiceConfig().getServicePort()); - server.setHandler(graphQlServiceImpl.getContextHandler()); - server.setStopAtShutdown(true); - } - - @Override - protected void doStart() { - LOG.info("Starting service: {}", this.getServiceName()); - try { - server.start(); - } catch (Exception e) { - LOG.error("Failed to start service: {}", this.getServiceName()); - throw new RuntimeException(e); - } - - try { - server.join(); - } catch (InterruptedException ie) { - Thread.currentThread().interrupt(); - throw new RuntimeException(ie); - } - } - - @Override - protected void doStop() { - LOG.info("Shutting down service: {}", this.getServiceName()); - - graphQlServiceImpl.shutdown(); - - while (!server.isStopped()) { - try { - server.stop(); - } catch (Exception e) { - LOG.error("Failed to shutdown service: {}", this.getServiceName()); - throw new RuntimeException(e); - } - } - try { - Thread.sleep(100); - } catch (InterruptedException ignore) { - } - } - - @Override - public boolean healthCheck() { - return true; + protected List getHandlerFactories() { + return List.of(new GraphQlServiceFactory()); } } diff --git a/hypertrace-graphql-service/src/main/java/org/hypertrace/graphql/service/GraphQlServiceFactory.java b/hypertrace-graphql-service/src/main/java/org/hypertrace/graphql/service/GraphQlServiceFactory.java new file mode 100644 index 00000000..498fdb41 --- /dev/null +++ b/hypertrace-graphql-service/src/main/java/org/hypertrace/graphql/service/GraphQlServiceFactory.java @@ -0,0 +1,38 @@ +package org.hypertrace.graphql.service; + +import java.util.List; +import org.hypertrace.core.graphql.spi.config.GraphQlServiceConfig; +import org.hypertrace.core.serviceframework.http.HttpContainerEnvironment; +import org.hypertrace.core.serviceframework.http.HttpHandlerDefinition; +import org.hypertrace.core.serviceframework.http.HttpHandlerDefinition.CorsConfig; +import org.hypertrace.core.serviceframework.http.HttpHandlerFactory; +import org.hypertrace.graphql.config.HypertraceGraphQlServiceConfig; +import org.hypertrace.graphql.impl.GraphQlFactory; + +public class GraphQlServiceFactory implements HttpHandlerFactory { + private static final String SERVICE_NAME = "hypertrace-graphql"; + + @Override + public List buildHandlers(HttpContainerEnvironment environment) { + HypertraceGraphQlServiceConfig config = + new DefaultGraphQlServiceConfig(environment.getConfig(SERVICE_NAME)); + DefaultGraphQlServiceLifecycle serviceLifecycle = new DefaultGraphQlServiceLifecycle(); + environment.getLifecycle().shutdownComplete().thenRun(serviceLifecycle::shutdown); + + return List.of( + HttpHandlerDefinition.builder() + .name("graphql") + .port(config.getServicePort()) + .contextPath(config.getGraphQlUrlPath()) + .corsConfig(buildCorsConfig(config)) + .servlet(new GraphQlServiceHttpServlet(GraphQlFactory.build(config, serviceLifecycle))) + .build()); + } + + private CorsConfig buildCorsConfig(GraphQlServiceConfig config) { + if (!config.isCorsEnabled()) { + return null; + } + return CorsConfig.builder().allowedHeaders(List.of("*")).build(); + } +} diff --git a/hypertrace-graphql-service/src/main/java/org/hypertrace/graphql/service/GraphQlServiceImpl.java b/hypertrace-graphql-service/src/main/java/org/hypertrace/graphql/service/GraphQlServiceImpl.java deleted file mode 100644 index 2f0ce3c4..00000000 --- a/hypertrace-graphql-service/src/main/java/org/hypertrace/graphql/service/GraphQlServiceImpl.java +++ /dev/null @@ -1,50 +0,0 @@ -package org.hypertrace.graphql.service; - -import com.typesafe.config.Config; -import java.util.EnumSet; -import javax.servlet.DispatcherType; -import org.eclipse.jetty.servlet.ServletContextHandler; -import org.eclipse.jetty.servlet.ServletHolder; -import org.eclipse.jetty.servlets.CrossOriginFilter; -import org.hypertrace.core.graphql.spi.config.GraphQlServiceConfig; -import org.hypertrace.graphql.config.HypertraceGraphQlServiceConfig; -import org.hypertrace.graphql.impl.GraphQlFactory; - -public class GraphQlServiceImpl { - private HypertraceGraphQlServiceConfig graphQlServiceConfig; - private DefaultGraphQlServiceLifecycle serviceLifecycle; - private ServletContextHandler contextHandler; - - public GraphQlServiceImpl(Config appConfig) { - this.graphQlServiceConfig = new DefaultGraphQlServiceConfig(appConfig); - this.serviceLifecycle = new DefaultGraphQlServiceLifecycle(); - - contextHandler = new ServletContextHandler(); - if (this.graphQlServiceConfig.isCorsEnabled()) { - contextHandler.addFilter( - CrossOriginFilter.class, - this.graphQlServiceConfig.getGraphQlUrlPath(), - EnumSet.of(DispatcherType.REQUEST)); - } - - contextHandler.addServlet( - new ServletHolder( - new GraphQlServiceHttpServlet( - GraphQlFactory.build(this.graphQlServiceConfig, this.serviceLifecycle))), - this.graphQlServiceConfig.getGraphQlUrlPath()); - } - - public GraphQlServiceConfig getGraphQlServiceConfig() { - return graphQlServiceConfig; - } - - public ServletContextHandler getContextHandler() { - return contextHandler; - } - - public void shutdown() { - if (serviceLifecycle != null) { - serviceLifecycle.shutdown(); - } - } -}