暂存提交
This commit is contained in:
@@ -13,11 +13,21 @@
|
||||
<name>integration-service</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baiyue.shared</groupId>
|
||||
<artifactId>baiyue-common-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baiyue.shared</groupId>
|
||||
<artifactId>baiyue-common-web</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baiyue.shared</groupId>
|
||||
<artifactId>baiyue-common-openapi</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@SpringBootApplication(scanBasePackages = {"com.baiyue.integration", "com.baiyue.common.web"})
|
||||
@ConfigurationPropertiesScan
|
||||
public class IntegrationServiceApplication {
|
||||
public static void main(String[] args) {
|
||||
|
||||
+58
-2
@@ -1,12 +1,68 @@
|
||||
package com.baiyue.integration.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
|
||||
@ConfigurationProperties(prefix = "baiyue.integration")
|
||||
public record IntegrationProperties(BaiduOcr baiduOcr, AmazonSpApi amazonSpApi) {
|
||||
public record BaiduOcr(boolean enabled, String apiKey, String secretKey) {
|
||||
public record BaiduOcr(boolean enabled,
|
||||
String apiKey,
|
||||
String secretKey,
|
||||
URI tokenUrl,
|
||||
URI meterUrl,
|
||||
Duration connectTimeout,
|
||||
Duration requestTimeout,
|
||||
Path inputRoot,
|
||||
Path outputRoot,
|
||||
DataSize maxImageSize,
|
||||
long maxImagePixels,
|
||||
int maxBatchSize,
|
||||
Duration minRequestInterval,
|
||||
int maxQpsRetries) {
|
||||
public boolean configured() {
|
||||
return enabled && hasText(apiKey) && hasText(secretKey);
|
||||
return enabled
|
||||
&& hasText(apiKey)
|
||||
&& hasText(secretKey)
|
||||
&& tokenUrl != null
|
||||
&& meterUrl != null;
|
||||
}
|
||||
|
||||
public Duration effectiveConnectTimeout() {
|
||||
return positive(connectTimeout) ? connectTimeout : Duration.ofSeconds(10);
|
||||
}
|
||||
|
||||
public Duration effectiveRequestTimeout() {
|
||||
return positive(requestTimeout) ? requestTimeout : Duration.ofSeconds(60);
|
||||
}
|
||||
|
||||
public DataSize effectiveMaxImageSize() {
|
||||
return maxImageSize != null && maxImageSize.toBytes() > 0
|
||||
? maxImageSize : DataSize.ofMegabytes(3);
|
||||
}
|
||||
|
||||
public long effectiveMaxImagePixels() {
|
||||
return maxImagePixels > 0 ? maxImagePixels : 40_000_000L;
|
||||
}
|
||||
|
||||
public int effectiveMaxBatchSize() {
|
||||
return maxBatchSize > 0 ? maxBatchSize : 50;
|
||||
}
|
||||
|
||||
public Duration effectiveMinRequestInterval() {
|
||||
return minRequestInterval != null && !minRequestInterval.isNegative()
|
||||
? minRequestInterval : Duration.ofMillis(600);
|
||||
}
|
||||
|
||||
public int effectiveMaxQpsRetries() {
|
||||
return Math.min(10, Math.max(0, maxQpsRetries));
|
||||
}
|
||||
|
||||
private boolean positive(Duration value) {
|
||||
return value != null && !value.isNegative() && !value.isZero();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -1,9 +1,9 @@
|
||||
package com.baiyue.integration.security;
|
||||
|
||||
import com.baiyue.common.core.api.ResultCode;
|
||||
import com.baiyue.common.core.exception.BizException;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
@@ -22,7 +22,7 @@ public class InternalServiceTokenGuard {
|
||||
public void verify(String actualToken) {
|
||||
byte[] actual = actualToken == null ? new byte[0] : actualToken.getBytes(StandardCharsets.UTF_8);
|
||||
if (!MessageDigest.isEqual(expectedToken, actual)) {
|
||||
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "内部服务令牌无效");
|
||||
throw new BizException(ResultCode.UNAUTHORIZED, "内部服务令牌无效");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,17 @@ baiyue:
|
||||
enabled: ${BAIDU_OCR_ENABLED:false}
|
||||
api-key: ${BAIDU_OCR_API_KEY:}
|
||||
secret-key: ${BAIDU_OCR_SECRET_KEY:}
|
||||
token-url: ${BAIDU_OCR_TOKEN_URL:https://aip.baidubce.com/oauth/2.0/token}
|
||||
meter-url: ${BAIDU_OCR_METER_URL:https://aip.baidubce.com/rest/2.0/ocr/v1/meter}
|
||||
connect-timeout: ${BAIDU_OCR_CONNECT_TIMEOUT:10s}
|
||||
request-timeout: ${BAIDU_OCR_REQUEST_TIMEOUT:60s}
|
||||
input-root: ${WEIGHT_OCR_INPUT_ROOT:${java.io.tmpdir}/baiyue-weight-ocr/input}
|
||||
output-root: ${WEIGHT_OCR_OUTPUT_ROOT:${java.io.tmpdir}/baiyue-weight-ocr/output}
|
||||
max-image-size: ${WEIGHT_OCR_MAX_IMAGE_SIZE:3MB}
|
||||
max-image-pixels: ${WEIGHT_OCR_MAX_IMAGE_PIXELS:40000000}
|
||||
max-batch-size: ${WEIGHT_OCR_MAX_BATCH_SIZE:50}
|
||||
min-request-interval: ${BAIDU_OCR_MIN_REQUEST_INTERVAL:600ms}
|
||||
max-qps-retries: ${BAIDU_OCR_MAX_QPS_RETRIES:3}
|
||||
amazon-sp-api:
|
||||
enabled: ${AMAZON_SP_API_ENABLED:false}
|
||||
client-id: ${AMAZON_SP_API_CLIENT_ID:}
|
||||
|
||||
+16
-2
@@ -2,19 +2,33 @@ package com.baiyue.integration.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class IntegrationPropertiesTest {
|
||||
@Test
|
||||
void providerIsConfiguredOnlyWhenEnabledAndAllSecretsExist() {
|
||||
var baidu = new IntegrationProperties.BaiduOcr(true, "key", "secret");
|
||||
var baidu = baidu(true, "key", "secret");
|
||||
var amazon = new IntegrationProperties.AmazonSpApi(
|
||||
true, "client", "secret", "refresh", "access", "aws-secret", "us-east-1");
|
||||
|
||||
assertThat(baidu.configured()).isTrue();
|
||||
assertThat(amazon.configured()).isTrue();
|
||||
assertThat(new IntegrationProperties.BaiduOcr(false, "key", "secret").configured()).isFalse();
|
||||
assertThat(baidu(false, "key", "secret").configured()).isFalse();
|
||||
assertThat(new IntegrationProperties.AmazonSpApi(
|
||||
true, "client", "secret", "", "access", "aws-secret", "us-east-1").configured()).isFalse();
|
||||
}
|
||||
|
||||
private IntegrationProperties.BaiduOcr baidu(boolean enabled, String apiKey, String secretKey) {
|
||||
return new IntegrationProperties.BaiduOcr(
|
||||
enabled, apiKey, secretKey,
|
||||
URI.create("https://example.com/token"), URI.create("https://example.com/meter"),
|
||||
Duration.ofSeconds(10), Duration.ofSeconds(60),
|
||||
Path.of("input"), Path.of("output"), null,
|
||||
40_000_000L, 50, Duration.ofMillis(600), 3
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user