Skip to content

Commit

Permalink
Add vert-x template
Browse files Browse the repository at this point in the history
Based upon the existing java8/11 template and the work of @k33g

https://gitlab.com/openfaas-experiments/java-8-vert-x-template/

Tested by creating a new function with `faas-cli new` and then
running `faas-cli up` against a Kubernetes cluster with OpenFaaS.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Jan 29, 2020
1 parent e4a831c commit 70eda00
Show file tree
Hide file tree
Showing 15 changed files with 398 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This repository contains the Classic OpenFaaS templates, but many more are avail
| python | Python | 2.7 | Alpine Linux | classic | [Python 2.7 template](https://github.com/openfaas/templates/tree/master/template/python)
| go | Go | 1.11 | Alpine Linux | classic | [Go template](https://github.com/openfaas/templates/tree/master/template/go)
| csharp | C# | N/A | Debian GNU/Linux 9 | classic | [C# template](https://github.com/openfaas/templates/tree/master/template/csharp)
| java11-vert-x | Java and [Vert.x](https://vertx.io/) | 11 | Debian GNU/Linux | of-watchdog | [Java LTS template](https://github.com/openfaas/templates/tree/master/template/java11)
| java11 | Java | 11 | Debian GNU/Linux | of-watchdog | [Java LTS template](https://github.com/openfaas/templates/tree/master/template/java11)
| java8 | Java | 8 | OpenJDK Alpine Linux | of-watchdog | [Java EOL template](https://github.com/openfaas/templates/tree/master/template/java8)
| ruby | Ruby | 2.5.1 | Alpine Linux 3.7 | classic| [Ruby template](https://github.com/openfaas/templates/tree/master/template/ruby)
Expand Down
62 changes: 62 additions & 0 deletions template/java11-vert-x/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
FROM openjdk:11-jdk-slim as builder

ENV GRADLE_VER=6.1.1
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

ENV GRADLE_OPTS="-Dorg.gradle.daemon=false"
WORKDIR /home/app

COPY . /home/app/

RUN gradle build
RUN find .

FROM openfaas/of-watchdog:0.7.6 as watchdog
FROM openjdk:11-jre-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

RUN addgroup --system app \
&& adduser --system --ingroup app app

WORKDIR /home/app/
RUN chown -R app /home/app

USER app

ENV upstream_url="http://127.0.0.1:8082"
ENV mode="http"
ENV CLASSPATH="/home/app/entrypoint-1.0/lib/*"

ENV fprocess="java -XX:+UseContainerSupport com.openfaas.entrypoint.App"
EXPOSE 8080

HEALTHCHECK --interval=5s CMD [ -e /tmp/.lock ] || exit 1

CMD ["fwatchdog"]
58 changes: 58 additions & 0 deletions template/java11-vert-x/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
## Template: java8-vert-x

The Java8-Vert.x template uses gradle as a build system.

Gradle version: 4.8.1

### Structure

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

The handler is written in the `./src/main/java/com/openfaas/function/Handler.java` folder

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.

### Serve a "pure" static html web application

This template allow you to serve static html assets (eg: single page application)

#### First, update your yaml deployment file:

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

```yaml
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.


#### Deployment yaml file sample:

```yaml
provider:
name: faas
gateway: http://openfaas.test:8080
functions:
hello-vert-x:
lang: java8-vert-x
environment:
FRONTAPP: true
handler: ./function
image: registry.test:5000/hello-vert-x:latest
```

22 changes: 22 additions & 0 deletions template/java11-vert-x/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This is a general purpose Gradle build.
* Learn how to create Gradle builds at https://guides.gradle.org/creating-new-gradle-builds/
*/

allprojects {
repositories {
jcenter()
}
}

subprojects {
// apply plugin: 'java'

version = '1.0'

// jar {
// manifest.attributes provider: 'gradle'
// }
}
51 changes: 51 additions & 0 deletions template/java11-vert-x/entrypoint/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/4.8.1/userguide/tutorial_java_projects.html
*/

plugins {
// Apply the java plugin to add support for Java
id 'java'

// Apply the application plugin to add support for building an application
id 'application'

}

// Define the main class for the application
mainClassName = 'App'

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

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

compile project(':function')
}

jar {
manifest {
attributes 'Implementation-Title': 'OpenFaaS Function',
'Implementation-Version': version
}
}

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

uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
10 changes: 10 additions & 0 deletions template/java11-vert-x/entrypoint/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* This file was generated by the Gradle 'init' task.
*
* The settings file is used to specify which projects to include in your build.
*
* Detailed information about configuring a multi-project build in Gradle can be found
* in the user guide at https://docs.gradle.org/4.8.1/userguide/multi_project_builds.html
*/

rootProject.name = 'entrypoint'
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) OpenFaaS Author(s) 2018. All rights reserved.
// 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 io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.ext.web.handler.BodyHandler;
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 {
BodyHandler handler = new com.openfaas.function.Handler();
router.route().handler(handler);
}

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());
}
});
}
}
3 changes: 3 additions & 0 deletions template/java11-vert-x/entrypoint/src/test/java/AppTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class AppTest {

}
27 changes: 27 additions & 0 deletions template/java11-vert-x/function/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java Library project to get you started.
* For more details take a look at the Java Libraries chapter in the Gradle
* user guide available at https://docs.gradle.org/4.8.1/userguide/java_library_plugin.html
*/

plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
}

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

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

// 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()
}
10 changes: 10 additions & 0 deletions template/java11-vert-x/function/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* This file was generated by the Gradle 'init' task.
*
* The settings file is used to specify which projects to include in your build.
*
* Detailed information about configuring a multi-project build in Gradle can be found
* in the user guide at https://docs.gradle.org/4.8.1/userguide/multi_project_builds.html
*/

rootProject.name = 'function'
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.openfaas.function;

import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.core.json.JsonObject;

public class Handler implements BodyHandler {

@Override
public void handle(RoutingContext routingContext) {
routingContext.response()
.putHeader("content-type", "application/json;charset=UTF-8")
.end(
new JsonObject()
.put("status", "ok")
.encodePrettily()
);
}

@Override
public BodyHandler setBodyLimit(long bodyLimit) {
return null;
}

@Override
public BodyHandler setUploadsDirectory(String uploadsDirectory) {
return null;
}

@Override
public BodyHandler setMergeFormAttributes(boolean mergeFormAttributes) {
return null;
}

@Override
public BodyHandler setDeleteUploadedFilesOnEnd(boolean deleteUploadedFilesOnEnd) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>WebApp with Vert.x and Java</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>
.container
{
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
}
.title
{
font-family: "Source Sans Pro", "Helvetica Neue", Arial, sans-serif;
display: block;
font-weight: 300;
font-size: 100px;
color: #35495e;
letter-spacing: 1px;
}
.subtitle
{
font-family: "Source Sans Pro", "Helvetica Neue", Arial, sans-serif;
font-weight: 300;
font-size: 42px;
color: #526488;
word-spacing: 5px;
padding-bottom: 15px;
}
.links
{
padding-top: 15px;
}
</style>
</head>
<body>
<section class="container">
<div>
<h1 class="title">
WebApp with Vert.x and Java
</h1>
<h2 class="subtitle">
Hosted with ❤️ on OpenFaaS
</h2>
</div>
</section>
</body>
</html>
Loading

0 comments on commit 70eda00

Please sign in to comment.