Skip to content

Commit 3b65dfb

Browse files
refactor: use http framework (#152)
* refactor: use http framework * chore: update to comitted submodule * chore: update framework versions
1 parent 1c64f19 commit 3b65dfb

File tree

6 files changed

+48
-113
lines changed

6 files changed

+48
-113
lines changed

hypertrace-graphql-impl/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ dependencies {
3636

3737
implementation("org.slf4j:slf4j-api")
3838
implementation("com.google.inject:guice")
39-
runtimeOnly("io.grpc:grpc-netty")
4039

4140
testImplementation("org.junit.jupiter:junit-jupiter")
4241
testImplementation("org.mockito:mockito-core")

hypertrace-graphql-service/build.gradle.kts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ plugins {
77

88
dependencies {
99
implementation("com.typesafe:config")
10-
implementation("org.hypertrace.core.serviceframework:platform-service-framework:0.1.21")
10+
implementation("org.hypertrace.core.serviceframework:platform-service-framework:0.1.42")
1111
implementation("org.slf4j:slf4j-api")
1212

13-
implementation("org.eclipse.jetty:jetty-server:9.4.42.v20210604")
14-
implementation("org.eclipse.jetty:jetty-servlet:9.4.42.v20210604")
15-
implementation("org.eclipse.jetty:jetty-servlets:9.4.42.v20210604")
13+
implementation("org.hypertrace.core.serviceframework:platform-http-service-framework:0.1.42")
1614

1715
implementation("com.graphql-java-kickstart:graphql-java-servlet")
1816
implementation(project(":hypertrace-graphql-impl"))
@@ -21,6 +19,7 @@ dependencies {
2119
annotationProcessor("org.projectlombok:lombok")
2220
compileOnly("org.projectlombok:lombok")
2321

22+
runtimeOnly("io.grpc:grpc-netty")
2423
runtimeOnly("org.apache.logging.log4j:log4j-slf4j-impl")
2524

2625
annotationProcessor(platform(project(":hypertrace-graphql-platform")))
Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,18 @@
11
package org.hypertrace.graphql.service;
22

3-
import org.eclipse.jetty.server.Server;
4-
import org.hypertrace.core.serviceframework.PlatformService;
3+
import java.util.List;
54
import org.hypertrace.core.serviceframework.config.ConfigClient;
6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
5+
import org.hypertrace.core.serviceframework.http.HttpHandlerFactory;
6+
import org.hypertrace.core.serviceframework.http.StandAloneHttpPlatformServiceContainer;
87

9-
public class GraphQlService extends PlatformService {
10-
private static final Logger LOG = LoggerFactory.getLogger(GraphQlService.class);
11-
12-
private Server server;
13-
private GraphQlServiceImpl graphQlServiceImpl;
8+
public class GraphQlService extends StandAloneHttpPlatformServiceContainer {
149

1510
public GraphQlService(ConfigClient configClient) {
1611
super(configClient);
1712
}
1813

1914
@Override
20-
protected void doInit() {
21-
graphQlServiceImpl = new GraphQlServiceImpl(this.getAppConfig());
22-
server = new Server(graphQlServiceImpl.getGraphQlServiceConfig().getServicePort());
23-
server.setHandler(graphQlServiceImpl.getContextHandler());
24-
server.setStopAtShutdown(true);
25-
}
26-
27-
@Override
28-
protected void doStart() {
29-
LOG.info("Starting service: {}", this.getServiceName());
30-
try {
31-
server.start();
32-
} catch (Exception e) {
33-
LOG.error("Failed to start service: {}", this.getServiceName());
34-
throw new RuntimeException(e);
35-
}
36-
37-
try {
38-
server.join();
39-
} catch (InterruptedException ie) {
40-
Thread.currentThread().interrupt();
41-
throw new RuntimeException(ie);
42-
}
43-
}
44-
45-
@Override
46-
protected void doStop() {
47-
LOG.info("Shutting down service: {}", this.getServiceName());
48-
49-
graphQlServiceImpl.shutdown();
50-
51-
while (!server.isStopped()) {
52-
try {
53-
server.stop();
54-
} catch (Exception e) {
55-
LOG.error("Failed to shutdown service: {}", this.getServiceName());
56-
throw new RuntimeException(e);
57-
}
58-
}
59-
try {
60-
Thread.sleep(100);
61-
} catch (InterruptedException ignore) {
62-
}
63-
}
64-
65-
@Override
66-
public boolean healthCheck() {
67-
return true;
15+
protected List<HttpHandlerFactory> getHandlerFactories() {
16+
return List.of(new GraphQlServiceFactory());
6817
}
6918
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.hypertrace.graphql.service;
2+
3+
import java.util.List;
4+
import org.hypertrace.core.graphql.spi.config.GraphQlServiceConfig;
5+
import org.hypertrace.core.serviceframework.http.HttpContainerEnvironment;
6+
import org.hypertrace.core.serviceframework.http.HttpHandlerDefinition;
7+
import org.hypertrace.core.serviceframework.http.HttpHandlerDefinition.CorsConfig;
8+
import org.hypertrace.core.serviceframework.http.HttpHandlerFactory;
9+
import org.hypertrace.graphql.config.HypertraceGraphQlServiceConfig;
10+
import org.hypertrace.graphql.impl.GraphQlFactory;
11+
12+
public class GraphQlServiceFactory implements HttpHandlerFactory {
13+
private static final String SERVICE_NAME = "hypertrace-graphql";
14+
15+
@Override
16+
public List<HttpHandlerDefinition> buildHandlers(HttpContainerEnvironment environment) {
17+
HypertraceGraphQlServiceConfig config =
18+
new DefaultGraphQlServiceConfig(environment.getConfig(SERVICE_NAME));
19+
DefaultGraphQlServiceLifecycle serviceLifecycle = new DefaultGraphQlServiceLifecycle();
20+
environment.getLifecycle().shutdownComplete().thenRun(serviceLifecycle::shutdown);
21+
22+
return List.of(
23+
HttpHandlerDefinition.builder()
24+
.name("graphql")
25+
.port(config.getServicePort())
26+
.contextPath(config.getGraphQlUrlPath())
27+
.corsConfig(buildCorsConfig(config))
28+
.servlet(new GraphQlServiceHttpServlet(GraphQlFactory.build(config, serviceLifecycle)))
29+
.build());
30+
}
31+
32+
private CorsConfig buildCorsConfig(GraphQlServiceConfig config) {
33+
if (!config.isCorsEnabled()) {
34+
return null;
35+
}
36+
return CorsConfig.builder().allowedHeaders(List.of("*")).build();
37+
}
38+
}

hypertrace-graphql-service/src/main/java/org/hypertrace/graphql/service/GraphQlServiceImpl.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)