Add a sample app with just beans that are Functions

Make it deployable via its maven coordinates in
spring-cloud-function-deployer (it is deployed by default on start
up right now, but that's just a demo)
This commit is contained in:
Dave Syer
2016-12-16 11:17:23 +00:00
parent 7408664aeb
commit c6736f959b
39 changed files with 1404 additions and 84 deletions

View File

@@ -58,6 +58,13 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-launcher</artifactId>
<version>${wrapper.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

View File

@@ -17,15 +17,16 @@
package org.springframework.cloud.function.web;
import java.util.function.Function;
import java.util.function.Supplier;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.function.registry.FileSystemFunctionRegistry;
import org.springframework.cloud.function.registry.FunctionRegistry;
import org.springframework.cloud.function.registry.FunctionCatalog;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
@@ -50,33 +51,22 @@ import reactor.ipc.netty.http.server.HttpServer;
* @author Mark Fisher
*/
@Configuration
@EnableConfigurationProperties({ FunctionConfigurationProperties.class,
WebConfigurationProperties.class })
@EnableConfigurationProperties({ WebConfigurationProperties.class })
@ConditionalOnClass({ HttpHandler.class, NettyContext.class })
public class RestConfiguration {
@Autowired
private FunctionConfigurationProperties functionProperties;
@Autowired
private WebConfigurationProperties webProperties;
@Bean
public FunctionRegistry registry() {
return new FileSystemFunctionRegistry();
}
@Bean
public HttpHandler httpHandler(FunctionRegistry registry) {
String name = functionProperties.getName();
Function<Flux<String>, Flux<String>> function = (name.indexOf(',') == -1)
? registry.lookupFunction(name)
: registry.composeFunction(
StringUtils.commaDelimitedListToStringArray(name));
FunctionInvokingHandler handler = new FunctionInvokingHandler(function);
RouterFunction<ServerResponse> route = RouterFunctions.route(
RequestPredicates.POST(webProperties.getPath())
public HttpHandler httpHandler(FunctionCatalog registry) {
FunctionInvokingHandler handler = new FunctionInvokingHandler(registry);
RouterFunction<?> route = RouterFunctions
.route(RequestPredicates.POST(webProperties.getPath() + "/{name}")
.and(RequestPredicates.contentType(MediaType.TEXT_PLAIN)),
handler::handleText);
handler::handlePost)
.andRoute(RequestPredicates.GET(webProperties.getPath() + "/{name}"),
handler::handleGet);
return RouterFunctions.toHttpHandler(route);
}
@@ -87,15 +77,27 @@ public class RestConfiguration {
private static class FunctionInvokingHandler {
private final Function<Flux<String>, Flux<String>> function;
private final FunctionCatalog registry;
private FunctionInvokingHandler(Function<Flux<String>, Flux<String>> function) {
this.function = function;
private FunctionInvokingHandler(FunctionCatalog registry) {
this.registry = registry;
}
private Mono<ServerResponse> handleText(ServerRequest request) {
private Mono<ServerResponse> handlePost(ServerRequest request) {
Flux<String> input = request.body(toFlux(String.class));
Publisher<String> output = this.function.apply(input);
String name = request.pathVariable("name");
Function<Flux<String>, Flux<String>> function = (name.indexOf(',') == -1)
? registry.lookupFunction(name)
: registry.composeFunction(
StringUtils.commaDelimitedListToStringArray(name));
Publisher<String> output = function.apply(input);
return ServerResponse.ok().body(fromPublisher(output, String.class));
}
private Mono<ServerResponse> handleGet(ServerRequest request) {
String name = request.pathVariable("name");
Supplier<Flux<String>> function = registry.lookupSupplier(name);
Publisher<String> output = function.get();
return ServerResponse.ok().body(fromPublisher(output, String.class));
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2017 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
*
* http://www.apache.org/licenses/LICENSE-2.0
* 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,
@@ -13,20 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.web;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author Mark Fisher
*/
@ConfigurationProperties(prefix = "web")
@ConfigurationProperties("server")
public class WebConfigurationProperties {
private int port = 8080;
private String path;
private String path = "";
public int getPort() {
return port;
@@ -41,6 +37,9 @@ public class WebConfigurationProperties {
}
public void setPath(String path) {
while (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
this.path = path;
}
}

View File

@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.function.web.RestConfiguration