Skip to content

Commit 026ba30

Browse files
authored
Merge pull request #1 from HonestMasker/feature/3.0.0
Feature/3.0.0
2 parents fe5ac4f + f298cf8 commit 026ba30

16 files changed

+157
-102
lines changed

.github/workflows/pipeline.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
uses: actions/setup-java@v3
2020
with:
2121
distribution: temurin
22-
java-version: 8
22+
java-version: 17
2323
cache: 'maven'
2424

2525
- name: Cache Maven Dependency

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
FROM openjdk:8-jre-alpine
1+
FROM amazoncorretto:17.0.12-alpine3.20
22
ADD ./target/huaying-app.jar /app.jar
33
CMD ["--server.port=8080"]
4-
ENV spring.profiles.active="pro"
4+
ENV spring.profiles.active="prod"
55
RUN apk add --no-cache tzdata && \
66
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
77
echo "Asia/Shanghai" > /etc/timezone && \

pom.xml

+34-6
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>2.7.13</version>
9+
<version>3.3.0</version>
1010
<relativePath/> <!-- lookup parent from repository -->
1111
</parent>
1212
<groupId>io.github.llnancy</groupId>
1313
<artifactId>huaying</artifactId>
14-
<version>1.0.1</version>
14+
<version>3.0.0</version>
1515

1616
<description>一个基于 Spring Boot 构建的随机资源 API 服务</description>
1717

1818
<properties>
19-
<java.version>8</java.version>
19+
<java.version>17</java.version>
2020
<project.encoding>UTF-8</project.encoding>
2121
<maven.compiler.source>${java.version}</maven.compiler.source>
2222
<maven.compiler.target>${java.version}</maven.compiler.target>
@@ -29,14 +29,25 @@
2929
<dependency>
3030
<groupId>io.github.llnancy</groupId>
3131
<artifactId>mojian-bom</artifactId>
32-
<version>0.0.2</version>
32+
<version>3.0.0-SNAPSHOT</version>
3333
<type>pom</type>
3434
<!-- 注意这里是import -->
3535
<scope>import</scope>
3636
</dependency>
3737
</dependencies>
3838
</dependencyManagement>
3939

40+
<repositories>
41+
<repository>
42+
<id>ossrh-snapshot</id>
43+
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
44+
<snapshots>
45+
<updatePolicy>always</updatePolicy>
46+
<enabled>true</enabled>
47+
</snapshots>
48+
</repository>
49+
</repositories>
50+
4051
<dependencies>
4152
<dependency>
4253
<groupId>org.springframework.boot</groupId>
@@ -61,17 +72,34 @@
6172
</dependency>
6273
<dependency>
6374
<groupId>io.github.llnancy</groupId>
64-
<artifactId>mojian-web</artifactId>
75+
<artifactId>mojian-web-advice</artifactId>
76+
</dependency>
77+
<dependency>
78+
<groupId>cn.hutool</groupId>
79+
<artifactId>hutool-core</artifactId>
80+
<version>5.8.32</version>
6581
</dependency>
6682
</dependencies>
6783

6884
<build>
6985
<finalName>huaying-app</finalName>
7086
<plugins>
87+
<plugin>
88+
<groupId>org.graalvm.buildtools</groupId>
89+
<artifactId>native-maven-plugin</artifactId>
90+
</plugin>
7191
<plugin>
7292
<groupId>org.springframework.boot</groupId>
7393
<artifactId>spring-boot-maven-plugin</artifactId>
94+
<configuration>
95+
<excludes>
96+
<exclude>
97+
<groupId>org.projectlombok</groupId>
98+
<artifactId>lombok</artifactId>
99+
</exclude>
100+
</excludes>
101+
</configuration>
74102
</plugin>
75103
</plugins>
76104
</build>
77-
</project>
105+
</project>

src/main/java/io/github/llnancy/huaying/HuaYingApplication.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.llnancy.huaying;
22

3+
import io.github.llnancy.mojian.web.advice.annotation.EnableGlobalExceptionHandler;
34
import org.springframework.boot.WebApplicationType;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
56
import org.springframework.boot.builder.SpringApplicationBuilder;
@@ -11,6 +12,7 @@
1112
* @since JDK8 2022/4/1
1213
*/
1314
@SpringBootApplication
15+
@EnableGlobalExceptionHandler
1416
public class HuaYingApplication {
1517

1618
public static void main(String[] args) {

src/main/java/io/github/llnancy/huaying/config/Constants.java

-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,4 @@ public interface Constants {
1919
String RESOURCES_NOT_EXISTED = "资源不存在~";
2020

2121
String JSON_VALUE = "json";
22-
23-
String REDIRECT_VALUE = "302";
24-
25-
String URL_VALUE = "url";
2622
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.github.llnancy.huaying.config;
2+
3+
import lombok.SneakyThrows;
4+
import org.springframework.aot.hint.ExecutableMode;
5+
import org.springframework.aot.hint.RuntimeHints;
6+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.context.annotation.ImportRuntimeHints;
9+
10+
/**
11+
* aot native configuration
12+
*
13+
* @author llnancy admin@lilu.org.cn
14+
* @since JDK17 2023/7/10
15+
*/
16+
@Configuration(proxyBeanMethods = false)
17+
@ImportRuntimeHints(NativeConfiguration.HyRuntimeHintsRegistrar.class)
18+
public class NativeConfiguration {
19+
20+
static class HyRuntimeHintsRegistrar implements RuntimeHintsRegistrar {
21+
22+
@SneakyThrows
23+
@Override
24+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
25+
hints.reflection()
26+
.registerConstructor(org.hibernate.validator.internal.util.logging.Log_$logger.class.getConstructor(org.jboss.logging.Logger.class), ExecutableMode.INVOKE)
27+
.registerField(org.hibernate.validator.internal.util.logging.Messages_$bundle.class.getField("INSTANCE"));
28+
}
29+
}
30+
}

src/main/java/io/github/llnancy/huaying/config/WebMvcConfig.java renamed to src/main/java/io/github/llnancy/huaying/config/WebMvcConfiguration.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
package io.github.llnancy.huaying.config;
22

33
import io.github.llnancy.huaying.config.property.HyProperties;
4+
import io.github.llnancy.huaying.web.controller.HyController;
45
import io.github.llnancy.huaying.web.interceptor.DomainInterceptor;
56
import lombok.RequiredArgsConstructor;
67
import lombok.extern.slf4j.Slf4j;
78
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.core.convert.converter.Converter;
10+
import org.springframework.format.FormatterRegistry;
811
import org.springframework.http.HttpHeaders;
12+
import org.springframework.lang.NonNull;
913
import org.springframework.web.servlet.config.annotation.CorsRegistry;
1014
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
1115
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
1216

1317
/**
14-
* Web Mvc配置
18+
* Web Mvc 配置
1519
*
1620
* @author sunchaser admin@lilu.org.cn
1721
* @since JDK8 2022/4/2
1822
*/
1923
@Configuration
2024
@Slf4j
2125
@RequiredArgsConstructor
22-
public class WebMvcConfig implements WebMvcConfigurer {
26+
public class WebMvcConfiguration implements WebMvcConfigurer {
2327

2428
private final DomainInterceptor domainInterceptor;
2529

@@ -47,7 +51,18 @@ public void addCorsMappings(CorsRegistry registry) {
4751
.allowedMethods("*")
4852
.allowedHeaders("shushan", "huaying")
4953
.allowCredentials(Boolean.TRUE)
50-
.exposedHeaders(HttpHeaders.SET_COOKIE) // 允许带上cookie
51-
.maxAge(3600L); // maxAge(3600)表明在3600秒内,不需要再发送预检验请求,可以缓存该结果
54+
.exposedHeaders(HttpHeaders.SET_COOKIE) // 允许带上 cookie
55+
.maxAge(3600L); // maxAge(3600) 表明在 3600 秒内,不需要再发送预检验请求,可以缓存该结果
56+
}
57+
58+
@Override
59+
public void addFormatters(FormatterRegistry registry) {
60+
Converter<String, HyController.ResponseType> converter = new Converter<>() {
61+
@Override
62+
public HyController.ResponseType convert(@NonNull String source) {
63+
return HyController.ResponseType.valueOf(source.toUpperCase());
64+
}
65+
};
66+
registry.addConverter(converter);
5267
}
5368
}

src/main/java/io/github/llnancy/huaying/config/property/HyProperties.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
public class HyProperties {
2121

2222
/**
23-
* 是否允许header中无referer的请求进行访问
23+
* 是否允许 header 中无 referer 的请求进行访问
2424
*/
2525
private Boolean noReferer;
2626

2727
/**
28-
* API基础路径
28+
* API 基础路径
2929
*/
3030
private String apiBasePath = StringUtils.EMPTY;
3131

src/main/java/io/github/llnancy/huaying/service/RandomService.java

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package io.github.llnancy.huaying.service;
22

3+
import com.google.common.collect.Lists;
34
import io.github.llnancy.huaying.config.Constants;
4-
import io.github.llnancy.mojian.base.exception.MjBaseBizException;
5+
import io.github.llnancy.mojian.base.exception.MoJianBaseBizException;
56
import lombok.RequiredArgsConstructor;
67
import org.springframework.stereotype.Service;
78
import org.springframework.util.CollectionUtils;
89

910
import java.time.LocalDate;
1011
import java.time.temporal.ChronoUnit;
12+
import java.util.Collections;
1113
import java.util.List;
1214
import java.util.concurrent.ThreadLocalRandom;
1315
import java.util.function.Function;
@@ -22,24 +24,34 @@
2224
@RequiredArgsConstructor
2325
public class RandomService {
2426

25-
public String random(String category) {
26-
return doRandom(category, size -> ThreadLocalRandom.current().nextInt(size));
27+
public List<String> random(String category, Integer count) {
28+
return doRandom(category, count, size -> ThreadLocalRandom.current().nextInt(size));
2729
}
2830

29-
public String today(String category) {
30-
return doRandom(category, size -> Math.toIntExact(ChronoUnit.DAYS.between(LocalDate.parse(Constants.INIT_DATE), LocalDate.now()) % size));
31+
public List<String> today(String category, Integer count) {
32+
return doRandom(category, count, size -> Math.toIntExact(ChronoUnit.DAYS.between(LocalDate.parse(Constants.INIT_DATE), LocalDate.now()) % size));
3133
}
3234

33-
public String doRandom(String category, Function<Integer, Integer> function) {
35+
public List<String> doRandom(String category, Integer count, Function<Integer, Integer> function) {
3436
if (Constants.DOMAINS.equals(category) || ResourcesServer.isEmpty()) {
35-
throw new MjBaseBizException(Constants.RESOURCES_NOT_EXISTED);
37+
throw new MoJianBaseBizException(Constants.RESOURCES_NOT_EXISTED);
3638
}
3739
List<String> resources = ResourcesServer.get(category);
3840
if (CollectionUtils.isEmpty(resources)) {
39-
throw new MjBaseBizException(Constants.RESOURCES_NOT_EXISTED);
41+
throw new MoJianBaseBizException(Constants.RESOURCES_NOT_EXISTED);
4042
}
41-
int randomInt = function.apply(resources.size());
42-
return resources.get(randomInt);
43+
int size = resources.size();
44+
List<String> result;
45+
if (count == 1) {
46+
int randomInt = function.apply(size);
47+
result = Lists.newArrayList(resources.get(randomInt));
48+
} else if (count < size) {
49+
Collections.shuffle(resources);
50+
result = resources.subList(0, count);
51+
} else {
52+
result = resources;
53+
}
54+
return result;
4355
}
4456

4557
public void flush() {
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package io.github.llnancy.huaying.web.advice;
22

3-
import io.github.llnancy.huaying.config.property.HyProperties;
4-
import io.github.llnancy.huaying.config.property.HyProperties.WebSiteInfo;
5-
import io.github.llnancy.mojian.base.entity.response.SingleResponse;
6-
import io.github.llnancy.mojian.web.advice.MjDefaultGlobalExceptionHandler;
7-
import lombok.RequiredArgsConstructor;
3+
import io.github.llnancy.mojian.web.advice.DefaultGlobalExceptionHandler;
4+
import io.github.llnancy.mojian.web.advice.handler.ExceptionHandlerFactory;
85
import org.springframework.core.Ordered;
96
import org.springframework.core.annotation.Order;
107
import org.springframework.web.bind.annotation.RestControllerAdvice;
@@ -16,19 +13,9 @@
1613
* @since JDK8 2022/4/7
1714
*/
1815
@RestControllerAdvice
19-
@Order(Ordered.HIGHEST_PRECEDENCE)
20-
@RequiredArgsConstructor
21-
public class HyExceptionHandler extends MjDefaultGlobalExceptionHandler {
16+
public class HyExceptionHandler extends DefaultGlobalExceptionHandler {
2217

23-
private final HyProperties hyProperties;
24-
25-
@Override
26-
public SingleResponse<WebSiteInfo> handle4xxClientError(Exception ex) {
27-
return SingleResponse.success(super.handle4xxClientError(ex), hyProperties.getWebSiteInfo());
28-
}
29-
30-
@Override
31-
public SingleResponse<WebSiteInfo> handle5xxServerError(Exception ex) {
32-
return SingleResponse.success(super.handle5xxServerError(ex), hyProperties.getWebSiteInfo());
18+
public HyExceptionHandler(ExceptionHandlerFactory factory) {
19+
super(factory);
3320
}
3421
}

0 commit comments

Comments
 (0)