Skip to content

Commit

Permalink
Merge pull request #4 from asavershin/add-kafka
Browse files Browse the repository at this point in the history
Add kafka
  • Loading branch information
asavershin authored Jun 20, 2024
2 parents ddde0e0 + b9cb4eb commit 4c24d4e
Show file tree
Hide file tree
Showing 137 changed files with 4,421 additions and 350 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# Start api

./mvnw clean package
В папке api: docker compose up -d

docker compose -f kafka/docker-compose.yml up --build -d

После того как запустится кафка

docker compose -f api/docker-compose.yml up --build -d

docker compose -f worker/docker-compose-blackwhite.yml up --build -d
docker compose -f worker/docker-compose-rotate.yml up --build -d
docker compose -f worker/docker-compose-imagga.yml up --build -d



2 changes: 2 additions & 0 deletions api/.env
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ REDIS_CACHE_TIME=86400000

# MINIO
MINIO_BUCKET=files
MINIO_EXPIRATION=1
MINIO_URL=http://minio-api:9000
MINIO_ROOT_USER=minioadmin
MINIO_ROOT_PASSWORD=minioadmin
MINIO_CONSOLE_PORT=9090
MINIO_PORT=9000
MINIO_TTL_PREFIX=temporary/

# MONGO
MONGO_INITDB_ROOT_USERNAME=admin
Expand Down
22 changes: 19 additions & 3 deletions api/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
version: '3.8'
networks:
kafka-net:
name: kafka-net
driver: bridge
api-net:
name: api-net
driver: bridge

services:

backend-api:
container_name: backend-api
networks:
- kafka-net
- api-net
build:
context: .
dockerfile: Dockerfile
Expand All @@ -14,7 +23,10 @@ services:
- redis-api
env_file:
- .env

db-api:
networks:
- api-net
image: postgres:15.1-alpine
container_name: db-api
env_file:
Expand All @@ -25,6 +37,8 @@ services:
- db-api-data:/var/lib/postgresql/data/

redis-api:
networks:
- api-net
image: redis:7.2-rc-alpine
restart: always
container_name: redis-api
Expand All @@ -35,7 +49,9 @@ services:
- redis-api-data:/data

minio-api:
image: minio/minio:latest
networks:
- api-net
image: minio/minio:RELEASE.2024-02-14T21-36-02Z
container_name: minio-api
env_file:
- .env
Expand All @@ -49,4 +65,4 @@ services:
volumes:
db-api-data:
redis-api-data:
minio-api-data:
minio-api-data:
25 changes: 25 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,23 @@
<!-- TESTS-->
<containers.version>1.19.1</containers.version>
<!-- TESTS-->

</properties>

<dependencies>

<dependency>
<groupId>com.bucket4j</groupId>
<artifactId>bucket4j-core</artifactId>
<version>8.10.1</version>
</dependency>

<dependency>
<groupId>com.bucket4j</groupId>
<artifactId>bucket4j-redis</artifactId>
<version>8.10.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down Expand Up @@ -165,6 +179,17 @@
</dependency>
<!-- FOR TESTS ONLY-->

<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>


</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.github.asavershin.api.common;

import lombok.Getter;

@Getter
public class NotFoundException extends RuntimeException {
/**
* @param message specifies information about the object not found
* Constructs a new instance of {@code NotFoundException}
* with the specified error message.
*
* @param message the error message
*/
public NotFoundException(final String message) {
super(message);
Expand Down
80 changes: 57 additions & 23 deletions api/src/main/java/com/github/asavershin/api/common/Validator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.asavershin.api.common;

import com.github.asavershin.api.domain.filter.Filter;

import java.util.Collection;
import java.util.Objects;

/**
Expand All @@ -13,12 +16,13 @@ public abstract class Validator {
* Asserts that the given string has a length within
* the specified range.
*
* @param aString the string to validate
* @param aString the string to validate
* @param aMinimum the minimum length
* @param aMaximum the maximum length
* @param aMessage the message to throw if the length is invalid
* @throws IllegalArgumentException if the length of the string
* is less than {@code aMinimum} or greater than {@code aMaximum}
* is less than {@code aMinimum}
* or greater than {@code aMaximum}
*/
public static void assertArgumentLength(final String aString,
final int aMinimum,
Expand All @@ -29,15 +33,16 @@ public static void assertArgumentLength(final String aString,
throw new IllegalArgumentException(aMessage);
}
}

/**
* Asserts that the given string has a length greater than or equal
* to the specified minimum.
*
* @param aString the string to validate
* @param aString the string to validate
* @param aMinimum the minimum length
* @param aMessage the message to throw if the length is invalid
* @throws IllegalArgumentException if the length of the string is
* less than {@code aMinimum}
* less than {@code aMinimum}
*/
public static void assertArgumentLength(final String aString,
final int aMinimum,
Expand All @@ -52,12 +57,12 @@ public static void assertArgumentLength(final String aString,
* Asserts that the given string matches the specified regular
* expression.
*
* @param email the string to validate
* @param regex the regular expression to match against
* @param email the string to validate
* @param regex the regular expression to match against
* @param aMessage the message to throw if the string does not
* match the regular expression
* match the regular expression
* @throws IllegalArgumentException if the string does not
* match the regular expression
* match the regular expression
*/
public static void assertStringFormat(final String email,
final String regex,
Expand All @@ -71,12 +76,12 @@ public static void assertStringFormat(final String email,
* Asserts that the given array has a length greater than or
* equal to the specified minimum.
*
* @param array the array to validate
* @param array the array to validate
* @param minLength the minimum length
* @param aMessage the message to throw if the length of the
* array is less than {@code minLength}
* @param aMessage the message to throw if the length of the
* array is less than {@code minLength}
* @throws IllegalArgumentException if the length of the array
* is less than {@code minLength}
* is less than {@code minLength}
*/
public static void assertArrayLength(final Object[] array,
final Integer minLength,
Expand All @@ -89,13 +94,14 @@ public static void assertArrayLength(final Object[] array,
/**
* Asserts that the given value is within the specified range.
*
* @param value the value to validate
* @param value the value to validate
* @param minLength the minimum length
* @param maxLength the maximum length
* @param aMessage the message to throw if the value is outside
* the specified range
* @param aMessage the message to throw if the value is outside
* the specified range
* @throws IllegalArgumentException if the value is less than
* {@code minLength} or greater than {@code maxLength}
* {@code minLength} or greater
* than {@code maxLength}
*/
public static void assertLongSize(final Long value,
final Long minLength,
Expand All @@ -110,12 +116,12 @@ public static void assertLongSize(final Long value,
* Asserts that the given value is greater than or equal to the
* specified minimum.
*
* @param value the value to validate
* @param value the value to validate
* @param minLength the minimum length
* @param aMessage the message to throw if the value is less
* than {@code minLength}
* @param aMessage the message to throw if the value is less
* than {@code minLength}
* @throws IllegalArgumentException if the value is less
* than {@code minLength}
* than {@code minLength}
*/
public static void assertLongSize(final Long value,
final Long minLength,
Expand All @@ -128,14 +134,42 @@ public static void assertLongSize(final Long value,
/**
* Asserts that the given object is not null.
*
* @param object the object to validate
* @param object the object to validate
* @param aMessage the message to throw if the object is null
* @param <T> Any type of aggregators or entities
* @throws IllegalArgumentException if the object is null
* @return object if the object is not null
*/
public static void assertNotFound(final Object object,
final String aMessage) {
public static <T> T assertNotFound(final T object,
final String aMessage) {
if (Objects.isNull(object)) {
throw new NotFoundException(aMessage);
}
return object;
}

/**
* Asserts that the given list of filters has a length within
* the specified range.
*
* @param filters the list of filters to validate
* @param minValue the minimum length of the list
* @param maxValue the maximum length of the list
* @param message the message to throw if the length of the
* list is invalid
* @throws IllegalArgumentException if the length of the list
* is less than {@code i} or
* greater than {@code maxValue}
*/
public static void assertCollectionLen(
final Collection<Filter> filters,
final Integer minValue,
final Integer maxValue,
final String message) {
var len = filters.size();

if (len < minValue || len > maxValue) {
throw new IllegalArgumentException(message);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.asavershin.api.common.domain;

public interface DomainEvent<T> {
/**
* Using for publishing new events.
* @return Message for publisher
*/
T publish();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Some common classes for domain.
*/
package com.github.asavershin.api.common.domain;
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
value = Command.class)
}
)
public class AnnotationsConfig {
public class AnnotationsConf {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@Configuration
@RequiredArgsConstructor
public class AuthConfig {
public class AuthConf {
/**
* Configures a BCryptPasswordEncoder bean for password encoding.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.asavershin.api.config;

import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Bucket;
import io.github.bucket4j.BucketConfiguration;
import io.github.bucket4j.distributed.ExpirationAfterWriteStrategy;
import io.github.bucket4j.distributed.proxy.ClientSideConfig;
import io.github.bucket4j.distributed.proxy.ProxyManager;
import io.github.bucket4j.redis.lettuce.cas.LettuceBasedProxyManager;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.codec.ByteArrayCodec;
import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.codec.StringCodec;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.Duration;
import java.util.function.Supplier;

@Configuration
public class Bucket4jConfig {
@Bean
public RedisClient redisClient(final RedisProperties redisProperties) {
return RedisClient.create(
RedisURI.Builder.redis(redisProperties.getHost(), redisProperties.getPort())
.withPassword(redisProperties.getPassword().toCharArray()).build());
}

@Bean
public ProxyManager<String> lettuceBasedProxyManager(final RedisClient redisClient) {
StatefulRedisConnection<String, byte[]> redisConnection = redisClient
.connect(RedisCodec.of(StringCodec.UTF8, ByteArrayCodec.INSTANCE));

return LettuceBasedProxyManager
.builderFor(redisConnection)
.build();
}
}
Loading

0 comments on commit 4c24d4e

Please sign in to comment.