Skip to content

refactor: use http framework #152

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 3 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
1 change: 0 additions & 1 deletion hypertrace-graphql-impl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 3 additions & 4 deletions hypertrace-graphql-service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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")))
Expand Down
Original file line number Diff line number Diff line change
@@ -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<HttpHandlerFactory> getHandlerFactories() {
return List.of(new GraphQlServiceFactory());
}
}
Original file line number Diff line number Diff line change
@@ -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<HttpHandlerDefinition> buildHandlers(HttpContainerEnvironment environment) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this in the hypertrace-core-graphql or is it needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's in both - each defines its own service (and docker image) as legacy of when hypertrace and hypertrace-core had independent platforms.

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();
}
}

This file was deleted.