Automatic push from FunctionsAPI

This commit is contained in:
FunctionsAPI 2025-01-19 13:51:30 +00:00
parent 0d56b35bfd
commit 8b7b0948ec
6 changed files with 215 additions and 2 deletions

View File

@ -1,2 +1 @@
# samples # java hello-world

38
pom.xml Normal file
View 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>

View File

@ -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;
}
}

View 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");
}
}

View 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();
}
}

View File

@ -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;
}
}