|
| 1 | +/* |
| 2 | + * Copyright 2021-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.function.grpc; |
| 18 | + |
| 19 | +import java.util.function.Function; |
| 20 | + |
| 21 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 22 | +import org.springframework.cloud.function.context.FunctionProperties; |
| 23 | +import org.springframework.cloud.function.context.MessageRoutingCallback; |
| 24 | +import org.springframework.context.annotation.Bean; |
| 25 | +import org.springframework.context.annotation.Configuration; |
| 26 | +import org.springframework.messaging.Message; |
| 27 | + |
| 28 | +/** |
| 29 | + * |
| 30 | + * @author Oleg Zhurakousky |
| 31 | + * @since 3.2 |
| 32 | + */ |
| 33 | +@Configuration(proxyBeanMethods = false) |
| 34 | +@EnableConfigurationProperties(FunctionGrpcProperties.class) |
| 35 | +public class GrpcFunctionAutoConfiguration { |
| 36 | + |
| 37 | + public static String GRPC_INVOKER_FUNCTION = "grpcInvokerFunction"; |
| 38 | + |
| 39 | + public static String GRPC = "grpc"; |
| 40 | + |
| 41 | + @Bean |
| 42 | + public Function<Message<byte[]>, Message<?>> grpcInvokerFunction() { |
| 43 | + return input -> { |
| 44 | + return GrpcUtils.requestReply(input); |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + @Bean |
| 49 | + public MessageRoutingCallback routingCallback() { |
| 50 | + return new MessageRoutingCallback() { |
| 51 | + public String routingResult(Message<?> message) { |
| 52 | + if (message.getHeaders().containsKey(FunctionProperties.PROXY) |
| 53 | + && message.getHeaders().get(FunctionProperties.PROXY).equals(GRPC)) { |
| 54 | + return GRPC_INVOKER_FUNCTION; |
| 55 | + } |
| 56 | + return null; |
| 57 | + } |
| 58 | + }; |
| 59 | + } |
| 60 | +} |
0 commit comments