initial web support

This commit is contained in:
markfisher
2016-09-22 11:59:31 -04:00
parent cd9c311326
commit 59376bbb0e
7 changed files with 315 additions and 2 deletions

View File

@@ -16,7 +16,7 @@
<properties>
<java.version>1.8</java.version>
<reactor.version>3.0.0.RELEASE</reactor.version>
<reactor.version>3.0.2.RELEASE</reactor.version>
<spring-cloud-stream.version>1.1.0.BUILD-SNAPSHOT</spring-cloud-stream.version>
</properties>
@@ -39,7 +39,7 @@
<module>spring-cloud-function-compiler</module>
<module>spring-cloud-function-core</module>
<module>spring-cloud-function-stream</module>
<!-- module>spring-cloud-function-web</module -->
<module>spring-cloud-function-web</module>
</modules>
<profiles>

View File

View File

@@ -0,0 +1,55 @@
<?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>
<artifactId>spring-cloud-function-web</artifactId>
<packaging>jar</packaging>
<name>Spring Cloud Function Web Support</name>
<description>Spring Cloud Function Web Support</description>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-parent</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web-reactive</artifactId>
<version>5.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.projectreactor.ipc</groupId>
<artifactId>reactor-netty</artifactId>
<version>0.5.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2016 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
*
* 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.web;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author Mark Fisher
*/
@ConfigurationProperties(prefix = "function")
public class FunctionConfigurationProperties {
private String name;
private String code;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2016 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
*
* 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.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author Mark Fisher
*/
@SpringBootApplication
public class RestApplication {
public static void main(String[] args) {
SpringApplication.run(RestApplication.class, args);
}
}

View File

@@ -0,0 +1,135 @@
/*
* Copyright 2016 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
*
* 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.web;
import static org.springframework.http.MediaType.TEXT_PLAIN;
import static org.springframework.web.reactive.function.BodyExtractors.toFlux;
import static org.springframework.web.reactive.function.BodyInserters.fromPublisher;
import static org.springframework.web.reactive.function.RequestPredicates.contentType;
import static org.springframework.web.reactive.function.RequestPredicates.POST;
import java.util.concurrent.Executors;
import java.util.function.Function;
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.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.function.registry.FunctionRegistry;
import org.springframework.cloud.function.registry.InMemoryFunctionRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.function.Request;
import org.springframework.web.reactive.function.Response;
import org.springframework.web.reactive.function.RouterFunction;
import org.springframework.web.reactive.function.RouterFunctions;
import reactor.core.publisher.Flux;
import reactor.ipc.netty.http.HttpServer;
/**
* @author Mark Fisher
*/
@Configuration
@EnableConfigurationProperties({ FunctionConfigurationProperties.class, WebConfigurationProperties.class })
public class RestConfiguration {
@Autowired
private FunctionConfigurationProperties functionProperties;
@Autowired
private WebConfigurationProperties webProperties;
@Bean
public FunctionRegistry registry() {
FunctionRegistry registry = new InMemoryFunctionRegistry();
registry.register(functionProperties.getName(), functionProperties.getCode());
return registry;
}
@Bean
public HttpHandler httpHandler(FunctionRegistry registry) {
Function<Flux<String>, Flux<String>> function = registry.lookup(functionProperties.getName());
FunctionInvokingHandler handler = new FunctionInvokingHandler(function);
RouterFunction<Publisher<String>> route = RouterFunctions.route(
POST(webProperties.getPath()).and(contentType(TEXT_PLAIN)), handler::handleText);
return RouterFunctions.toHttpHandler(route);
}
@Bean
public LifecycleAwareHttpServer httpServer(HttpHandler handler) {
return new LifecycleAwareHttpServer(handler, webProperties.getPort());
}
private static class FunctionInvokingHandler {
private final Function<Flux<String>, Flux<String>> function;
private FunctionInvokingHandler(Function<Flux<String>, Flux<String>> function) {
this.function = function;
}
private Response<Publisher<String>> handleText(Request request) {
Flux<String> input = request.body(toFlux(String.class));
Publisher<String> output = this.function.apply(input);
return Response.ok().body(fromPublisher(output, String.class));
}
}
private static class LifecycleAwareHttpServer implements InitializingBean, DisposableBean {
private final HttpHandler handler;
private final int port;
private volatile HttpServer server;
private LifecycleAwareHttpServer(HttpHandler handler, int port) {
this.handler = handler;
this.port = port;
}
@Override
public void afterPropertiesSet() {
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(this.handler);
final HttpServer server = HttpServer.create("localhost", port);
this.server = server;
Executors.newSingleThreadExecutor().submit(new Runnable() {
@Override
public void run() {
try {
server.startAndAwait(adapter);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
}
@Override
public void destroy() {
if (this.server != null) {
this.server.shutdown();
}
}
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2016 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
*
* 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.web;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author Mark Fisher
*/
@ConfigurationProperties(prefix = "web")
public class WebConfigurationProperties {
private int port = 8080;
private String path;
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}