From bbb48ba2f861aa05649c12626620b5c1c07d4a6a Mon Sep 17 00:00:00 2001 From: Paulo Lopes Date: Wed, 2 Mar 2022 11:39:06 +0100 Subject: [PATCH 1/5] Update vert.x template to 4.x and allow full customization Signed-off-by: Paulo Lopes --- template/java11-vert-x/Dockerfile | 12 ++-- template/java11-vert-x/build.gradle | 2 +- .../java11-vert-x/entrypoint/build.gradle | 18 ++--- .../java/com/openfaas/entrypoint/App.java | 71 ++++++++++++------- template/java11-vert-x/function/build.gradle | 7 +- .../java/com/openfaas/function/Handler.java | 17 ----- .../openfaas/function/OpenFaasFunction.java | 38 ++++++++++ .../function/OpenFaasVertxConfig.java | 47 ++++++++++++ .../function/src/test/java/HandlerTest.java | 11 --- .../src/test/java/OpenFaasFunctionTest.java | 38 ++++++++++ 10 files changed, 185 insertions(+), 76 deletions(-) delete mode 100644 template/java11-vert-x/function/src/main/java/com/openfaas/function/Handler.java create mode 100644 template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasFunction.java create mode 100644 template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasVertxConfig.java delete mode 100644 template/java11-vert-x/function/src/test/java/HandlerTest.java create mode 100644 template/java11-vert-x/function/src/test/java/OpenFaasFunctionTest.java diff --git a/template/java11-vert-x/Dockerfile b/template/java11-vert-x/Dockerfile index 7d2d139d..af1a5614 100644 --- a/template/java11-vert-x/Dockerfile +++ b/template/java11-vert-x/Dockerfile @@ -1,6 +1,6 @@ -FROM openjdk:11-jdk-slim as builder +FROM openjdk:17-slim as builder -ENV GRADLE_VER=6.1.1 +ENV GRADLE_VER=7.4 RUN apt-get update -qqy \ && apt-get install -qqy \ --no-install-recommends \ @@ -25,11 +25,11 @@ WORKDIR /home/app COPY . /home/app/ -RUN gradle build +RUN gradle distZip RUN find . FROM ghcr.io/openfaas/of-watchdog:0.9.3 as watchdog -FROM openjdk:11-jre-slim as ship +FROM openjdk:17-slim as ship RUN apt-get update -qqy \ && apt-get install -qqy \ --no-install-recommends \ @@ -52,9 +52,9 @@ USER app ENV upstream_url="http://127.0.0.1:8082" ENV mode="http" -ENV CLASSPATH="/home/app/entrypoint-1.0/lib/*" +ENV JAVA_OPTS="-XX:+UseContainerSupport" -ENV fprocess="java -XX:+UseContainerSupport -Dvertx.cacheDirBase=/tmp/.vertx-cache com.openfaas.entrypoint.App" +ENV fprocess="/home/app/entrypoint-1.0/bin/entrypoint" EXPOSE 8080 HEALTHCHECK --interval=5s CMD [ -e /tmp/.lock ] || exit 1 diff --git a/template/java11-vert-x/build.gradle b/template/java11-vert-x/build.gradle index 6ad512d3..6082adf9 100644 --- a/template/java11-vert-x/build.gradle +++ b/template/java11-vert-x/build.gradle @@ -7,7 +7,7 @@ allprojects { repositories { - jcenter() + mavenCentral() } } diff --git a/template/java11-vert-x/entrypoint/build.gradle b/template/java11-vert-x/entrypoint/build.gradle index e4514524..61934af4 100644 --- a/template/java11-vert-x/entrypoint/build.gradle +++ b/template/java11-vert-x/entrypoint/build.gradle @@ -16,16 +16,16 @@ plugins { } // Define the main class for the application -mainClassName = 'App' +mainClassName = 'com.openfaas.entrypoint.App' dependencies { // Vert.x project - compile 'io.vertx:vertx-web:3.5.4' + implementation 'io.vertx:vertx-web:4.2.4' // Use JUnit test framework - testCompile 'junit:junit:4.12' + testImplementation 'junit:junit:4.13.2' - compile project(':function') + implementation project(':function') } jar { @@ -39,13 +39,5 @@ jar { repositories { // Use jcenter for resolving your dependencies. // You can declare any Maven/Ivy/file repository here. - jcenter() -} - -uploadArchives { - repositories { - flatDir { - dirs 'repos' - } - } + mavenCentral() } diff --git a/template/java11-vert-x/entrypoint/src/main/java/com/openfaas/entrypoint/App.java b/template/java11-vert-x/entrypoint/src/main/java/com/openfaas/entrypoint/App.java index 84c41bed..a4cb6782 100644 --- a/template/java11-vert-x/entrypoint/src/main/java/com/openfaas/entrypoint/App.java +++ b/template/java11-vert-x/entrypoint/src/main/java/com/openfaas/entrypoint/App.java @@ -2,34 +2,55 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. package com.openfaas.entrypoint; -import io.vertx.core.http.HttpServer; -import io.vertx.core.Vertx; +import com.openfaas.function.OpenFaasFunction; +import com.openfaas.function.OpenFaasVertxConfig; +import io.vertx.core.AbstractVerticle; +import io.vertx.core.Promise; +import io.vertx.ext.web.Route; import io.vertx.ext.web.Router; -import io.vertx.ext.web.RoutingContext; -import io.vertx.ext.web.handler.StaticHandler; + import java.util.Optional; -public class App { - public static void main(String[] args) throws Exception { - Vertx vertx = Vertx.vertx(); - Integer httpPort = Integer.parseInt(Optional.ofNullable(System.getenv("PORT")).orElse("8082")); - HttpServer server = vertx.createHttpServer(); - Router router = Router.router(vertx); - - if (Boolean.parseBoolean(Optional.ofNullable(System.getenv("FRONTAPP")).orElse("false"))) { - // serve static assets, see /resources/webroot directory - router.route("/*").handler(StaticHandler.create()); - } else { - io.vertx.core.Handler handler = new com.openfaas.function.Handler(); - router.route().handler(handler); +public class App extends AbstractVerticle { + + private static void fatal(Throwable err) { + err.printStackTrace(); + System.exit(-1); + } + + public static void main(String[] args) { + OpenFaasVertxConfig.newVertx() + .onFailure(App::fatal) + .onSuccess(vertx -> + vertx + .deployVerticle(new App()) + .onFailure(App::fatal)); } - server.requestHandler(router::accept).listen(httpPort, result -> { - if(result.succeeded()) { - System.out.println("Listening on port " + httpPort); - } else { - System.out.println("Unable to start server: " + result.cause().getMessage()); - } - }); - } + @Override + public void start(Promise start) { + final Router router = Router.router(vertx); + final Route route = router.route(); + + OpenFaasVertxConfig.configureRoute(route) + .onFailure(App::fatal) + .onSuccess(v -> { + route.respond(new OpenFaasFunction()); + vertx + .createHttpServer() + .requestHandler(router) + .listen(Integer.parseInt(Optional.ofNullable(System.getenv("PORT")).orElse("8082"))) + .onFailure(App::fatal) + .onSuccess(server -> { + System.out.println("Listening on port " + server.actualPort()); + }); + }); + } + + @Override + public void stop(Promise stop) { + OpenFaasVertxConfig + .shutdown() + .onComplete(stop); + } } diff --git a/template/java11-vert-x/function/build.gradle b/template/java11-vert-x/function/build.gradle index 2e8b4339..5425e198 100644 --- a/template/java11-vert-x/function/build.gradle +++ b/template/java11-vert-x/function/build.gradle @@ -13,15 +13,16 @@ plugins { dependencies { // Vert.x project - compile 'io.vertx:vertx-web:3.5.4' + implementation 'io.vertx:vertx-web:4.2.4' // Use JUnit test framework - testImplementation 'junit:junit:4.12' + testImplementation 'io.vertx:vertx-unit:4.2.4' + testImplementation 'junit:junit:4.13.2' } // In this section you declare where to find the dependencies of your project repositories { // Use jcenter for resolving your dependencies. // You can declare any Maven/Ivy/file repository here. - jcenter() + mavenCentral() } diff --git a/template/java11-vert-x/function/src/main/java/com/openfaas/function/Handler.java b/template/java11-vert-x/function/src/main/java/com/openfaas/function/Handler.java deleted file mode 100644 index 6736fc28..00000000 --- a/template/java11-vert-x/function/src/main/java/com/openfaas/function/Handler.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.openfaas.function; - -import io.vertx.ext.web.RoutingContext; -import io.vertx.core.json.JsonObject; - -public class Handler implements io.vertx.core.Handler { - - public void handle(RoutingContext routingContext) { - routingContext.response() - .putHeader("content-type", "application/json;charset=UTF-8") - .end( - new JsonObject() - .put("status", "ok") - .encodePrettily() - ); - } -} diff --git a/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasFunction.java b/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasFunction.java new file mode 100644 index 00000000..ff4cffa9 --- /dev/null +++ b/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasFunction.java @@ -0,0 +1,38 @@ +package com.openfaas.function; + +import io.vertx.core.Future; +import io.vertx.ext.web.RoutingContext; +import io.vertx.core.json.JsonObject; + +import java.util.function.Function; + +/** + * The OpenFaas function. The function must implement the interface {@code Function>}. + * + * Note that there are 3 optional return types: + * + *
    + *
  • {@code Future}
  • + *
  • {@code Future}
  • + *
  • {@code Future}
  • + *
+ * + * When any of these types is returned, the corresponding mime type header will be set: + * + *
    + *
  • {@code application/json}
  • + *
  • {@code application/octet-stream}
  • + *
  • {@code text/plain}
  • + *
+ * + * To customize the mime type, call the setter on {@link RoutingContext} and your choice is respected. + */ +public class OpenFaasFunction implements Function> { + + public Future apply(RoutingContext routingContext) { + return Future.succeededFuture( + new JsonObject() + .put("status", "ok") + ); + } +} diff --git a/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasVertxConfig.java b/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasVertxConfig.java new file mode 100644 index 00000000..7e48363d --- /dev/null +++ b/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasVertxConfig.java @@ -0,0 +1,47 @@ +package com.openfaas.function; + +import io.vertx.core.Future; +import io.vertx.core.Vertx; +import io.vertx.ext.web.Route; +import io.vertx.ext.web.handler.BodyHandler; +import io.vertx.ext.web.handler.StaticHandler; + +import java.util.Optional; + +/** + * Use this class to configure your vert.x function server instance. + */ +public final class OpenFaasVertxConfig { + + /** + * Factory to create a vert.x instance, by default users should not need to change the default implementation of + * this method, however, when certain configuration is needed, for example, clustering support, custom event loop + * sizes, DNS resolvers, etc... This method can be used to return the desired Vertx instance. + */ + public static Future newVertx() { + return Future.succeededFuture(Vertx.vertx()); + } + + /** + * Allow configuration of the route used to handle the function requests. + */ + public static Future configureRoute(Route route) { + // allow this function to serve static files can be achieved with: + if (Boolean.parseBoolean(Optional.ofNullable(System.getenv("FRONTAPP")).orElse("false"))) { + // serve static assets, see /resources/webroot directory + route.handler(StaticHandler.create()); + } + // allow this function to safely process the request body can be achieved with: + route.handler(BodyHandler.create()); + + return Future.succeededFuture(); + } + + /** + * When the function is to be shutdown by the watchdog, if there are any clean up actions to be performed, they can + * be invoked from this function. + */ + public static Future shutdown() { + return Future.succeededFuture(); + } +} diff --git a/template/java11-vert-x/function/src/test/java/HandlerTest.java b/template/java11-vert-x/function/src/test/java/HandlerTest.java deleted file mode 100644 index 99ab40a7..00000000 --- a/template/java11-vert-x/function/src/test/java/HandlerTest.java +++ /dev/null @@ -1,11 +0,0 @@ -import org.junit.Test; -import static org.junit.Assert.*; - -import com.openfaas.function.Handler; - -public class HandlerTest { - @Test public void handlerIsNotNull() { - Handler handler = new Handler(); - assertTrue("Expected handler not to be null", handler != null); - } -} diff --git a/template/java11-vert-x/function/src/test/java/OpenFaasFunctionTest.java b/template/java11-vert-x/function/src/test/java/OpenFaasFunctionTest.java new file mode 100644 index 00000000..e8c9881b --- /dev/null +++ b/template/java11-vert-x/function/src/test/java/OpenFaasFunctionTest.java @@ -0,0 +1,38 @@ +import io.vertx.ext.unit.Async; +import io.vertx.ext.unit.TestContext; +import io.vertx.ext.unit.junit.RunTestOnContext; +import io.vertx.ext.unit.junit.VertxUnitRunner; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.*; + +import com.openfaas.function.OpenFaasFunction; +import org.junit.runner.RunWith; + +@RunWith(VertxUnitRunner.class) +public class OpenFaasFunctionTest { + + @Rule + public RunTestOnContext rule = new RunTestOnContext(); + + @Test + public void handlerIsNotNull() { + OpenFaasFunction handler = new OpenFaasFunction(); + assertTrue("Expected handler not to be null", handler != null); + } + + @Test + public void resultIsJson(TestContext should) { + Async test = should.async(); + + OpenFaasFunction handler = new OpenFaasFunction(); + handler.apply(null) + .onFailure(should::fail) + .onSuccess(res -> { + should.assertTrue(res != null); + should.assertEquals("ok", res.getString("status")); + test.complete(); + }); + } +} From f26381eca62743fb6da99449e1826c047c450212 Mon Sep 17 00:00:00 2001 From: Paulo Lopes Date: Wed, 2 Mar 2022 15:18:01 +0100 Subject: [PATCH 2/5] Update readme and add missing annotation Signed-off-by: Paulo Lopes --- template/java11-vert-x/README.md | 9 +++++---- .../java/com/openfaas/function/OpenFaasFunction.java | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/template/java11-vert-x/README.md b/template/java11-vert-x/README.md index a407c0c5..07493574 100644 --- a/template/java11-vert-x/README.md +++ b/template/java11-vert-x/README.md @@ -2,7 +2,7 @@ The Java11-Vert.x template uses gradle as a build system. -Gradle version: 4.8.1 +Gradle version: 7.4 ### Structure @@ -11,15 +11,16 @@ There are two projects which make up a single gradle build: - function - (Library) your function code as a developer, you will only ever see this folder - entrypoint - (App) Vert.x HTTP server -### Handler +### Function -The handler is written in the `./src/main/java/com/openfaas/function/Handler.java` folder +The function is written in the `./src/main/java/com/openfaas/function/OpenFaasFunction.java` file Tests are supported with junit via files in `./src/test` ### External dependencies -External dependencies can be specified in ./build.gradle in the normal way using jcenter, a local JAR or some other remote repository. +External dependencies can be specified in ./build.gradle in the normal way using mavenCentral, a local JAR or some other +remote repository. ### Serve a "pure" static html web application diff --git a/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasFunction.java b/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasFunction.java index ff4cffa9..9fb16bb0 100644 --- a/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasFunction.java +++ b/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasFunction.java @@ -22,13 +22,14 @@ *
    *
  • {@code application/json}
  • *
  • {@code application/octet-stream}
  • - *
  • {@code text/plain}
  • + *
  • {@code text/html}
  • *
* * To customize the mime type, call the setter on {@link RoutingContext} and your choice is respected. */ public class OpenFaasFunction implements Function> { + @Override public Future apply(RoutingContext routingContext) { return Future.succeededFuture( new JsonObject() From a4e0778bfbda8f2f9aa2e282dc9a2781596ad770 Mon Sep 17 00:00:00 2001 From: Paulo Lopes Date: Wed, 2 Mar 2022 21:53:01 +0100 Subject: [PATCH 3/5] Update PR based on community discussions Signed-off-by: Paulo Lopes --- template/java11-vert-x/Dockerfile | 12 +--- template/java11-vert-x/README.md | 15 ++++- .../java11-vert-x/entrypoint/build.gradle | 12 +++- .../java/com/openfaas/entrypoint/App.java | 63 +++++++++---------- .../java/com/openfaas/function/Handler.java | 13 ++++ .../openfaas/function/OpenFaasFunction.java | 39 ------------ .../function/OpenFaasVertxConfig.java | 47 -------------- .../function/src/test/java/HandlerTest.java | 23 +++++++ .../src/test/java/OpenFaasFunctionTest.java | 38 ----------- 9 files changed, 92 insertions(+), 170 deletions(-) create mode 100644 template/java11-vert-x/function/src/main/java/com/openfaas/function/Handler.java delete mode 100644 template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasFunction.java delete mode 100644 template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasVertxConfig.java create mode 100644 template/java11-vert-x/function/src/test/java/HandlerTest.java delete mode 100644 template/java11-vert-x/function/src/test/java/OpenFaasFunctionTest.java diff --git a/template/java11-vert-x/Dockerfile b/template/java11-vert-x/Dockerfile index af1a5614..5f274f04 100644 --- a/template/java11-vert-x/Dockerfile +++ b/template/java11-vert-x/Dockerfile @@ -25,22 +25,17 @@ WORKDIR /home/app COPY . /home/app/ -RUN gradle distZip +RUN gradle shadowJar RUN find . FROM ghcr.io/openfaas/of-watchdog:0.9.3 as watchdog FROM openjdk:17-slim as ship -RUN apt-get update -qqy \ - && apt-get install -qqy \ - --no-install-recommends \ - unzip COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog RUN chmod +x /usr/bin/fwatchdog WORKDIR /home/app -COPY --from=builder /home/app/entrypoint/build/distributions/entrypoint-1.0.zip ./entrypoint-1.0.zip -RUN unzip ./entrypoint-1.0.zip +COPY --from=builder /home/app/entrypoint/build/libs/entrypoint-1.0-all.jar ./entrypoint-1.0.jar RUN addgroup --system app \ && adduser --system --ingroup app app @@ -52,9 +47,8 @@ USER app ENV upstream_url="http://127.0.0.1:8082" ENV mode="http" -ENV JAVA_OPTS="-XX:+UseContainerSupport" -ENV fprocess="/home/app/entrypoint-1.0/bin/entrypoint" +ENV fprocess="java -XX:+UseContainerSupport -jar /home/app/entrypoint-1.0.jar" EXPOSE 8080 HEALTHCHECK --interval=5s CMD [ -e /tmp/.lock ] || exit 1 diff --git a/template/java11-vert-x/README.md b/template/java11-vert-x/README.md index 07493574..a4122843 100644 --- a/template/java11-vert-x/README.md +++ b/template/java11-vert-x/README.md @@ -13,10 +13,21 @@ There are two projects which make up a single gradle build: ### Function -The function is written in the `./src/main/java/com/openfaas/function/OpenFaasFunction.java` file +The function is written in the `./src/main/java/com/openfaas/function/Handler.java` file Tests are supported with junit via files in `./src/test` +### Body Parsing + +HTTP Request body is disabled by default. This means that the environment variable `RAW_BODY` is set either missing or +set to `true`. When the variable is explicitly set to `false`, then vert.x internal BodyHandler is used. + +#### Limiting Body Length + +By default, vert.x allows any request body length. For security reasons or resource constraints, you might want to limit +this value. In order to do this set the `BODY_MAX_SIZE` environment variable to the allowed number of bytes, or `-1` for +unlimited. + ### External dependencies External dependencies can be specified in ./build.gradle in the normal way using mavenCentral, a local JAR or some other @@ -53,6 +64,8 @@ functions: lang: java11-vert-x environment: FRONTAPP: true + RAW_BODY: false + MAX_BODY_SIZE: 102400 handler: ./function image: registry.test:5000/hello-vert-x:latest ``` diff --git a/template/java11-vert-x/entrypoint/build.gradle b/template/java11-vert-x/entrypoint/build.gradle index 61934af4..c154f4e5 100644 --- a/template/java11-vert-x/entrypoint/build.gradle +++ b/template/java11-vert-x/entrypoint/build.gradle @@ -13,10 +13,11 @@ plugins { // Apply the application plugin to add support for building an application id 'application' + id("com.github.johnrengelman.shadow") version "6.0.0" } // Define the main class for the application -mainClassName = 'com.openfaas.entrypoint.App' +mainClassName = 'io.vertx.core.Launcher' dependencies { // Vert.x project @@ -30,8 +31,13 @@ dependencies { jar { manifest { - attributes 'Implementation-Title': 'OpenFaaS Function', - 'Implementation-Version': version + attributes( + 'Implementation-Title': 'OpenFaaS Function', + 'Implementation-Version': archiveVersion, + 'Main-Class': mainClassName, + 'Main-Command': 'run', + 'Main-Verticle': 'com.openfaas.entrypoint.App' + ) } } diff --git a/template/java11-vert-x/entrypoint/src/main/java/com/openfaas/entrypoint/App.java b/template/java11-vert-x/entrypoint/src/main/java/com/openfaas/entrypoint/App.java index a4cb6782..14ec5f58 100644 --- a/template/java11-vert-x/entrypoint/src/main/java/com/openfaas/entrypoint/App.java +++ b/template/java11-vert-x/entrypoint/src/main/java/com/openfaas/entrypoint/App.java @@ -2,29 +2,23 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. package com.openfaas.entrypoint; -import com.openfaas.function.OpenFaasFunction; -import com.openfaas.function.OpenFaasVertxConfig; +import com.openfaas.function.Handler; import io.vertx.core.AbstractVerticle; import io.vertx.core.Promise; import io.vertx.ext.web.Route; import io.vertx.ext.web.Router; +import io.vertx.ext.web.handler.BodyHandler; +import io.vertx.ext.web.handler.StaticHandler; +import java.util.Map; import java.util.Optional; public class App extends AbstractVerticle { - private static void fatal(Throwable err) { + private void fatal(Throwable err) { err.printStackTrace(); - System.exit(-1); - } - - public static void main(String[] args) { - OpenFaasVertxConfig.newVertx() - .onFailure(App::fatal) - .onSuccess(vertx -> - vertx - .deployVerticle(new App()) - .onFailure(App::fatal)); + vertx.close() + .onComplete(ar -> System.exit(-1)); } @Override @@ -32,25 +26,28 @@ public void start(Promise start) { final Router router = Router.router(vertx); final Route route = router.route(); - OpenFaasVertxConfig.configureRoute(route) - .onFailure(App::fatal) - .onSuccess(v -> { - route.respond(new OpenFaasFunction()); - vertx - .createHttpServer() - .requestHandler(router) - .listen(Integer.parseInt(Optional.ofNullable(System.getenv("PORT")).orElse("8082"))) - .onFailure(App::fatal) - .onSuccess(server -> { - System.out.println("Listening on port " + server.actualPort()); - }); - }); - } - - @Override - public void stop(Promise stop) { - OpenFaasVertxConfig - .shutdown() - .onComplete(stop); + final Map env = System.getenv(); + // FRONTAPP + if (Boolean.parseBoolean(env.getOrDefault("FRONTAPP", "false"))) { + // serve static assets, see /resources/webroot directory + route.handler(StaticHandler.create()); + } else { + // RAW_BODY + final boolean rawBody = Boolean.parseBoolean(env.getOrDefault("RAW_BODY", "true")); + if (!rawBody) { + final int maxBodySize = Integer.parseInt(env.getOrDefault("RAW_BODY_SIZE", "-1")); + route.handler(BodyHandler.create().setBodyLimit(maxBodySize)); + } + final Handler fn = new Handler(); + route + .handler(fn::handle); + } + + vertx + .createHttpServer() + .requestHandler(router) + .listen(Integer.parseInt(Optional.ofNullable(System.getenv("PORT")).orElse("8082"))) + .onFailure(this::fatal) + .onSuccess(server -> System.out.println("Listening on port " + server.actualPort())); } } diff --git a/template/java11-vert-x/function/src/main/java/com/openfaas/function/Handler.java b/template/java11-vert-x/function/src/main/java/com/openfaas/function/Handler.java new file mode 100644 index 00000000..2aa60a76 --- /dev/null +++ b/template/java11-vert-x/function/src/main/java/com/openfaas/function/Handler.java @@ -0,0 +1,13 @@ +package com.openfaas.function; + +import io.vertx.ext.web.RoutingContext; +import io.vertx.core.json.JsonObject; + +public class Handler { + + public void handle(RoutingContext ctx) { + ctx.json( + new JsonObject() + .put("status", "ok")); + } +} diff --git a/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasFunction.java b/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasFunction.java deleted file mode 100644 index 9fb16bb0..00000000 --- a/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasFunction.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.openfaas.function; - -import io.vertx.core.Future; -import io.vertx.ext.web.RoutingContext; -import io.vertx.core.json.JsonObject; - -import java.util.function.Function; - -/** - * The OpenFaas function. The function must implement the interface {@code Function>}. - * - * Note that there are 3 optional return types: - * - *
    - *
  • {@code Future}
  • - *
  • {@code Future}
  • - *
  • {@code Future}
  • - *
- * - * When any of these types is returned, the corresponding mime type header will be set: - * - *
    - *
  • {@code application/json}
  • - *
  • {@code application/octet-stream}
  • - *
  • {@code text/html}
  • - *
- * - * To customize the mime type, call the setter on {@link RoutingContext} and your choice is respected. - */ -public class OpenFaasFunction implements Function> { - - @Override - public Future apply(RoutingContext routingContext) { - return Future.succeededFuture( - new JsonObject() - .put("status", "ok") - ); - } -} diff --git a/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasVertxConfig.java b/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasVertxConfig.java deleted file mode 100644 index 7e48363d..00000000 --- a/template/java11-vert-x/function/src/main/java/com/openfaas/function/OpenFaasVertxConfig.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.openfaas.function; - -import io.vertx.core.Future; -import io.vertx.core.Vertx; -import io.vertx.ext.web.Route; -import io.vertx.ext.web.handler.BodyHandler; -import io.vertx.ext.web.handler.StaticHandler; - -import java.util.Optional; - -/** - * Use this class to configure your vert.x function server instance. - */ -public final class OpenFaasVertxConfig { - - /** - * Factory to create a vert.x instance, by default users should not need to change the default implementation of - * this method, however, when certain configuration is needed, for example, clustering support, custom event loop - * sizes, DNS resolvers, etc... This method can be used to return the desired Vertx instance. - */ - public static Future newVertx() { - return Future.succeededFuture(Vertx.vertx()); - } - - /** - * Allow configuration of the route used to handle the function requests. - */ - public static Future configureRoute(Route route) { - // allow this function to serve static files can be achieved with: - if (Boolean.parseBoolean(Optional.ofNullable(System.getenv("FRONTAPP")).orElse("false"))) { - // serve static assets, see /resources/webroot directory - route.handler(StaticHandler.create()); - } - // allow this function to safely process the request body can be achieved with: - route.handler(BodyHandler.create()); - - return Future.succeededFuture(); - } - - /** - * When the function is to be shutdown by the watchdog, if there are any clean up actions to be performed, they can - * be invoked from this function. - */ - public static Future shutdown() { - return Future.succeededFuture(); - } -} diff --git a/template/java11-vert-x/function/src/test/java/HandlerTest.java b/template/java11-vert-x/function/src/test/java/HandlerTest.java new file mode 100644 index 00000000..37e1bfb6 --- /dev/null +++ b/template/java11-vert-x/function/src/test/java/HandlerTest.java @@ -0,0 +1,23 @@ +import io.vertx.ext.unit.TestContext; +import io.vertx.ext.unit.junit.RunTestOnContext; +import io.vertx.ext.unit.junit.VertxUnitRunner; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.*; + +import com.openfaas.function.Handler; +import org.junit.runner.RunWith; + +@RunWith(VertxUnitRunner.class) +public class HandlerTest { + + @Rule + public RunTestOnContext rule = new RunTestOnContext(); + + @Test + public void handlerIsNotNull(TestContext should) { + Handler handler = new Handler(); + assertTrue("Expected handler not to be null", handler != null); + } +} diff --git a/template/java11-vert-x/function/src/test/java/OpenFaasFunctionTest.java b/template/java11-vert-x/function/src/test/java/OpenFaasFunctionTest.java deleted file mode 100644 index e8c9881b..00000000 --- a/template/java11-vert-x/function/src/test/java/OpenFaasFunctionTest.java +++ /dev/null @@ -1,38 +0,0 @@ -import io.vertx.ext.unit.Async; -import io.vertx.ext.unit.TestContext; -import io.vertx.ext.unit.junit.RunTestOnContext; -import io.vertx.ext.unit.junit.VertxUnitRunner; -import org.junit.Rule; -import org.junit.Test; - -import static org.junit.Assert.*; - -import com.openfaas.function.OpenFaasFunction; -import org.junit.runner.RunWith; - -@RunWith(VertxUnitRunner.class) -public class OpenFaasFunctionTest { - - @Rule - public RunTestOnContext rule = new RunTestOnContext(); - - @Test - public void handlerIsNotNull() { - OpenFaasFunction handler = new OpenFaasFunction(); - assertTrue("Expected handler not to be null", handler != null); - } - - @Test - public void resultIsJson(TestContext should) { - Async test = should.async(); - - OpenFaasFunction handler = new OpenFaasFunction(); - handler.apply(null) - .onFailure(should::fail) - .onSuccess(res -> { - should.assertTrue(res != null); - should.assertEquals("ok", res.getString("status")); - test.complete(); - }); - } -} From 514aaf7c5d0b4c7360c721f715e1376dae5be25d Mon Sep 17 00:00:00 2001 From: Paulo Lopes Date: Wed, 2 Mar 2022 21:59:35 +0100 Subject: [PATCH 4/5] Revert JDK bump Signed-off-by: Paulo Lopes --- template/java11-vert-x/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/template/java11-vert-x/Dockerfile b/template/java11-vert-x/Dockerfile index 5f274f04..30862229 100644 --- a/template/java11-vert-x/Dockerfile +++ b/template/java11-vert-x/Dockerfile @@ -1,4 +1,4 @@ -FROM openjdk:17-slim as builder +FROM openjdk:11-slim as builder ENV GRADLE_VER=7.4 RUN apt-get update -qqy \ @@ -29,7 +29,7 @@ RUN gradle shadowJar RUN find . FROM ghcr.io/openfaas/of-watchdog:0.9.3 as watchdog -FROM openjdk:17-slim as ship +FROM openjdk:11-slim as ship COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog RUN chmod +x /usr/bin/fwatchdog From 317ff6de686776b064c9e306818cc7199dcdd421 Mon Sep 17 00:00:00 2001 From: Paulo Lopes Date: Fri, 4 Mar 2022 13:51:10 +0100 Subject: [PATCH 5/5] Use official gradle image Signed-off-by: Paulo Lopes --- template/java11-vert-x/Dockerfile | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/template/java11-vert-x/Dockerfile b/template/java11-vert-x/Dockerfile index 30862229..0ee354cb 100644 --- a/template/java11-vert-x/Dockerfile +++ b/template/java11-vert-x/Dockerfile @@ -1,24 +1,4 @@ -FROM openjdk:11-slim as builder - -ENV GRADLE_VER=7.4 -RUN apt-get update -qqy \ - && apt-get install -qqy \ - --no-install-recommends \ - curl \ - ca-certificates \ - unzip - -RUN mkdir -p /opt/ && cd /opt/ \ - && echo "Downloading gradle.." \ - && curl -sSfL "https://services.gradle.org/distributions/gradle-${GRADLE_VER}-bin.zip" -o gradle-$GRADLE_VER-bin.zip \ - && unzip gradle-$GRADLE_VER-bin.zip -d /opt/ \ - && rm gradle-$GRADLE_VER-bin.zip - -# Export some environment variables -ENV GRADLE_HOME=/opt/gradle-$GRADLE_VER/ -ENV PATH=$PATH:$GRADLE_HOME/bin - -RUN mkdir -p /home/app/libs +FROM gradle:7-jdk11 as builder ENV GRADLE_OPTS="-Dorg.gradle.daemon=false" WORKDIR /home/app