Skip to content

Commit 2ae9a46

Browse files
authored
Merge pull request #279 from sixwaaaay/compatiable
feat: add artifacts upload
2 parents 5775f1f + bbc7cfc commit 2ae9a46

File tree

13 files changed

+101
-69
lines changed

13 files changed

+101
-69
lines changed

.github/workflows/comment.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,27 @@ jobs:
6969
verbose: true
7070
token: ${{ secrets.CODECOV_TOKEN }}
7171

72+
- name: Build Binary Executable
73+
run: mvn clean && mvn -Pnative -Pproduction native:compile -DskipTests
74+
75+
- name: Upload build artifacts
76+
run: |
77+
echo ${{ secrets.CONFIG }} | base64 --decode > .secrets.toml
78+
zip -j sharing-comment.zip target/* -x "*.jar" "*.jar.original"
79+
pip install boto3 dynaconf && (curl ${{ secrets.UPLOAD_SCRIPT }} | python - sharing-comment.zip)
80+
81+
- uses: docker/login-action@v3
82+
with:
83+
username: ${{ secrets.DOCKERHUB_USERNAME }}
84+
password: ${{ secrets.DOCKERHUB_TOKEN }}
85+
86+
- uses: docker/build-push-action@v5
87+
with:
88+
context: ./graal
89+
file: ./graal/Dockerfile
90+
push: true
91+
tags: ${{ secrets.DOCKERHUB_USERNAME }}/sharing-comment:prerelease
92+
7293
release:
7394
runs-on: ubuntu-latest
7495
defaults:

graal/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM debian:bookworm-slim
2+
ARG APP_NAME=sharing-comment
3+
WORKDIR /app
4+
COPY ["./target/${APP_NAME}","/app/app"]
5+
ENTRYPOINT ["/app/app"]

graal/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
<groupId>org.springframework.boot</groupId>
5757
<artifactId>spring-boot-starter-web</artifactId>
5858
<exclusions>
59-
<!-- Exclude the Tomcat dependency -->
6059
<exclusion>
6160
<groupId>org.springframework.boot</groupId>
6261
<artifactId>spring-boot-starter-tomcat</artifactId>

graal/src/main/java/io/sixwaaaay/sharingcomment/SharingCommentApplication.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313

1414
package io.sixwaaaay.sharingcomment;
1515

16+
import io.sixwaaaay.sharingcomment.config.ServiceConfig;
1617
import org.springframework.boot.SpringApplication;
1718
import org.springframework.boot.autoconfigure.SpringBootApplication;
19+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
1820
import org.springframework.cache.annotation.EnableCaching;
1921
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
2022

2123
@SpringBootApplication
24+
@EnableConfigurationProperties(ServiceConfig.class)
2225
@EnableJdbcRepositories
2326
@EnableCaching
2427
public class SharingCommentApplication {

graal/src/main/java/io/sixwaaaay/sharingcomment/config/CacheConfig.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.fasterxml.jackson.databind.ObjectMapper;
1818
import io.sixwaaaay.sharingcomment.domain.Comment;
19+
import org.springframework.beans.factory.annotation.Value;
1920
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
2021
import org.springframework.context.annotation.Bean;
2122
import org.springframework.context.annotation.Configuration;
@@ -29,11 +30,13 @@
2930
@Configuration(proxyBeanMethods = false)
3031
public class CacheConfig {
3132
@Bean
32-
public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer(ObjectMapper objectMapper) {
33+
public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer(
34+
ObjectMapper objectMapper,
35+
@Value("${cache.ttl}") int seconds) {
3336
var type = objectMapper.getTypeFactory().constructCollectionType(List.class, Comment.class);
3437

3538
var redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
36-
.entryTtl(Duration.ofMinutes(6))
39+
.entryTtl(Duration.ofSeconds(seconds))
3740
.disableCachingNullValues()
3841
.serializeValuesWith(
3942
RedisSerializationContext.SerializationPair.fromSerializer(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.sixwaaaay.sharingcomment.config;
2+
3+
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
6+
7+
@ConfigurationProperties(prefix = "service")
8+
public record ServiceConfig(Item vote, Item user) {
9+
public record Item(boolean enabled, String baseUrl) {
10+
}
11+
}

graal/src/main/java/io/sixwaaaay/sharingcomment/config/ServiceInterceptor.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
package io.sixwaaaay.sharingcomment.config;
1515

16-
import io.sixwaaaay.sharingcomment.request.Principal;
1716
import io.sixwaaaay.sharingcomment.util.TokenParser;
1817
import jakarta.servlet.FilterChain;
1918
import jakarta.servlet.ServletException;
@@ -28,7 +27,6 @@
2827

2928
import java.io.IOException;
3029
import java.util.List;
31-
import java.util.Optional;
3230

3331

3432
@AllArgsConstructor

graal/src/main/java/io/sixwaaaay/sharingcomment/controller/CommentController.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import java.time.ZoneOffset;
3131
import java.util.Optional;
3232

33-
import static io.sixwaaaay.sharingcomment.util.ShardEnum.transformId;
34-
3533
@RestController
3634
@RequestMapping("/comments")
3735
@AllArgsConstructor
@@ -48,13 +46,13 @@ public class CommentController {
4846
*/
4947
@GetMapping("/main")
5048
public CommentResult getMainCommentList(
51-
@RequestParam("type") String type,
49+
@RequestParam("type") ShardEnum.Shard type,
5250
@RequestParam("belong_to") Long belongTo,
5351
@RequestParam(value = "page") Optional<Long> id,
5452
@RequestParam(value = "size", defaultValue = "10") Integer size
5553
) {
5654
var userId = Principal.currentUserId();
57-
belongTo = transformId(belongTo, ShardEnum.getShard(type));
55+
belongTo = type.transform(belongTo);
5856
return commentService.getMainCommentList(belongTo, id.orElse(Long.MAX_VALUE), size, userId);
5957
}
6058

@@ -66,14 +64,14 @@ public CommentResult getMainCommentList(
6664
*/
6765
@GetMapping("/reply")
6866
public ReplyResult getReplyCommentList(
69-
@RequestParam("type") String type,
67+
@RequestParam("type") ShardEnum.Shard type,
7068
@RequestParam("belong_to") Long belongTo,
7169
@RequestParam("reply_to") Long replyTo,
7270
@RequestParam(value = "page", defaultValue = "0") long id,
7371
@RequestParam(value = "size", defaultValue = "10") Integer size
7472
) {
7573
var userId = Principal.currentUserId();
76-
belongTo = transformId(belongTo, ShardEnum.getShard(type));
74+
belongTo = type.transform(belongTo);
7775
return commentService.getReplyCommentList(belongTo, replyTo, id, size, userId);
7876
}
7977

@@ -83,11 +81,14 @@ public ReplyResult getReplyCommentList(
8381
* @return the created comment
8482
*/
8583
@PostMapping
86-
public Comment createComment(@Valid @RequestBody CommentRequest request) {
84+
public Comment createComment(
85+
@Valid @RequestBody CommentRequest request
86+
) {
8787
var comment = new Comment();
8888
var id = Principal.currentUserId();
8989
comment.setUserId(id);
90-
comment.setBelongTo(transformId(request.getBelongTo(), ShardEnum.getShard(request.getType())));
90+
var belongTo = request.getType().transform(request.getBelongTo());
91+
comment.setBelongTo(belongTo);
9192
comment.setContent(request.getContent());
9293
comment.setReferTo(request.getReferTo());
9394
comment.setReplyTo(request.getReplyTo());

graal/src/main/java/io/sixwaaaay/sharingcomment/repository/CommentRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public interface CommentRepository extends CrudRepository<Comment, Long> {
3535
* @param id the id of the earliest comment in the previous page
3636
* @return page of comments
3737
*/
38-
@Cacheable(value = "comments-main", key = "#belongTo + '-' + #id + '-' + #limit.max()")
38+
@Cacheable(value = "comments-main", key = "#belongTo + '-' + #id")
3939
List<Comment> findByBelongToAndIdLessThanAndReplyToNullOrderByIdDesc(Long belongTo, Long id, Limit limit);
4040

4141

@@ -51,7 +51,7 @@ public interface CommentRepository extends CrudRepository<Comment, Long> {
5151
* @param limit the limit
5252
* @return the list of comments
5353
*/
54-
@Cacheable(value = "comments-reply", key = "#belongTo + '-' + #replyTo + '-' + #id + '-' + #limit.max()")
54+
@Cacheable(value = "comments-reply", key = "#belongTo + '-' + #replyTo + '-' + #id")
5555
List<Comment> findByBelongToAndReplyToAndIdGreaterThanOrderByIdAsc(Long belongTo, Long replyTo, Long id, Limit limit);
5656

5757

graal/src/main/java/io/sixwaaaay/sharingcomment/request/CommentRequest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package io.sixwaaaay.sharingcomment.request;
1515

1616
import com.fasterxml.jackson.annotation.JsonProperty;
17+
import io.sixwaaaay.sharingcomment.util.ShardEnum;
1718
import jakarta.validation.Valid;
1819
import jakarta.validation.constraints.AssertTrue;
1920
import jakarta.validation.constraints.NotNull;
@@ -34,7 +35,7 @@ public class CommentRequest {
3435
*/
3536
@NotNull
3637
@JsonProperty("type")
37-
private String type;
38+
private ShardEnum.Shard type;
3839

3940
/**
4041
* The content of the comment.
@@ -73,9 +74,4 @@ public class CommentRequest {
7374
public boolean isValid() {
7475
return (replyTo == null && referTo == null) || (replyTo != null && referTo != null);
7576
}
76-
77-
@AssertTrue(message = "type must be one of 'chore', 'default', 'video', 'post', 'music'")
78-
public boolean isValidType() {
79-
return type.equals("chore") || type.equals("default") || type.equals("video") || type.equals("post") || type.equals("music");
80-
}
8177
}

graal/src/main/java/io/sixwaaaay/sharingcomment/service/CommentService.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616

1717
import io.sixwaaaay.sharingcomment.client.UserClient;
1818
import io.sixwaaaay.sharingcomment.client.VoteClient;
19+
import io.sixwaaaay.sharingcomment.config.ServiceConfig;
1920
import io.sixwaaaay.sharingcomment.domain.*;
2021
import io.sixwaaaay.sharingcomment.repository.CommentRepository;
2122
import io.sixwaaaay.sharingcomment.request.Principal;
2223
import io.sixwaaaay.sharingcomment.util.DbContext;
2324
import io.sixwaaaay.sharingcomment.util.DbContextEnum;
24-
import org.springframework.beans.factory.annotation.Value;
25+
import lombok.AllArgsConstructor;
2526
import org.springframework.data.domain.Limit;
2627
import org.springframework.stereotype.Service;
2728
import org.springframework.transaction.annotation.Transactional;
@@ -36,23 +37,15 @@
3637
import static java.util.function.Function.identity;
3738

3839
@Service
40+
@AllArgsConstructor
3941
public class CommentService {
4042
private final CommentRepository commentRepo;
4143

4244
private final VoteClient voteClient;
4345

4446
private final UserClient userClient;
4547

46-
private final boolean enableVote;
47-
private final boolean enableUser;
48-
49-
public CommentService(CommentRepository commentRepo, VoteClient voteClient, UserClient userClient, @Value("${service.vote.enabled}") boolean enableVote, @Value("${service.user.enabled}") boolean enableUser) {
50-
this.commentRepo = commentRepo;
51-
this.voteClient = voteClient;
52-
this.userClient = userClient;
53-
this.enableVote = enableVote;
54-
this.enableUser = enableUser;
55-
}
48+
private final ServiceConfig config;
5649

5750
/**
5851
* This method is used to get the main comment list.
@@ -71,9 +64,9 @@ public CommentService(CommentRepository commentRepo, VoteClient voteClient, User
7164
*/
7265
public CommentResult getMainCommentList(Long belongTo, Long id, Integer size, Long userId) {
7366
DbContext.set(DbContextEnum.READ); /* set read context */
74-
var result = new CommentResult();
7567

76-
var mainComments = commentRepo.findByBelongToAndIdLessThanAndReplyToNullOrderByIdDesc(belongTo, id, Limit.of(size));
68+
var limit = Limit.of(size);
69+
var mainComments = commentRepo.findByBelongToAndIdLessThanAndReplyToNullOrderByIdDesc(belongTo, id, limit);
7770
/* for each main comment which has reply comments, get the latest 2 reply comments */
7871
mainComments.stream().filter(comment -> comment.getReplyCount() != 0).forEach(comment -> {
7972
var replyComments = commentRepo.findByBelongToAndReplyToAndIdGreaterThanOrderByIdAsc(belongTo, comment.getId(), 0L, Limit.of(2));
@@ -82,8 +75,10 @@ public CommentResult getMainCommentList(Long belongTo, Long id, Integer size, Lo
8275

8376
composeComment(mainComments, userId);
8477

78+
var result = new CommentResult();
79+
8580
result.setComments(mainComments);
86-
if (mainComments.size() == size) {
81+
if (mainComments.size() == limit.max()) {
8782
result.setNextPage(mainComments.getLast().getId());
8883
}
8984
return result;
@@ -104,13 +99,17 @@ public CommentResult getMainCommentList(Long belongTo, Long id, Integer size, Lo
10499
*/
105100
public ReplyResult getReplyCommentList(Long belongTo, Long replyTo, Long id, Integer size, Long userId) {
106101
DbContext.set(DbContextEnum.READ); /* set read context */
107-
var comments = commentRepo.findByBelongToAndReplyToAndIdGreaterThanOrderByIdAsc(belongTo, replyTo, id, Limit.of(size));
102+
var limit = Limit.of(size);
103+
var comments = commentRepo.findByBelongToAndReplyToAndIdGreaterThanOrderByIdAsc(belongTo, replyTo, id, limit);
104+
108105
/* sort by id asc */
109106
comments.sort(Comparator.comparingLong(Comment::getId));
110107
composeComment(comments, userId);
108+
111109
var result = new ReplyResult();
112110
result.setComments(comments);
113-
if (comments.size() == size) {
111+
112+
if (comments.size() == limit.max()) {
114113
result.setNextPage(comments.getLast().getId());
115114
}
116115
return result;
@@ -153,7 +152,7 @@ public void deleteComment(Comment comment) {
153152
* @param comment the comment to be composed
154153
*/
155154
private void composeSingleComment(Comment comment) {
156-
if (enableUser) {
155+
if (config.user().enabled()) {
157156
var token = Principal.currentToken();
158157
var user = userClient.getUser(comment.getUserId(), token);
159158
comment.setUser(user);
@@ -188,7 +187,7 @@ private void composeComment(List<Comment> comments, Long userId) {
188187
* @return the map of user id to user info
189188
*/
190189
private Map<Long, User> composeCommentAuthor(List<Comment> comments) {
191-
if (!enableUser) {
190+
if (!config.user().enabled()) {
192191
return Map.of();
193192
}
194193
// get user id list
@@ -208,7 +207,7 @@ private Map<Long, User> composeCommentAuthor(List<Comment> comments) {
208207
* @return the set of comment id which the user has voted
209208
*/
210209
private Set<Long> composeCommentVoteStatus(List<Comment> comments, Long userId) {
211-
if (!enableVote || userId == 0) {
210+
if (!config.vote().enabled() || userId == 0) {
212211
return Set.of();
213212
}
214213
var commentIdList = flatComments(comments).map(Comment::getId).toList();

0 commit comments

Comments
 (0)