Skip to content
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

Modernize the vert.x template #264

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 16 additions & 15 deletions template/java11-vert-x/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,17 @@ 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
### FaaSFunction

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/FaaSFunction.java` folder

Tests are supported with junit via files in `./src/test`

### External dependencies
The function can optionally implement the interface `Handler<RoutingContext>`. When this is true, the entrypoint will mount the function on any HTTP verb at the end of the router.

External dependencies can be specified in ./build.gradle in the normal way using jcenter, a local JAR or some other remote repository.

### Serve a "pure" static html web application
Helper handlers can be added to the router in the `setUp()` method. This is useful for example to enable the `BodyHandler` which will parse request body of requests.

This template allow you to serve static html assets (eg: single page application)
#### Serve a "pure" static html web application

#### First, update your yaml deployment file:
This template can serve static html assets (eg: single page application). This is an example of using the `setUp()` example.

You only need to add to the `environment` key, a `FRONTAPP` variable (with a value set to `true`)

Expand All @@ -34,14 +30,19 @@ environment:
FRONTAPP: true
```

#### Then add assets in the webroot directory

Put your static assets in this directory: `/src/main/resources/webroot`

> If `FRONTAPP` is set to `false` (or does not exist), it's the `Handler` instance that serves the data.
> If `FRONTAPP` is set to `false` (or does not exist), it's the `FaaSFunction` instance that serves the data.

#### Testing the function

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.

#### Deployment yaml file sample:
### Deployment yaml file sample:

```yaml
provider:
Expand All @@ -51,7 +52,7 @@ functions:
hello-vert-x:
lang: java11-vert-x
environment:
FRONTAPP: true
FRONTAPP: false
handler: ./function
image: registry.test:5000/hello-vert-x:latest
```
Expand Down
4 changes: 2 additions & 2 deletions template/java11-vert-x/entrypoint/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ mainClassName = 'App'

dependencies {
// Vert.x project
compile 'io.vertx:vertx-web:3.5.4'
compile 'io.vertx:vertx-web:4.1.2'

// Use JUnit test framework
testCompile 'junit:junit:4.12'
testCompile 'junit:junit:4.13.1'

compile project(':function')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package com.openfaas.entrypoint;

import com.openfaas.function.FaaSFunction;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpServer;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
Expand All @@ -10,26 +12,39 @@
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);
public static void main(String[] args) {
final Vertx vertx = Vertx.vertx();
final 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<RoutingContext> handler = new com.openfaas.function.Handler();
router.route().handler(handler);
try {
final FaaSFunction fn = new FaaSFunction();
fn.setUp(vertx, router)
.onFailure(App::fail)
.onSuccess(ready -> {
Object ref = fn;
if (ref instanceof Handler) {
router.route()
.handler((Handler<RoutingContext>) ref);
}
listen(vertx, router);
});
} catch (RuntimeException e) {
fail(e);
}
}

private static void listen(Vertx vertx, Router router) {
int httpPort = Integer.parseInt(Optional.ofNullable(System.getenv("PORT")).orElse("8082"));
HttpServer server = vertx.createHttpServer();

server.requestHandler(router)
.listen(httpPort)
.onFailure(App::fail)
.onSuccess(res -> System.out.println("Listening on port " + httpPort));
}

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());
}
});
private static void fail(Throwable err) {
err.printStackTrace();
System.exit(1);
}
}
4 changes: 2 additions & 2 deletions template/java11-vert-x/function/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ plugins {

dependencies {
// Vert.x project
compile 'io.vertx:vertx-web:3.5.4'
compile 'io.vertx:vertx-web:4.1.2'

// Use JUnit test framework
testImplementation 'junit:junit:4.12'
testImplementation 'junit:junit:4.13.1'
}

// In this section you declare where to find the dependencies of your project
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.openfaas.function;

import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.handler.StaticHandler;

import java.util.Optional;

public class FaaSFunction implements Handler<RoutingContext> {

public Future<Void> setUp(Vertx vertx, Router router) {
// allow customization of the router
// for example, mount extra handlers like BodyHandler, CORS, etc...

if (Boolean.parseBoolean(Optional.ofNullable(System.getenv("FRONTAPP")).orElse("false"))) {
// serve static assets, see /resources/webroot directory
router.route()
.handler(StaticHandler.create());
}

return Future.succeededFuture();
}

public void handle(RoutingContext ctx) {
ctx.json(
new JsonObject()
.put("status", "ok"));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import com.openfaas.function.FaaSFunction;
import org.junit.Test;
import static org.junit.Assert.*;

public class FaaSFunctionTest {
@Test public void handlerIsNotNull() {
FaaSFunction function = new FaaSFunction();
assertTrue("Expected function not to be null", function != null);
}
}
11 changes: 0 additions & 11 deletions template/java11-vert-x/function/src/test/java/HandlerTest.java

This file was deleted.