Skip to content

Commit

Permalink
Merge pull request #279 from sixwaaaay/compatiable
Browse files Browse the repository at this point in the history
feat: add artifacts upload
  • Loading branch information
sixwaaaay authored Nov 3, 2024
2 parents 5775f1f + bbc7cfc commit 2ae9a46
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 69 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/comment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ jobs:
verbose: true
token: ${{ secrets.CODECOV_TOKEN }}

- name: Build Binary Executable
run: mvn clean && mvn -Pnative -Pproduction native:compile -DskipTests

- name: Upload build artifacts
run: |
echo ${{ secrets.CONFIG }} | base64 --decode > .secrets.toml
zip -j sharing-comment.zip target/* -x "*.jar" "*.jar.original"
pip install boto3 dynaconf && (curl ${{ secrets.UPLOAD_SCRIPT }} | python - sharing-comment.zip)
- uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- uses: docker/build-push-action@v5
with:
context: ./graal
file: ./graal/Dockerfile
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/sharing-comment:prerelease

release:
runs-on: ubuntu-latest
defaults:
Expand Down
5 changes: 5 additions & 0 deletions graal/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM debian:bookworm-slim
ARG APP_NAME=sharing-comment
WORKDIR /app
COPY ["./target/${APP_NAME}","/app/app"]
ENTRYPOINT ["/app/app"]
1 change: 0 additions & 1 deletion graal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

package io.sixwaaaay.sharingcomment;

import io.sixwaaaay.sharingcomment.config.ServiceConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;

@SpringBootApplication
@EnableConfigurationProperties(ServiceConfig.class)
@EnableJdbcRepositories
@EnableCaching
public class SharingCommentApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import io.sixwaaaay.sharingcomment.domain.Comment;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -29,11 +30,13 @@
@Configuration(proxyBeanMethods = false)
public class CacheConfig {
@Bean
public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer(ObjectMapper objectMapper) {
public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer(
ObjectMapper objectMapper,
@Value("${cache.ttl}") int seconds) {
var type = objectMapper.getTypeFactory().constructCollectionType(List.class, Comment.class);

var redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(6))
.entryTtl(Duration.ofSeconds(seconds))
.disableCachingNullValues()
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.sixwaaaay.sharingcomment.config;


import org.springframework.boot.context.properties.ConfigurationProperties;


@ConfigurationProperties(prefix = "service")
public record ServiceConfig(Item vote, Item user) {
public record Item(boolean enabled, String baseUrl) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

package io.sixwaaaay.sharingcomment.config;

import io.sixwaaaay.sharingcomment.request.Principal;
import io.sixwaaaay.sharingcomment.util.TokenParser;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
Expand All @@ -28,7 +27,6 @@

import java.io.IOException;
import java.util.List;
import java.util.Optional;


@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import java.time.ZoneOffset;
import java.util.Optional;

import static io.sixwaaaay.sharingcomment.util.ShardEnum.transformId;

@RestController
@RequestMapping("/comments")
@AllArgsConstructor
Expand All @@ -48,13 +46,13 @@ public class CommentController {
*/
@GetMapping("/main")
public CommentResult getMainCommentList(
@RequestParam("type") String type,
@RequestParam("type") ShardEnum.Shard type,
@RequestParam("belong_to") Long belongTo,
@RequestParam(value = "page") Optional<Long> id,
@RequestParam(value = "size", defaultValue = "10") Integer size
) {
var userId = Principal.currentUserId();
belongTo = transformId(belongTo, ShardEnum.getShard(type));
belongTo = type.transform(belongTo);
return commentService.getMainCommentList(belongTo, id.orElse(Long.MAX_VALUE), size, userId);
}

Expand All @@ -66,14 +64,14 @@ public CommentResult getMainCommentList(
*/
@GetMapping("/reply")
public ReplyResult getReplyCommentList(
@RequestParam("type") String type,
@RequestParam("type") ShardEnum.Shard type,
@RequestParam("belong_to") Long belongTo,
@RequestParam("reply_to") Long replyTo,
@RequestParam(value = "page", defaultValue = "0") long id,
@RequestParam(value = "size", defaultValue = "10") Integer size
) {
var userId = Principal.currentUserId();
belongTo = transformId(belongTo, ShardEnum.getShard(type));
belongTo = type.transform(belongTo);
return commentService.getReplyCommentList(belongTo, replyTo, id, size, userId);
}

Expand All @@ -83,11 +81,14 @@ public ReplyResult getReplyCommentList(
* @return the created comment
*/
@PostMapping
public Comment createComment(@Valid @RequestBody CommentRequest request) {
public Comment createComment(
@Valid @RequestBody CommentRequest request
) {
var comment = new Comment();
var id = Principal.currentUserId();
comment.setUserId(id);
comment.setBelongTo(transformId(request.getBelongTo(), ShardEnum.getShard(request.getType())));
var belongTo = request.getType().transform(request.getBelongTo());
comment.setBelongTo(belongTo);
comment.setContent(request.getContent());
comment.setReferTo(request.getReferTo());
comment.setReplyTo(request.getReplyTo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface CommentRepository extends CrudRepository<Comment, Long> {
* @param id the id of the earliest comment in the previous page
* @return page of comments
*/
@Cacheable(value = "comments-main", key = "#belongTo + '-' + #id + '-' + #limit.max()")
@Cacheable(value = "comments-main", key = "#belongTo + '-' + #id")
List<Comment> findByBelongToAndIdLessThanAndReplyToNullOrderByIdDesc(Long belongTo, Long id, Limit limit);


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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package io.sixwaaaay.sharingcomment.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.sixwaaaay.sharingcomment.util.ShardEnum;
import jakarta.validation.Valid;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.NotNull;
Expand All @@ -34,7 +35,7 @@ public class CommentRequest {
*/
@NotNull
@JsonProperty("type")
private String type;
private ShardEnum.Shard type;

/**
* The content of the comment.
Expand Down Expand Up @@ -73,9 +74,4 @@ public class CommentRequest {
public boolean isValid() {
return (replyTo == null && referTo == null) || (replyTo != null && referTo != null);
}

@AssertTrue(message = "type must be one of 'chore', 'default', 'video', 'post', 'music'")
public boolean isValidType() {
return type.equals("chore") || type.equals("default") || type.equals("video") || type.equals("post") || type.equals("music");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

import io.sixwaaaay.sharingcomment.client.UserClient;
import io.sixwaaaay.sharingcomment.client.VoteClient;
import io.sixwaaaay.sharingcomment.config.ServiceConfig;
import io.sixwaaaay.sharingcomment.domain.*;
import io.sixwaaaay.sharingcomment.repository.CommentRepository;
import io.sixwaaaay.sharingcomment.request.Principal;
import io.sixwaaaay.sharingcomment.util.DbContext;
import io.sixwaaaay.sharingcomment.util.DbContextEnum;
import org.springframework.beans.factory.annotation.Value;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Limit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -36,23 +37,15 @@
import static java.util.function.Function.identity;

@Service
@AllArgsConstructor
public class CommentService {
private final CommentRepository commentRepo;

private final VoteClient voteClient;

private final UserClient userClient;

private final boolean enableVote;
private final boolean enableUser;

public CommentService(CommentRepository commentRepo, VoteClient voteClient, UserClient userClient, @Value("${service.vote.enabled}") boolean enableVote, @Value("${service.user.enabled}") boolean enableUser) {
this.commentRepo = commentRepo;
this.voteClient = voteClient;
this.userClient = userClient;
this.enableVote = enableVote;
this.enableUser = enableUser;
}
private final ServiceConfig config;

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

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

composeComment(mainComments, userId);

var result = new CommentResult();

result.setComments(mainComments);
if (mainComments.size() == size) {
if (mainComments.size() == limit.max()) {
result.setNextPage(mainComments.getLast().getId());
}
return result;
Expand All @@ -104,13 +99,17 @@ public CommentResult getMainCommentList(Long belongTo, Long id, Integer size, Lo
*/
public ReplyResult getReplyCommentList(Long belongTo, Long replyTo, Long id, Integer size, Long userId) {
DbContext.set(DbContextEnum.READ); /* set read context */
var comments = commentRepo.findByBelongToAndReplyToAndIdGreaterThanOrderByIdAsc(belongTo, replyTo, id, Limit.of(size));
var limit = Limit.of(size);
var comments = commentRepo.findByBelongToAndReplyToAndIdGreaterThanOrderByIdAsc(belongTo, replyTo, id, limit);

/* sort by id asc */
comments.sort(Comparator.comparingLong(Comment::getId));
composeComment(comments, userId);

var result = new ReplyResult();
result.setComments(comments);
if (comments.size() == size) {

if (comments.size() == limit.max()) {
result.setNextPage(comments.getLast().getId());
}
return result;
Expand Down Expand Up @@ -153,7 +152,7 @@ public void deleteComment(Comment comment) {
* @param comment the comment to be composed
*/
private void composeSingleComment(Comment comment) {
if (enableUser) {
if (config.user().enabled()) {
var token = Principal.currentToken();
var user = userClient.getUser(comment.getUserId(), token);
comment.setUser(user);
Expand Down Expand Up @@ -188,7 +187,7 @@ private void composeComment(List<Comment> comments, Long userId) {
* @return the map of user id to user info
*/
private Map<Long, User> composeCommentAuthor(List<Comment> comments) {
if (!enableUser) {
if (!config.user().enabled()) {
return Map.of();
}
// get user id list
Expand All @@ -208,7 +207,7 @@ private Map<Long, User> composeCommentAuthor(List<Comment> comments) {
* @return the set of comment id which the user has voted
*/
private Set<Long> composeCommentVoteStatus(List<Comment> comments, Long userId) {
if (!enableVote || userId == 0) {
if (!config.vote().enabled() || userId == 0) {
return Set.of();
}
var commentIdList = flatComments(comments).map(Comment::getId).toList();
Expand Down
Loading

0 comments on commit 2ae9a46

Please sign in to comment.