Skip to content

Commit

Permalink
create platform http service apache#265
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Mar 19, 2020
1 parent c74965b commit 0448459
Show file tree
Hide file tree
Showing 29 changed files with 1,157 additions and 411 deletions.
2 changes: 1 addition & 1 deletion camel-k-main/camel-k-runtime-health/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</dependency>
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-runtime-inspector</artifactId>
<artifactId>camel-k-runtime-http</artifactId>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
import org.apache.camel.CamelContext;
import org.apache.camel.Ordered;
import org.apache.camel.health.HealthCheck;
Expand All @@ -33,7 +35,7 @@
import org.apache.camel.impl.health.ContextHealthCheck;
import org.apache.camel.impl.health.RoutesHealthCheckRepository;
import org.apache.camel.k.ContextCustomizer;
import org.apache.camel.k.inspector.InspectorCustomizer;
import org.apache.camel.k.http.PlatformHttpRouter;

public class HealthContextCustomizer implements ContextCustomizer {
public static final String DEFAULT_PATH = "/health";
Expand Down Expand Up @@ -83,7 +85,7 @@ public void setIncludeContext(boolean includeContext) {

@Override
public int getOrder() {
return Ordered.HIGHEST;
return Ordered.LOWEST;
}

@Override
Expand All @@ -105,14 +107,15 @@ public void apply(CamelContext camelContext) {
throw new RuntimeException(e);
}

camelContext.getRegistry().bind(
"health-route",
customizer(camelContext)
// add health route
addRoute(
camelContext,
PlatformHttpRouter.lookup(camelContext).get()
);
}

private InspectorCustomizer customizer(CamelContext camelContext) {
return router -> router.route(HttpMethod.GET, path).handler(routingContext -> {
private Route addRoute(CamelContext camelContext, Router router) {
return router.route(HttpMethod.GET, path).handler(routingContext -> {
int code = 200;

Collection<HealthCheck.Result> results = HealthCheckHelper.invoke(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.inspector.InspectorContextCustomizer;
import org.apache.camel.k.http.PlatformHttpServiceContextCustomizer;
import org.apache.camel.test.AvailablePortFinder;
import org.apache.camel.util.ObjectHelper;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -46,22 +46,22 @@ public void configure() throws Exception {
}
});

HealthContextCustomizer healthCustomizer = new HealthContextCustomizer();
healthCustomizer.apply(runtime.getCamelContext());

InspectorContextCustomizer inspectorCustomizer = new InspectorContextCustomizer();
inspectorCustomizer.setBindPort(AvailablePortFinder.getNextAvailable());
PlatformHttpServiceContextCustomizer phsc = new PlatformHttpServiceContextCustomizer();
phsc.setBindPort(AvailablePortFinder.getNextAvailable());

String url;
if (ObjectHelper.isEmpty(path)) {
url = "http://localhost:" + inspectorCustomizer.getBindPort() + HealthContextCustomizer.DEFAULT_PATH;
url = "http://localhost:" + phsc.getBindPort() + HealthContextCustomizer.DEFAULT_PATH;
} else {
inspectorCustomizer.setPath(path);
phsc.setPath(path);

url = "http://localhost:" + inspectorCustomizer.getBindPort() + path + HealthContextCustomizer.DEFAULT_PATH;
url = "http://localhost:" + phsc.getBindPort() + path + HealthContextCustomizer.DEFAULT_PATH;
}

inspectorCustomizer.apply(runtime.getCamelContext());
phsc.apply(runtime.getCamelContext());

HealthContextCustomizer healthCustomizer = new HealthContextCustomizer();
healthCustomizer.apply(runtime.getCamelContext());

try {
runtime.getCamelContext().start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>camel-k-runtime-inspector</artifactId>
<artifactId>camel-k-runtime-http</artifactId>

<dependencies>

Expand All @@ -39,6 +39,10 @@
<groupId>org.apache.camel</groupId>
<artifactId>camel-core-engine</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-platform-http</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-runtime-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.k.http;

import io.vertx.ext.web.Router;
import org.apache.camel.CamelContext;
import org.apache.camel.component.platform.http.PlatformHttpConstants;
import org.apache.camel.support.CamelContextHelper;

public class PlatformHttpRouter {
public static final String PLATFORM_HTTP_ROUTER_NAME = PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME + "-router";

private final Router router;

public PlatformHttpRouter(Router router) {
this.router = router;
}

public Router get() {
return router;
}

// **********************
//
// Helpers
//
// **********************

public static PlatformHttpRouter lookup(CamelContext camelContext) {
return CamelContextHelper.mandatoryLookup(
camelContext,
PlatformHttpRouter.PLATFORM_HTTP_ROUTER_NAME,
PlatformHttpRouter.class
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.k.http;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;

import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
import org.apache.camel.CamelContext;
import org.apache.camel.component.platform.http.PlatformHttpConstants;
import org.apache.camel.support.service.ServiceSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final class PlatformHttpServer extends ServiceSupport {
private static final Logger LOGGER = LoggerFactory.getLogger(PlatformHttpServer.class);

private final CamelContext context;
private final PlatformHttpServiceConfiguration configuration;
private final Vertx vertx;
private final ExecutorService executor;

private HttpServer server;

public PlatformHttpServer(CamelContext context, PlatformHttpServiceConfiguration configuration, Vertx vertx, ExecutorService executor) {
this.context = context;
this.configuration = configuration;
this.vertx = vertx;
this.executor = executor;
}

@Override
protected void doStart() throws Exception {
startAsync().toCompletableFuture().join();
}

@Override
protected void doStop() throws Exception {
try {
if (server != null) {
stopAsync().toCompletableFuture().join();
}
} finally {
this.server = null;
}
}

private CompletionStage<Void> startAsync() {
final Router router = Router.router(vertx);
final Router subRouter = Router.router(vertx);

router.route()
.order(Integer.MIN_VALUE)
.handler(ctx -> {
ctx.request().resume();
createBodyHandler().handle(ctx);
});

router.mountSubRouter(configuration.getPath(), subRouter);

context.getRegistry().bind(PlatformHttpRouter.PLATFORM_HTTP_ROUTER_NAME, new PlatformHttpRouter(subRouter));

//HttpServerOptions options = new HttpServerOptions();
if (configuration.getSslContextParameters() != null) {
// TODO: add ssl support
throw new UnsupportedOperationException("Not yet implemented");
}

server = vertx.createHttpServer();

return CompletableFuture.runAsync(
() -> {
CountDownLatch latch = new CountDownLatch(1);
server.requestHandler(router).listen(configuration.getBindPort(), configuration.getBindHost(), result -> {
try {
if (result.failed()) {
LOGGER.warn("Failed to start Vert.x HttpServer on {}:{}, reason: {}",
configuration.getBindHost(),
configuration.getBindPort(),
result.cause().getMessage()
);

throw new RuntimeException(result.cause());
}

LOGGER.info("Vert.x HttpServer started on {}:{}", configuration.getBindHost(), configuration.getBindPort());
} finally {
latch.countDown();
}
});

try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
},
executor
);
}

protected CompletionStage<Void> stopAsync() {
return CompletableFuture.runAsync(
() -> {
CountDownLatch latch = new CountDownLatch(1);

// remove the platform-http component
context.removeComponent(PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME);

server.close(result -> {
try {
if (result.failed()) {
LOGGER.warn("Failed to close Vert.x HttpServer reason: {}",
result.cause().getMessage()
);

throw new RuntimeException(result.cause());
}

LOGGER.info("Vert.x HttpServer stopped");
} finally {
latch.countDown();
}
});

try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
},
executor
);
}

private Handler<RoutingContext> createBodyHandler() {
BodyHandler bodyHandler = BodyHandler.create();

if (configuration.getMaxBodySize() != null) {
bodyHandler.setBodyLimit(configuration.getMaxBodySize().longValueExact());
}

bodyHandler.setHandleFileUploads(configuration.getBodyHandler().isHandleFileUploads());
bodyHandler.setUploadsDirectory(configuration.getBodyHandler().getUploadsDirectory());
bodyHandler.setDeleteUploadedFilesOnEnd(configuration.getBodyHandler().isDeleteUploadedFilesOnEnd());
bodyHandler.setMergeFormAttributes(configuration.getBodyHandler().isMergeFormAttributes());
bodyHandler.setPreallocateBodyBuffer(configuration.getBodyHandler().isPreallocateBodyBuffer());

return new Handler<RoutingContext>() {
@Override
public void handle(RoutingContext event) {
event.request().resume();
bodyHandler.handle(event);
}
};
}
}
Loading

0 comments on commit 0448459

Please sign in to comment.