Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
32cbc5a273 |
38
pom.xml
Normal file
38
pom.xml
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
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>
|
||||||
|
|
||||||
|
<groupId>dev.openfunction.samples</groupId>
|
||||||
|
<artifactId>samples</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>snapshots</id>
|
||||||
|
<name>Maven snapshots</name>
|
||||||
|
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
|
||||||
|
<releases>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</releases>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>dev.openfunction.functions</groupId>
|
||||||
|
<artifactId>functions-framework-api</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2022 The OpenFunction Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dev.openfunction.samples;
|
||||||
|
|
||||||
|
import dev.openfunction.functions.CloudEventFunction;
|
||||||
|
import dev.openfunction.functions.Context;
|
||||||
|
import io.cloudevents.CloudEvent;
|
||||||
|
|
||||||
|
public class CloudEventFunctionImpl implements CloudEventFunction {
|
||||||
|
@Override
|
||||||
|
public Error accept(Context ctx, CloudEvent event) throws Exception {
|
||||||
|
if (event.getData() == null) {
|
||||||
|
System.out.println("receive event without data");
|
||||||
|
} else {
|
||||||
|
System.out.println("receive event: " + new String(event.getData().toBytes()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
27
src/main/java/dev/openfunction/samples/Main.java
Normal file
27
src/main/java/dev/openfunction/samples/Main.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// Copyright 2019 Google LLC
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
package dev.openfunction.samples;
|
||||||
|
|
||||||
|
import dev.openfunction.functions.HttpFunction;
|
||||||
|
import dev.openfunction.functions.HttpRequest;
|
||||||
|
import dev.openfunction.functions.HttpResponse;
|
||||||
|
|
||||||
|
public class Main implements HttpFunction {
|
||||||
|
// Simple function to return "Hello World"
|
||||||
|
@Override
|
||||||
|
public void service(HttpRequest request, HttpResponse response) throws Exception {
|
||||||
|
|
||||||
|
response.getWriter().write("Hello World");
|
||||||
|
}
|
||||||
|
}
|
||||||
41
src/main/java/dev/openfunction/samples/OpenFunctionImpl.java
Normal file
41
src/main/java/dev/openfunction/samples/OpenFunctionImpl.java
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2022 The OpenFunction Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dev.openfunction.samples;
|
||||||
|
|
||||||
|
import dev.openfunction.functions.OpenFunction;
|
||||||
|
import dev.openfunction.functions.Context;
|
||||||
|
import dev.openfunction.functions.Out;
|
||||||
|
|
||||||
|
public class OpenFunctionImpl implements OpenFunction {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Out accept(Context context, String payload) throws Exception {
|
||||||
|
System.out.printf("receive event: %s", payload).println();
|
||||||
|
|
||||||
|
if (context.getOutputs() != null) {
|
||||||
|
for (String key : context.getOutputs().keySet()) {
|
||||||
|
Error error = context.send(key, payload);
|
||||||
|
if (error != null) {
|
||||||
|
System.out.printf("send to output %s error, %s", key, error.getMessage()).println();
|
||||||
|
} else {
|
||||||
|
System.out.printf("send to output %s", key).println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Out();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2022 The OpenFunction Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dev.openfunction.samples.plugins;
|
||||||
|
|
||||||
|
import dev.openfunction.functions.Context;
|
||||||
|
import dev.openfunction.functions.Plugin;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class ExamplePlugin implements Plugin {
|
||||||
|
@Override
|
||||||
|
public String name() {
|
||||||
|
return "plugin-example";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String version() {
|
||||||
|
return "v1.0.0";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Plugin init() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Error execPreHook(Context ctx) {
|
||||||
|
execHook(ctx, "pre");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Error execPostHook(Context ctx) {
|
||||||
|
execHook(ctx, "post");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void execHook(Context ctx, String type) {
|
||||||
|
String ts = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.XXX").format(new Date());
|
||||||
|
if (ctx.getBindingEvent() != null) {
|
||||||
|
System.out.printf("plugin %s:%s exec %s hook for binding %s at %s", name(), version(), type, ctx.getBindingEvent().getName(), ts).println();
|
||||||
|
} else if (ctx.getTopicEvent() != null) {
|
||||||
|
System.out.printf("plugin %s:%s exec %s hook for pubsub %s at %s", name(), version(), type, ctx.getTopicEvent().getName(), ts).println();
|
||||||
|
} else if (ctx.getHttpRequest() != null) {
|
||||||
|
if (ctx.getCloudEvent() != null) {
|
||||||
|
System.out.printf("plugin %s:%s exec %s hook for cloudevent function at %s", name(), version(), type, ts).println();
|
||||||
|
} else {
|
||||||
|
System.out.printf("plugin %s:%s exec %s hook for http function at %s", name(), version(), type, ts).println();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.out.println("unknown function type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getField(String fieldName) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user