-
Notifications
You must be signed in to change notification settings - Fork 0
[#5] Implement command service skeleton with Kafka producer #117
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3b8d6d5
[#5] Implement command service skeleton with Kafka producer
igorsatsyuk b107f4c
[#5] Address PR review comments
igorsatsyuk 2968fc5
[#5] Address second round of Copilot review comments
igorsatsyuk 261f20c
[#5] Address third round of Copilot review: fix unbounded heap growth…
igorsatsyuk 7e6e043
[#5] Address fourth Copilot review comments
igorsatsyuk f14b83f
[#5] Add Kafka integration coverage and metadata fallback test
igorsatsyuk ada7541
[#5] Cover missing request body envelope handling
igorsatsyuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Command Service (Issue #5) | ||
|
|
||
| MVP command-side service that accepts user login commands and publishes `UserLoggedInEvent` to Kafka. | ||
|
|
||
| ## Implemented | ||
|
|
||
| - WebFlux endpoint: `POST /commands/user/login` | ||
| - Kafka producer to topic `user.login.events` | ||
| - Temporary in-memory event storage for accepted events | ||
| - Validation + error handling responses in `CommandResponse` format | ||
|
|
||
| ## Run | ||
|
|
||
| From `backend/`: | ||
|
|
||
| ```pwsh | ||
| mvn spring-boot:run -pl command-service -am | ||
| ``` | ||
|
|
||
| ## Quick check | ||
|
|
||
| ```pwsh | ||
| curl -X POST http://localhost:8081/commands/user/login -H "Content-Type: application/json" -d '{"userId":"user1"}' | ||
| ``` | ||
|
|
||
| ## Test | ||
|
|
||
| From `backend/`: | ||
|
|
||
| ```pwsh | ||
| mvn -pl command-service -am test | ||
| ``` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...and-service/src/main/java/lt/satsyuk/distributed/audit/command/api/CommandController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package lt.satsyuk.distributed.audit.command.api; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import lt.satsyuk.distributed.audit.command.service.UserLoginCommandService; | ||
| import lt.satsyuk.distributed.audit.contracts.command.UserLoginCommand; | ||
| import lt.satsyuk.distributed.audit.contracts.dto.CommandResponse; | ||
| import org.springframework.http.HttpHeaders; | ||
| 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.RequestHeader; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import org.springframework.http.server.reactive.ServerHttpRequest; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| @RestController | ||
| public class CommandController { | ||
|
|
||
| private final UserLoginCommandService userLoginCommandService; | ||
|
|
||
| public CommandController(UserLoginCommandService userLoginCommandService) { | ||
| this.userLoginCommandService = userLoginCommandService; | ||
| } | ||
|
|
||
| @PostMapping("/commands/user/login") | ||
| public Mono<ResponseEntity<CommandResponse>> userLogin( | ||
| @Valid @RequestBody UserLoginCommand command, | ||
|
igorsatsyuk marked this conversation as resolved.
|
||
| @RequestHeader(value = HttpHeaders.USER_AGENT, required = false) String requestUserAgent, | ||
| ServerHttpRequest request | ||
| ) { | ||
| String requestIp = null; | ||
| if (request.getRemoteAddress() != null && request.getRemoteAddress().getAddress() != null) { | ||
| requestIp = request.getRemoteAddress().getAddress().getHostAddress(); | ||
| } | ||
|
|
||
| return userLoginCommandService.handleUserLogin(command, requestIp, requestUserAgent) | ||
| .map(response -> ResponseEntity.accepted().body(response)); | ||
| } | ||
| } | ||
|
|
||
51 changes: 51 additions & 0 deletions
51
...ervice/src/main/java/lt/satsyuk/distributed/audit/command/api/GlobalExceptionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package lt.satsyuk.distributed.audit.command.api; | ||
|
|
||
| import lt.satsyuk.distributed.audit.command.service.CommandPublishException; | ||
| import lt.satsyuk.distributed.audit.contracts.dto.CommandResponse; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.validation.FieldError; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
| import org.springframework.web.bind.support.WebExchangeBindException; | ||
| import org.springframework.web.server.ServerWebInputException; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import java.util.stream.Collectors; | ||
|
|
||
| @RestControllerAdvice | ||
| public class GlobalExceptionHandler { | ||
|
|
||
| @ExceptionHandler(WebExchangeBindException.class) | ||
| public ResponseEntity<CommandResponse> handleValidationError(WebExchangeBindException exception) { | ||
|
igorsatsyuk marked this conversation as resolved.
|
||
| String message = exception.getBindingResult() | ||
| .getFieldErrors() | ||
| .stream() | ||
| .map(this::formatFieldError) | ||
| .collect(Collectors.joining("; ")); | ||
|
|
||
| return ResponseEntity.badRequest().body(CommandResponse.rejected(message)); | ||
| } | ||
|
|
||
| @ExceptionHandler(ServerWebInputException.class) | ||
| public ResponseEntity<CommandResponse> handleWebInputError(ServerWebInputException exception) { | ||
| String message = Objects.requireNonNullElse(exception.getReason(), "Invalid request payload"); | ||
| return ResponseEntity.badRequest().body(CommandResponse.rejected(message)); | ||
|
igorsatsyuk marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @ExceptionHandler(CommandPublishException.class) | ||
| public ResponseEntity<CommandResponse> handlePublishError(CommandPublishException exception) { | ||
| return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE) | ||
| .body(CommandResponse.rejected(exception.getMessage())); | ||
|
igorsatsyuk marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private String formatFieldError(FieldError error) { | ||
| String defaultMessage = Objects.requireNonNullElse(error.getDefaultMessage(), "Validation failed"); | ||
| if (defaultMessage.contains(error.getField())) { | ||
| return defaultMessage; | ||
| } | ||
| return error.getField() + " " + defaultMessage; | ||
| } | ||
| } | ||
|
|
||
21 changes: 21 additions & 0 deletions
21
...vice/src/main/java/lt/satsyuk/distributed/audit/command/config/KafkaTopicsProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package lt.satsyuk.distributed.audit.command.config; | ||
|
|
||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
|
||
| /** | ||
| * Externalized Kafka topic names used by command-service. | ||
| */ | ||
| @ConfigurationProperties(prefix = "kafka.topics") | ||
| public class KafkaTopicsProperties { | ||
|
|
||
| private String userLoginEvents; | ||
|
|
||
| public String getUserLoginEvents() { | ||
| return userLoginEvents; | ||
| } | ||
|
|
||
| public void setUserLoginEvents(String userLoginEvents) { | ||
| this.userLoginEvents = userLoginEvents; | ||
| } | ||
| } | ||
|
|
33 changes: 33 additions & 0 deletions
33
...e/src/main/java/lt/satsyuk/distributed/audit/command/repository/InMemoryEventStorage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package lt.satsyuk.distributed.audit.command.repository; | ||
|
|
||
| import lt.satsyuk.distributed.audit.event.AuditEvent; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.ConcurrentLinkedQueue; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| /** | ||
| * Temporary in-memory storage for accepted events (MVP stage for issue #5). | ||
| */ | ||
| @Component | ||
| public class InMemoryEventStorage { | ||
|
|
||
| private final ConcurrentLinkedQueue<AuditEvent> events = new ConcurrentLinkedQueue<>(); | ||
| private final AtomicInteger eventsCount = new AtomicInteger(0); | ||
|
|
||
| public void save(AuditEvent event) { | ||
| events.add(event); | ||
| eventsCount.incrementAndGet(); | ||
|
igorsatsyuk marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| public List<AuditEvent> findAll() { | ||
| return List.copyOf(new ArrayList<>(events)); | ||
| } | ||
|
|
||
| public int count() { | ||
| return eventsCount.get(); | ||
| } | ||
| } | ||
|
|
||
12 changes: 12 additions & 0 deletions
12
...e/src/main/java/lt/satsyuk/distributed/audit/command/service/CommandPublishException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package lt.satsyuk.distributed.audit.command.service; | ||
|
|
||
| /** | ||
| * Raised when a command cannot be published to Kafka. | ||
| */ | ||
| public class CommandPublishException extends RuntimeException { | ||
|
|
||
| public CommandPublishException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } | ||
|
|
52 changes: 52 additions & 0 deletions
52
...e/src/main/java/lt/satsyuk/distributed/audit/command/service/UserLoginCommandService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package lt.satsyuk.distributed.audit.command.service; | ||
|
|
||
| import lt.satsyuk.distributed.audit.command.config.KafkaTopicsProperties; | ||
| import lt.satsyuk.distributed.audit.command.repository.InMemoryEventStorage; | ||
| import lt.satsyuk.distributed.audit.contracts.command.UserLoginCommand; | ||
| import lt.satsyuk.distributed.audit.contracts.dto.CommandResponse; | ||
| import lt.satsyuk.distributed.audit.event.AuditEvent; | ||
| import lt.satsyuk.distributed.audit.event.UserLoggedInEvent; | ||
| import org.springframework.kafka.core.KafkaTemplate; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.util.StringUtils; | ||
| import reactor.core.publisher.Mono; | ||
| import reactor.core.scheduler.Schedulers; | ||
|
|
||
| @Service | ||
| public class UserLoginCommandService { | ||
|
|
||
| private final KafkaTemplate<String, AuditEvent> kafkaTemplate; | ||
| private final KafkaTopicsProperties kafkaTopicsProperties; | ||
| private final InMemoryEventStorage inMemoryEventStorage; | ||
|
|
||
| public UserLoginCommandService( | ||
| KafkaTemplate<String, AuditEvent> kafkaTemplate, | ||
| KafkaTopicsProperties kafkaTopicsProperties, | ||
| InMemoryEventStorage inMemoryEventStorage | ||
| ) { | ||
| this.kafkaTemplate = kafkaTemplate; | ||
| this.kafkaTopicsProperties = kafkaTopicsProperties; | ||
| this.inMemoryEventStorage = inMemoryEventStorage; | ||
| } | ||
|
|
||
| public Mono<CommandResponse> handleUserLogin(UserLoginCommand command, String requestIp, String requestUserAgent) { | ||
| // Prefer server-derived metadata over client-supplied body values to prevent spoofing. | ||
| String effectiveIp = StringUtils.hasText(requestIp) ? requestIp : command.getIpAddress(); | ||
| String effectiveUserAgent = StringUtils.hasText(requestUserAgent) | ||
| ? requestUserAgent | ||
| : command.getUserAgent(); | ||
|
igorsatsyuk marked this conversation as resolved.
|
||
|
|
||
| UserLoggedInEvent event = UserLoggedInEvent.of(command.getUserId(), effectiveIp, effectiveUserAgent); | ||
|
|
||
| return Mono.defer(() -> Mono.fromFuture(kafkaTemplate.send( | ||
| kafkaTopicsProperties.getUserLoginEvents(), | ||
| event.getEventId(), | ||
| event | ||
| ))) | ||
|
igorsatsyuk marked this conversation as resolved.
igorsatsyuk marked this conversation as resolved.
|
||
| .publishOn(Schedulers.boundedElastic()) | ||
|
igorsatsyuk marked this conversation as resolved.
|
||
| .doOnNext(ignored -> inMemoryEventStorage.save(event)) | ||
| .map(ignored -> CommandResponse.accepted(event.getEventId())) | ||
| .onErrorMap(error -> new CommandPublishException("Failed to publish event to Kafka", error)); | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.