Skip to content

Commit

Permalink
Refactor files
Browse files Browse the repository at this point in the history
  • Loading branch information
nntthuy-axonivy committed Dec 27, 2024
1 parent 5db7f07 commit 5dbe2ca
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 16 deletions.
4 changes: 3 additions & 1 deletion marketplace-build/.env
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ MARKET_GITHUB_OAUTH_APP_CLIENT_SECRET=
MARKET_JWT_SECRET_KEY=
MARKET_CORS_ALLOWED_ORIGIN=*
MARKET_MONGO_LOG_LEVEL=DEBUG
MARKET_LOG_PATH=logs
MARKET_LOG_PATH=logs
MARKET_CLICK_LIMIT=
MARKET_LIMITED_REQUEST_PATHS=
4 changes: 3 additions & 1 deletion marketplace-build/dev/.env
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ MARKET_GITHUB_OAUTH_APP_CLIENT_SECRET=
MARKET_JWT_SECRET_KEY=
MARKET_CORS_ALLOWED_ORIGIN=*
MARKET_MONGO_LOG_LEVEL=DEBUG
MARKET_LOG_PATH=logs
MARKET_LOG_PATH=logs
MARKET_CLICK_LIMIT=
MARKET_LIMITED_REQUEST_PATHS=
4 changes: 3 additions & 1 deletion marketplace-build/dev/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ services:
- MARKET_CORS_ALLOWED_ORIGIN=${MARKET_CORS_ALLOWED_ORIGIN}
- MARKET_MONGO_LOG_LEVEL=${MARKET_MONGO_LOG_LEVEL}
- MARKET_LOG_PATH=${MARKET_LOG_PATH}
- MARKET_CLICK_LIMIT=${MARKET_CLICK_LIMIT}
- MARKET_LIMITED_REQUEST_PATHS=${MARKET_LIMITED_REQUEST_PATHS}
build:
context: ../../marketplace-service
dockerfile: Dockerfile
Expand All @@ -53,4 +55,4 @@ volumes:

networks:
marketplace-network:
external: true
external: true
4 changes: 3 additions & 1 deletion marketplace-build/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ services:
- MARKET_CORS_ALLOWED_ORIGIN=${MARKET_CORS_ALLOWED_ORIGIN}
- MARKET_MONGO_LOG_LEVEL=${MARKET_MONGO_LOG_LEVEL}
- MARKET_LOG_PATH=${MARKET_LOG_PATH}
- MARKET_CLICK_LIMIT=${MARKET_CLICK_LIMIT}
- MARKET_LIMITED_REQUEST_PATHS=${MARKET_LIMITED_REQUEST_PATHS}
build:
context: ../marketplace-service
dockerfile: Dockerfile
Expand All @@ -53,4 +55,4 @@ volumes:

networks:
marketplace-network:
external: true
external: true
4 changes: 3 additions & 1 deletion marketplace-build/release/.env
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ MARKET_GITHUB_OAUTH_APP_CLIENT_SECRET=
MARKET_JWT_SECRET_KEY=
MARKET_CORS_ALLOWED_ORIGIN=*
MARKET_MONGO_LOG_LEVEL=DEBUG
MARKET_LOG_PATH=logs
MARKET_LOG_PATH=logs
MARKET_CLICK_LIMIT=
MARKET_LIMITED_REQUEST_PATHS=
2 changes: 2 additions & 0 deletions marketplace-build/release/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ services:
- MARKET_CORS_ALLOWED_ORIGIN=${MARKET_CORS_ALLOWED_ORIGIN}
- MARKET_MONGO_LOG_LEVEL=${MARKET_MONGO_LOG_LEVEL}
- MARKET_LOG_PATH=${MARKET_LOG_PATH}
- MARKET_CLICK_LIMIT=${MARKET_CLICK_LIMIT}
- MARKET_LIMITED_REQUEST_PATHS=${MARKET_LIMITED_REQUEST_PATHS}
networks:
- marketplace-network

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@

import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Bucket;
import io.github.bucket4j.Refill;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Log4j2
@Component
public class RateLimitFilter extends OncePerRequestFilter {
private static final String REQUEST_PATH = "/api";
@Value("${market.allowed.download-capacity}")
public class LimitCallingConfig extends OncePerRequestFilter {
private static final String REQUEST_HEADER = "X-Forwarded-For";
@Value("${market.allowed.click-capacity}")
private int capacity;

@Value("${market.limited.request-paths}")
private List<String> requestPaths;
private final Map<String, Bucket> buckets = new ConcurrentHashMap<>();

@Override
Expand All @@ -29,15 +34,13 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse

String clientIp = getClientIp(request);
String apiPath = request.getRequestURI();

if (apiPath.contains(REQUEST_PATH)) {
if (requestPaths.stream().anyMatch(path -> apiPath.matches(path + ".*"))) {
Bucket bucket = buckets.computeIfAbsent(clientIp, this::createNewBucket);

if (bucket.tryConsume(1)) {
System.out.println("Request allowed for IP: " + clientIp + ". Remaining tokens: " + bucket.getAvailableTokens());
log.warn("Request allowed for IP: {}. Remaining tokens: {}", clientIp, bucket.getAvailableTokens());
filterChain.doFilter(request, response);
} else {
System.out.println("Too many requests from IP: " + clientIp);
response.setStatus(HttpServletResponse.SC_BAD_GATEWAY);
response.getWriter().write("Too many requests. Please try again later.");
}
Expand All @@ -47,12 +50,15 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
}

private Bucket createNewBucket(String clientIp) {
Bandwidth limit = Bandwidth.classic(capacity, Refill.greedy(10, Duration.ofMinutes(2)));
Bandwidth limit = Bandwidth.builder()
.capacity(capacity)
.refillGreedy(capacity, Duration.ofMinutes(1))
.build();
return Bucket.builder().addLimit(limit).build();
}

private String getClientIp(HttpServletRequest request) {
String forwardedFor = request.getHeader("X-Forwarded-For");
String forwardedFor = request.getHeader(REQUEST_HEADER);
if (forwardedFor != null && !forwardedFor.isEmpty()) {
return forwardedFor.split(",")[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ jwt.secret=${MARKET_JWT_SECRET_KEY}
jwt.expiration=365
logging.level.org.springframework.data.mongodb.core.MongoTemplate=${MARKET_MONGO_LOG_LEVEL}
spring.jackson.serialization.indent_output=true
loggable.log-path=${MARKET_LOG_PATH}
loggable.log-path=${MARKET_LOG_PATH}
market.allowed.click-capacity=${MARKET_CLICK_LIMIT}
market.limited.request-paths=${MARKET_LIMITED_REQUEST_PATHS}

0 comments on commit 5dbe2ca

Please sign in to comment.