@@ -36,7 +36,7 @@ Start by adding the Maven plugin provided as part of the Google Functions Framew
|
||||
<artifactId>function-maven-plugin</artifactId>
|
||||
<version>0.9.1</version>
|
||||
<configuration>
|
||||
<functionTarget>org.springframework.cloud.function.adapter.gcloud.GcfSpringBootHttpRequestHandler</functionTarget>
|
||||
<functionTarget>org.springframework.cloud.function.adapter.gcloud.FunctionInvoker</functionTarget>
|
||||
<port>8080</port>
|
||||
</configuration>
|
||||
</plugin>
|
||||
@@ -118,7 +118,7 @@ From the project base directory run the following command to deploy.
|
||||
|
||||
----
|
||||
gcloud alpha functions deploy function-sample-gcp \
|
||||
--entry-point org.springframework.cloud.function.adapter.gcloud.GcfSpringBootHttpRequestHandler \
|
||||
--entry-point org.springframework.cloud.function.adapter.gcloud.FunctionInvoker \
|
||||
--runtime java11 \
|
||||
--trigger-http \
|
||||
--source deploy \
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<google.cloud.functions.version>1.0.0-alpha-2-rc3</google.cloud.functions.version>
|
||||
<gson.version>2.8.5</gson.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -31,17 +30,11 @@
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-function-context</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-function-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Test-only dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or 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
|
||||
*
|
||||
* 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 org.springframework.cloud.function.adapter.gcloud;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.google.cloud.functions.HttpFunction;
|
||||
import com.google.cloud.functions.HttpRequest;
|
||||
import com.google.cloud.functions.HttpResponse;
|
||||
|
||||
import org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
/**
|
||||
* Implementation of {@link HttpFunction} for Google Cloud Function (GCF).
|
||||
* This is the Spring Cloud Function adapter for GCF HTTP function.
|
||||
*
|
||||
* @author Dmitry Solomakha
|
||||
* @author Mike Eltsufin
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
* @since 3.0.4
|
||||
*/
|
||||
public class FunctionInvoker
|
||||
extends AbstractSpringFunctionAdapterInitializer<HttpRequest> implements HttpFunction {
|
||||
|
||||
public FunctionInvoker() {
|
||||
super();
|
||||
}
|
||||
|
||||
public FunctionInvoker(Class<?> configurationClass) {
|
||||
super(configurationClass);
|
||||
System.setProperty("spring.http.converters.preferred-json-mapper", "gson");
|
||||
Thread.currentThread() //TODO investigate if it is necessary
|
||||
.setContextClassLoader(FunctionInvoker.class.getClassLoader());
|
||||
initialize(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* The implementation of a GCF {@link HttpFunction} that will be used as the entry point from GCF.
|
||||
*/
|
||||
@Override
|
||||
public void service(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception {
|
||||
try {
|
||||
String functionName = System.getenv().containsKey("spring.cloud.function.definition")
|
||||
? System.getenv("spring.cloud.function.definition") : "";
|
||||
|
||||
Function<Message<BufferedReader>, Message<byte[]>> function =
|
||||
this.catalog.lookup(functionName, MimeTypeUtils.APPLICATION_JSON.toString());
|
||||
Assert.notNull(function, "'function' with name '" + functionName + "' must not be null");
|
||||
|
||||
Message<BufferedReader> message = getInputType() == Void.class
|
||||
? null : MessageBuilder.withPayload(httpRequest.getReader())
|
||||
.copyHeaders(httpRequest.getHeaders())
|
||||
.build();
|
||||
Message<byte[]> result = function.apply(message);
|
||||
|
||||
if (result != null) {
|
||||
httpResponse.getWriter().write(new String(result.getPayload(), StandardCharsets.UTF_8));
|
||||
for (Entry<String, Object> header : result.getHeaders().entrySet()) {
|
||||
httpResponse.appendHeader(header.getKey(), header.getValue().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
httpResponse.getWriter().close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or 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
|
||||
*
|
||||
* 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 org.springframework.cloud.function.adapter.gcloud;
|
||||
|
||||
import com.google.cloud.functions.HttpFunction;
|
||||
import com.google.cloud.functions.HttpRequest;
|
||||
import com.google.cloud.functions.HttpResponse;
|
||||
import com.google.gson.Gson;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer;
|
||||
|
||||
/**
|
||||
* Implementation of {@link HttpFunction} for Google Cloud Function (GCF).
|
||||
* This is the Spring Cloud Function adapter for GCF HTTP function.
|
||||
*
|
||||
* @author Dmitry Solomakha
|
||||
* @author Mike Eltsufin
|
||||
*/
|
||||
public class GcfSpringBootHttpRequestHandler
|
||||
extends AbstractSpringFunctionAdapterInitializer<HttpRequest> implements HttpFunction {
|
||||
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
public GcfSpringBootHttpRequestHandler() {
|
||||
super();
|
||||
}
|
||||
|
||||
public GcfSpringBootHttpRequestHandler(Class<?> configurationClass) {
|
||||
super(configurationClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* The implementation of a GCF {@link HttpFunction} that will be used as the entrypoint from GCF.
|
||||
*/
|
||||
@Override
|
||||
public void service(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception {
|
||||
Thread.currentThread()
|
||||
.setContextClassLoader(GcfSpringBootHttpRequestHandler.class.getClassLoader());
|
||||
|
||||
initialize(httpRequest);
|
||||
|
||||
Publisher<?> input;
|
||||
if (getInputType() == Void.class) {
|
||||
input = Mono.empty();
|
||||
}
|
||||
else {
|
||||
input = Mono.just(gson.fromJson(httpRequest.getReader(), getInputType()));
|
||||
}
|
||||
|
||||
Publisher<?> output = this.apply(input);
|
||||
|
||||
Object result = this.result(input, output);
|
||||
|
||||
httpResponse.getWriter().write(gson.toJson(result));
|
||||
httpResponse.getWriter().close();
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,8 @@ import org.springframework.cloud.function.context.config.ContextFunctionCatalogA
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -44,7 +46,7 @@ import static org.mockito.Mockito.when;
|
||||
* @author Dmitry Solomakha
|
||||
* @author Mike Eltsufin
|
||||
*/
|
||||
public class GcfSpringBootHttpRequestHandlerTests {
|
||||
public class FunctionInvokerTests {
|
||||
|
||||
private static final Gson gson = new Gson();
|
||||
|
||||
@@ -71,21 +73,23 @@ public class GcfSpringBootHttpRequestHandlerTests {
|
||||
}
|
||||
|
||||
private <I, O> void testFunction(Class<?> configurationClass, I input, O expectedOutput) throws Exception {
|
||||
GcfSpringBootHttpRequestHandler handler = new GcfSpringBootHttpRequestHandler(configurationClass);
|
||||
try (FunctionInvoker handler = new FunctionInvoker(configurationClass);) {
|
||||
|
||||
HttpRequest request = Mockito.mock(HttpRequest.class);
|
||||
HttpRequest request = Mockito.mock(HttpRequest.class);
|
||||
|
||||
if (input != null) {
|
||||
when(request.getReader()).thenReturn(new BufferedReader(new StringReader(gson.toJson(input))));
|
||||
if (input != null) {
|
||||
when(request.getReader()).thenReturn(new BufferedReader(new StringReader(gson.toJson(input))));
|
||||
}
|
||||
|
||||
HttpResponse response = Mockito.mock(HttpResponse.class);
|
||||
StringWriter writer = new StringWriter();
|
||||
when(response.getWriter()).thenReturn(new BufferedWriter(writer));
|
||||
|
||||
handler.service(request, response);
|
||||
if (expectedOutput != null) {
|
||||
assertThat(writer.toString()).isEqualTo(gson.toJson(expectedOutput));
|
||||
}
|
||||
}
|
||||
|
||||
HttpResponse response = Mockito.mock(HttpResponse.class);
|
||||
StringWriter writer = new StringWriter();
|
||||
when(response.getWriter()).thenReturn(new BufferedWriter(writer));
|
||||
|
||||
handler.service(request, response);
|
||||
|
||||
assertThat(writer.toString()).isEqualTo(gson.toJson(expectedOutput));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@@ -110,8 +114,11 @@ public class GcfSpringBootHttpRequestHandlerTests {
|
||||
@Import({ ContextFunctionCatalogAutoConfiguration.class })
|
||||
protected static class JsonInputOutputFunction {
|
||||
@Bean
|
||||
public Function<IncomingRequest, OutgoingResponse> function() {
|
||||
return (in) -> new OutgoingResponse("Thank you for sending the message: " + in.message);
|
||||
public Function<IncomingRequest, Message<OutgoingResponse>> function() {
|
||||
return (in) -> {
|
||||
return MessageBuilder.withPayload(new OutgoingResponse("Thank you for sending the message: " + in.message))
|
||||
.setHeader("foo", "bar").build();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,6 @@ import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
||||
@@ -79,7 +78,7 @@ public abstract class AbstractSpringFunctionAdapterInitializer<C> implements Clo
|
||||
private FunctionInspector inspector;
|
||||
|
||||
@Autowired(required = false)
|
||||
private FunctionCatalog catalog;
|
||||
protected FunctionCatalog catalog;
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
@@ -266,7 +265,7 @@ public abstract class AbstractSpringFunctionAdapterInitializer<C> implements Clo
|
||||
new FunctionRegistration(context.getBean(name), name);
|
||||
|
||||
Type type = FunctionContextUtils.
|
||||
findType(name, (ConfigurableListableBeanFactory) this.context.getBeanFactory());
|
||||
findType(name, this.context.getBeanFactory());
|
||||
|
||||
this.functionRegistration = functionRegistration.type(new FunctionType(type)).wrap();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user