Skip to content

refactor: use http service framework #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions hypertrace-core-graphql-impl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,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")
testImplementation("org.mockito:mockito-junit-jupiter")
Expand Down
8 changes: 4 additions & 4 deletions hypertrace-core-graphql-platform/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ dependencies {
api(platform("io.grpc:grpc-bom:1.47.0"))
constraints {

api("org.hypertrace.core.grpcutils:grpc-context-utils:0.7.4")
api("org.hypertrace.core.grpcutils:grpc-client-utils:0.7.4")
api("org.hypertrace.core.grpcutils:grpc-client-rx-utils:0.7.4")
api("org.hypertrace.core.grpcutils:grpc-client-rx-utils:0.7.4")
api("org.hypertrace.core.grpcutils:grpc-context-utils:0.7.6")
api("org.hypertrace.core.grpcutils:grpc-client-utils:0.7.6")
api("org.hypertrace.core.grpcutils:grpc-client-rx-utils:0.7.6")
api("org.hypertrace.core.grpcutils:grpc-client-rx-utils:0.7.6")
api("org.hypertrace.gateway.service:gateway-service-api:0.2.10")
api("org.hypertrace.core.attribute.service:caching-attribute-service-client:0.13.6")

Expand Down
11 changes: 2 additions & 9 deletions hypertrace-core-graphql-service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,17 @@ plugins {
dependencies {
implementation(platform(project(":hypertrace-core-graphql-platform")))

implementation("com.typesafe:config")
implementation("org.hypertrace.core.serviceframework:platform-service-framework:0.1.23")
implementation("org.hypertrace.core.serviceframework:platform-http-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("com.graphql-java-kickstart:graphql-java-servlet")
implementation(project(":hypertrace-core-graphql-impl"))
implementation(project(":hypertrace-core-graphql-spi"))

runtimeOnly("org.apache.logging.log4j:log4j-slf4j-impl")
runtimeOnly("io.grpc:grpc-netty")
}

application {
mainClass.set("org.hypertrace.core.serviceframework.PlatformServiceLauncher")
}
tasks.run<JavaExec> {
jvmArgs = listOf("-Dservice.name=${project.name}")
}
Original file line number Diff line number Diff line change
@@ -1,95 +1,47 @@
package org.hypertrace.core.graphql.service;

import java.util.EnumSet;
import javax.servlet.DispatcherType;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlets.CrossOriginFilter;
import java.util.List;
import org.hypertrace.core.graphql.impl.GraphQlFactory;
import org.hypertrace.core.graphql.spi.config.GraphQlServiceConfig;
import org.hypertrace.core.serviceframework.PlatformService;
import org.hypertrace.core.serviceframework.config.ConfigClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.core.serviceframework.http.StandAloneHttpPlatformServiceContainer;

public class GraphQlService extends PlatformService {

private static final Logger LOG = LoggerFactory.getLogger(GraphQlService.class);

private GraphQlServiceConfig graphQlServiceConfig;
private DefaultGraphQlServiceLifecycle serviceLifecycle;
private Server server;
public class GraphQlService extends StandAloneHttpPlatformServiceContainer {
private static final String SERVICE_NAME = "hypertrace-core-graphql";

public GraphQlService(ConfigClient configClient) {
super(configClient);
}

@Override
protected void doInit() {
this.graphQlServiceConfig = new DefaultGraphQlServiceConfig(this.getAppConfig());
this.serviceLifecycle = new DefaultGraphQlServiceLifecycle();
this.server = new Server(this.graphQlServiceConfig.getServicePort());

ServletContextHandler context = new ServletContextHandler();
if (this.graphQlServiceConfig.isCorsEnabled()) {
context.addFilter(
CrossOriginFilter.class,
this.graphQlServiceConfig.getGraphQlUrlPath(),
EnumSet.of(DispatcherType.REQUEST));
}

context.addServlet(
new ServletHolder(
new GraphQlServiceHttpServlet(
GraphQlFactory.build(this.graphQlServiceConfig, this.serviceLifecycle))),
this.graphQlServiceConfig.getGraphQlUrlPath());

this.server.setHandler(context);
this.server.setStopAtShutdown(true);
protected List<HttpHandlerFactory> getHandlerFactories() {
return List.of(this::buildHandlerDefinition);
}

@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);
}
List<HttpHandlerDefinition> buildHandlerDefinition(HttpContainerEnvironment environment) {
GraphQlServiceConfig 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());
}

@Override
protected void doStop() {
LOG.info("Shutting down service: {}", this.getServiceName());
if (this.serviceLifecycle != null) {
this.serviceLifecycle.shutdown();
}
while (!server.isStopped()) {
try {
server.stop();
} catch (Exception e) {
LOG.error("Failed to shutdown service: {}", this.getServiceName());
throw new RuntimeException(e);
}
private CorsConfig buildCorsConfig(GraphQlServiceConfig config) {
if (!config.isCorsEnabled()) {
return null;
}
try {
Thread.sleep(100);
} catch (InterruptedException ignore) {
}
}

@Override
public boolean healthCheck() {
return true;
return CorsConfig.builder().allowedHeaders(List.of("*")).build();
}
}