Skip to content

Commit

Permalink
Merge pull request #193 from fnproject/mww-190404-reverse-up
Browse files Browse the repository at this point in the history
Update Java FDK string-reverse example
  • Loading branch information
michael-w-williams authored May 8, 2019
2 parents 45da3e0 + 79daa42 commit fdac7f9
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 80 deletions.
20 changes: 10 additions & 10 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
# `fn` Java FDK Example Projects
# Fn Java FDK Example Projects

In this directory you will find some example projects demonstrating different
features of the `fn` Java FDK:
features of the Fn Java FDK:

* Plain java code support (`string-reverse`)
* Functional testing of your functions (`regex-query` and `qr-code`)
* Built-in JSON coercion (`regex-query`)
* [InputEvent and OutputEvent](/docs/DataBinding.md) handling (`qr-code`)

## 1. String reverse
## (1) [String reverse](string-reverse/README.md)

This function takes a string and returns the reverse of the string.
The `fn` Java FDK runtime will handle marshalling data into your
functions without the function having to have any knowledge of the FDK API.
The Fn Java FDK handles marshalling data into your
functions without the function having any knowledge of the FDK API.

## 2. Regex query
## (2) Regex query

This function takes a JSON object containing a `text` field and a `regex`
field and return a JSON object with a list of matches in the `matches`
field. It demonstrates the builtin JSON support of the fn Java
wrapper (provided through Jackson) and how the platform handles serialisation
wrapper (provided through Jackson) and how the platform handles serialization
of POJO return values.

## 3. QR Code gen
## (3) QR Code gen

This function parses the query parameters of a GET request (through the
`InputEvent` passed into the function) to generate a QR code. It demonstrates
the `InputEvent` and `OutputEvent` interfaces which provide low level
access to data entering the `fn` Java FDK.

## 4. Asynchronous thumbnails generation
## (4) Asynchronous thumbnails generation

This example showcases the Fn Flow asynchronous execution API, by
creating a workflow that takes an image and asynchronously generates three
thumbnails for it, then uploads them to an object storage.

## 5. Gradle build
## (5) Gradle build
This shows how to use Gradle to build functions using the Java FDK.
60 changes: 32 additions & 28 deletions examples/string-reverse/README.md
Original file line number Diff line number Diff line change
@@ -1,71 +1,75 @@
# Example oFunctions Project: String Reverse
# Example Java Function: String Reverse

This example provides an HTTP endpoint for reversing strings
This example provides an HTTP trigger endpoint for reversing strings.

```bash
$ curl -d "Hello, World!" "http://localhost:8080/r/string-reverse-app/reverse"
!dlroW ,olleH
$ curl -d "Hello World" http://localhost:8080/t/string-reverse-app/string-reverse
dlroW olleH
```


## Demonstrated FDK features

This example uses **no** features of the fn Java FDK; in fact it doesn't have
a dependency on the fn Java FDK, it just plain old Java code.
This example uses **none** of the Fn Java FDK features, in fact it doesn't have
any dependency on the Fn Java FDK. It is just plain old Java code.

## Step by step

Ensure you have the functions server running using, this will host your
function and provide the HTTP endpoints that invoke it:
Ensure you have the Fn server running to host your
function and provide the HTTP endpoint that invokes it:

```bash
(1) Start the server

```sh
$ fn start
```

Build the function locally
(2) Create an app for the function

```bash
$ fn build
```sh
$ fn create app string-reverse-app
```

Create an app and route to host the function
(3) Deploy the function to your app from the `string-reverse` directory.

```bash
$ fn create app string-reverse-app
$ fn create route string-reverse-app /reverse
```sh
fn deploy --app string-reverse-app --local
```

(4) Invoke the function and reverse the string.

```sh
echo "Hello World" | fn invoke string-reverse-app string-reverse
dlroW olleH
```

Invoke the function to reverse a string
(5) Invoke the function using curl and a trigger to reverse a string.

```bash
$ curl -d "Hello, World!" "http://localhost:8080/r/string-reverse-app/reverse"
!dlroW ,olleH
$ curl -d "Hello World" http://localhost:8080/t/string-reverse-app/string-reverse
dlroW olleH
```


## Code walkthrough

The entrypoint to the function is specified in `func.yaml` in the `cmd` key.
It is set this to `com.fnproject.fn.examples.StringReverse::reverse`. The whole class
It is set this to `com.example.fn.StringReverse::reverse`. The whole class
`StringReverse` is shown below:


```java
package com.fnproject.fn.examples;
package com.example.fn;

public class StringReverse {
public String reverse(String str) {
StringBuilder builder = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
builder.append(str.charAt(i));
}
return builder.toString();
return new StringBuilder(str).reverse().toString();
}
}
```

As you can see, this is plain java with no references to the fn API. The
fn Java FDK handles the marshalling of the HTTP body into the `str`
As you can see, this is plain java with no references to the Fn API. The
Fn Java FDK handles the marshalling of the HTTP body into the `str`
parameter as well as the marshalling of the returned reversed string into the HTTP
response body (see [Data Binding](/docs/DataBinding.md) for more
information on how marshalling is performed).
Expand Down
16 changes: 10 additions & 6 deletions examples/string-reverse/func.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
name: fn-example/string-reverse
version: 0.0.1
schema_version: 20180708
name: string-reverse
version: 0.0.2
runtime: java
timeout: 30
format: http
cmd: com.fnproject.fn.examples.StringReverse::reverse
path: /reverse
build_image: fnproject/fn-java-fdk-build:jdk11-1.0.87
run_image: fnproject/fn-java-fdk:jre11-1.0.87
cmd: com.example.fn.StringReverse::reverse
triggers:
- name: string-reverse
type: http
source: /string-reverse
66 changes: 43 additions & 23 deletions examples/string-reverse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,50 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<fdk.version>1.0.87</fdk.version>
</properties>

<groupId>com.fnproject.fn.examples</groupId>
<groupId>com.example.fn</groupId>
<artifactId>string-reverse</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.0</version>

<repositories>
<repository>
<id>fn-release-repo</id>
<url>https://dl.bintray.com/fnproject/fnproject</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.fnproject.fn</groupId>
<artifactId>api</artifactId>
<version>${fdk.version}</version>
</dependency>
<dependency>
<groupId>com.fnproject.fn</groupId>
<artifactId>testing-core</artifactId>
<version>${fdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fnproject.fn</groupId>
<artifactId>testing-junit4</artifactId>
<version>${fdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand All @@ -28,27 +55,20 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>fn-maven-releases</id>
<url>https://dl.bintray.com/fnproject/fnproject</url>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.fn;

public class StringReverse {
public String reverse(String str) {
return new StringBuilder(str).reverse().toString();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fnproject.examples;
package com.example.fn.testing;

import com.fnproject.fn.examples.StringReverse;
import com.example.fn.StringReverse;
import org.junit.Test;

import static junit.framework.TestCase.assertEquals;
Expand Down

0 comments on commit fdac7f9

Please sign in to comment.