Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add endpoint to publish raw domain events #86

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
all: build

start:
@docker-compose -f docker-compose.ci.yml up -d
@docker compose -f docker-compose.ci.yml up -d

build:
@./gradlew build --warning-mode all
Expand Down
1 change: 1 addition & 0 deletions apps/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.main.allow-bean-definition-overriding=true
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@

const urlParts = inputs.map(input => input.name + "=" + input.value);

const url = "http://localhost:8091/courses?" + urlParts.join("&");
const url = "http://localhost:8040/courses?" + urlParts.join("&");

addCoursesList(url);
}
</script>

<script>
addCoursesList("http://localhost:8091/courses");
addCoursesList("http://localhost:8040/courses");
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;

import tv.codely.apps.backoffice.backend.command.ConsumeRabbitMqDomainEventsCommand;
import tv.codely.shared.domain.Service;

@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
Expand All @@ -17,8 +18,10 @@
public class BackofficeBackendApplication {

public static HashMap<String, Class<?>> commands() {
return new HashMap<String, Class<?>>() {
{}
return new HashMap<>() {
{
put("domain-events:rabbitmq:consume", ConsumeRabbitMqDomainEventsCommand.class);
}
};
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tv.codely.apps.backoffice.backend.command;

import tv.codely.shared.infrastructure.bus.event.rabbitmq.RabbitMqDomainEventsConsumer;
import tv.codely.shared.infrastructure.cli.ConsoleCommand;

public final class ConsumeRabbitMqDomainEventsCommand extends ConsoleCommand {

private final RabbitMqDomainEventsConsumer consumer;

public ConsumeRabbitMqDomainEventsCommand(RabbitMqDomainEventsConsumer consumer) {
this.consumer = consumer;
}

@Override
public void execute(String[] args) {
consumer.consume("backoffice");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a demo

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package tv.codely.apps.backoffice.frontend.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
Expand All @@ -10,6 +12,10 @@
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import tv.codely.shared.infrastructure.bus.event.mysql.MySqlEventBus;
import tv.codely.shared.infrastructure.bus.event.rabbitmq.RabbitMqEventBus;
import tv.codely.shared.infrastructure.bus.event.rabbitmq.RabbitMqPublisher;

@Configuration
@EnableWebMvc
public class BackofficeFrontendWebConfig implements WebMvcConfigurer {
Expand Down Expand Up @@ -43,4 +49,13 @@ public FreeMarkerConfigurer freeMarkerConfigurer() {

return configurer;
}

@Primary
@Bean
public RabbitMqEventBus rabbitMqEventBus(
RabbitMqPublisher publisher,
@Qualifier("backofficeMysqlEventBus") MySqlEventBus failoverPublisher
) {
return new RabbitMqEventBus(publisher, failoverPublisher);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public ConsumeRabbitMqDomainEventsCommand(RabbitMqDomainEventsConsumer consumer)

@Override
public void execute(String[] args) {
consumer.consume();
consumer.consume("mooc");
rgomezcasas marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tv.codely.apps.mooc.backend.config;

import java.util.Optional;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -10,17 +12,17 @@
@Configuration
public class MoocBackendServerConfiguration {

private final RequestMappingHandlerMapping mapping;
private final Optional<RequestMappingHandlerMapping> mapping;

public MoocBackendServerConfiguration(RequestMappingHandlerMapping mapping) {
public MoocBackendServerConfiguration(Optional<RequestMappingHandlerMapping> mapping) {
this.mapping = mapping;
}

@Bean
public FilterRegistrationBean<ApiExceptionMiddleware> apiExceptionMiddleware() {
FilterRegistrationBean<ApiExceptionMiddleware> registrationBean = new FilterRegistrationBean<>();

registrationBean.setFilter(new ApiExceptionMiddleware(mapping));
mapping.ifPresent(map -> registrationBean.setFilter(new ApiExceptionMiddleware(map)));

return registrationBean;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tv.codely.apps.mooc.backend.controller.playground;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import tv.codely.shared.domain.Utils;

@RestController
record DomainEventPostController(RabbitTemplate rabbitTemplate) {
@PostMapping(value = "/domain-events")
public ResponseEntity<String> index(@RequestBody Request request) {
System.out.println(request.eventName());

var serializedEvent = Utils.jsonEncode(request.eventRaw());

Message message = new Message(
serializedEvent.getBytes(),
MessagePropertiesBuilder.newInstance().setContentEncoding("utf-8").setContentType("application/json").build()
);

rabbitTemplate.send("domain_events", request.eventName(), message);

return new ResponseEntity<>(HttpStatus.CREATED);
}
}

final class Request {

private String eventName;
private Object eventRaw;

public void setEventName(String eventName) {
this.eventName = eventName;
}

public void setEventRaw(Object eventRaw) {
this.eventRaw = eventRaw;
}

String eventName() {
return eventName;
}

Object eventRaw() {
return eventRaw;
}
}
2 changes: 1 addition & 1 deletion docker-compose.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ services:
platform: linux/amd64
restart: unless-stopped
ports:
- "5630:5672"
- "5672:5672"
- "8090:15672"
environment:
- RABBITMQ_DEFAULT_USER=codely
Expand Down
38 changes: 32 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ services:
platform: linux/amd64
restart: unless-stopped
ports:
- "5630:5672"
- "5672:5672"
- "8090:15672"
environment:
- RABBITMQ_DEFAULT_USER=codely
Expand Down Expand Up @@ -51,12 +51,29 @@ services:
volumes:
- .:/app:delegated
- backoffice_backend_gradle_cache:/app/.gradle
- backoffice_backend_build:/app/build
depends_on:
- shared_mysql
- shared_rabbitmq
- backoffice_elasticsearch
command: ["./gradlew", "bootRun", "--args", "backoffice_backend server"]

backoffice_backend_consumers_java:
container_name: codely-java_ddd_example-backoffice_backend_consumers
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
volumes:
- .:/app:delegated
- backoffice_consumers_gradle_cache:/app/.gradle
- backoffice_consumers_build:/app/build
depends_on:
- shared_mysql
- shared_rabbitmq
- backoffice_elasticsearch
command: ["./gradlew", "bootRun", "--args", "backoffice_backend domain-events:rabbitmq:consume"]

backoffice_frontend_server_java:
container_name: codely-java_ddd_example-backoffice_frontend_server
build:
Expand All @@ -68,6 +85,7 @@ services:
volumes:
- .:/app:delegated
- backoffice_frontend_gradle_cache:/app/.gradle
- backoffice_frontend_build:/app/build
depends_on:
- shared_mysql
- shared_rabbitmq
Expand All @@ -85,29 +103,37 @@ services:
volumes:
- .:/app:delegated
- mooc_backend_gradle_cache:/app/.gradle
- mooc_backend_build:/app/build
depends_on:
- shared_mysql
- shared_rabbitmq
- backoffice_elasticsearch
command: ["./gradlew", "bootRun", "--args", "mooc_backend server"]

test_server_java:
container_name: codely-java_ddd_example-test_server
mooc_backend_consumers_java:
container_name: codely-java_ddd_example-mooc_backend_consumers
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
volumes:
- .:/app:delegated
- test_gradle_cache:/app/.gradle
- mooc_consumers_gradle_cache:/app/.gradle
- mooc_consumers_build:/app/build
depends_on:
- shared_mysql
- shared_rabbitmq
- backoffice_elasticsearch
tty: true
command: ["./gradlew", "bootRun", "--args", "mooc_backend domain-events:rabbitmq:consume"]

volumes:
backoffice_backend_gradle_cache:
backoffice_backend_build:
backoffice_consumers_gradle_cache:
backoffice_consumers_build:
backoffice_frontend_gradle_cache:
backoffice_frontend_build:
mooc_backend_gradle_cache:
test_gradle_cache:
mooc_backend_build:
mooc_consumers_gradle_cache:
mooc_consumers_build:
62 changes: 62 additions & 0 deletions etc/http/publish_domain_events.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
POST http://localhost:8030/domain-events
Content-Type: application/json

{
"eventName": "course.created",
"eventRaw": {
"data": {
"id": "{{$random.uuid}}",
"type": "course.created",
"occurred_on": "2023-11-14 10:00:00",
"attributes": {
"id": "c3a11f1d-512e-420b-aeae-e687a3c449aa",
"name": "Demo course",
"duration": "2 days"
}
},
"meta": {
}
}
}

###
POST http://localhost:8030/domain-events
Content-Type: application/json

{
"eventName": "course.renamed",
"eventRaw": {
"data": {
"id": "{{$random.uuid}}",
"type": "course.renamed",
"occurred_on": "2023-11-14 10:00:00",
"attributes": {
"id": "7b081a3e-f90e-4efe-a3a5-81e853e89c8b",
"name": "Este es el nombre bueno"
}
},
"meta": {
}
}
}

###
POST http://localhost:8030/domain-events
Content-Type: application/json

{
"eventName": "course.renamed",
"eventRaw": {
"data": {
"id": "{{$random.uuid}}",
"type": "course.renamed",
"occurred_on": "2022-11-14 10:00:00",
"attributes": {
"id": "7b081a3e-f90e-4efe-a3a5-81e853e89c8b",
"name": "Este es el nombre malo"
}
},
"meta": {
}
}
}
33 changes: 33 additions & 0 deletions etc/scripts/create_infinite_courses.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function create_course() {
eventId=$(uuidgen)
courseId=$(uuidgen)
courseName=$(shuf -n3 /usr/share/dict/words | xargs)
courseDuration=$((1 + RANDOM % 1000))

curl -X POST --location "http://localhost:8030/domain-events" \
-H "Content-Type: application/json" \
-d '{
"eventName": "course.created",
"eventRaw": {
"data": {
"id": "'"$eventId"'",
"type": "course.created",
"occurred_on": "2023-11-14 10:00:00",
"attributes": {
"id": "'"$courseId"'",
"name": "'"$courseName"'",
"duration": "'"$courseDuration"' days"
}
},
"meta": {
}
}
}'

echo "Created: $courseName"
}

while true; do
create_course &
sleep 0.001
done
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public BackofficeCourseCreator(BackofficeCourseRepository repository) {
}

public void create(String id, String name, String duration) {
this.repository.save(new BackofficeCourse(id, name, duration));
if (this.repository.search(id).isEmpty()) {
this.repository.save(new BackofficeCourse(id, name, duration));
}
}
}
Loading
Loading