|
| 1 | +/* |
| 2 | + * Copyright 2024 The Dapr Authors |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package io.dapr.spring.boot.autoconfigure.client; |
| 15 | + |
| 16 | +import io.dapr.client.DaprClient; |
| 17 | +import io.dapr.client.DaprClientBuilder; |
| 18 | +import io.dapr.config.Properties; |
| 19 | +import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 20 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 21 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 22 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 23 | +import org.springframework.context.annotation.Bean; |
| 24 | + |
| 25 | +@AutoConfiguration |
| 26 | +@ConditionalOnClass(DaprClient.class) |
| 27 | +@EnableConfigurationProperties(DaprClientProperties.class) |
| 28 | +public class DaprClientAutoConfiguration { |
| 29 | + |
| 30 | + @Bean |
| 31 | + @ConditionalOnMissingBean(DaprConnectionDetails.class) |
| 32 | + DaprConnectionDetails daprConnectionDetails(DaprClientProperties properties) { |
| 33 | + return new PropertiesDaprConnectionDetails(properties); |
| 34 | + } |
| 35 | + |
| 36 | + @Bean |
| 37 | + @ConditionalOnMissingBean |
| 38 | + DaprClientBuilder daprClientBuilder(DaprConnectionDetails daprConnectionDetails) { |
| 39 | + DaprClientBuilder builder = new DaprClientBuilder(); |
| 40 | + if (daprConnectionDetails.httpEndpoint() != null) { |
| 41 | + builder.withPropertyOverride(Properties.HTTP_ENDPOINT, daprConnectionDetails.httpEndpoint()); |
| 42 | + } |
| 43 | + if (daprConnectionDetails.grpcEndpoint() != null) { |
| 44 | + builder.withPropertyOverride(Properties.GRPC_ENDPOINT, daprConnectionDetails.grpcEndpoint()); |
| 45 | + } |
| 46 | + if (daprConnectionDetails.httpPort() != null) { |
| 47 | + builder.withPropertyOverride(Properties.HTTP_PORT, String.valueOf(daprConnectionDetails.httpPort())); |
| 48 | + } |
| 49 | + if (daprConnectionDetails.grpcPort() != null) { |
| 50 | + builder.withPropertyOverride(Properties.GRPC_PORT, String.valueOf(daprConnectionDetails.grpcPort())); |
| 51 | + } |
| 52 | + return builder; |
| 53 | + } |
| 54 | + |
| 55 | + @Bean |
| 56 | + @ConditionalOnMissingBean |
| 57 | + DaprClient daprClient(DaprClientBuilder daprClientBuilder) { |
| 58 | + return daprClientBuilder.build(); |
| 59 | + } |
| 60 | + |
| 61 | +} |
0 commit comments