-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Based upon the existing java8/11 template and the work of @k33g https://gitlab.com/openfaas-experiments/java-8-vert-x-template/ Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
- Loading branch information
Showing
15 changed files
with
400 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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/ | ||
|
||
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
37 changes: 37 additions & 0 deletions
37
template/java11-vert-x/entrypoint/src/main/java/com/openfaas/entrypoint/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// 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("8081")); | ||
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()); | ||
} | ||
}); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public class AppTest { | ||
// WIP 🚧 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
41 changes: 41 additions & 0 deletions
41
template/java11-vert-x/function/src/main/java/com/openfaas/function/Handler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
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; | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
template/java11-vert-x/function/src/main/resources/webroot/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.