AnalyzeController.java 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. package io.github.qifan777.knowledge.code.analyze;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import lombok.AllArgsConstructor;
  4. import lombok.SneakyThrows;
  5. import org.springframework.http.MediaType;
  6. import org.springframework.http.codec.ServerSentEvent;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import reactor.core.publisher.Flux;
  12. @RestController
  13. @RequestMapping("analyze")
  14. @AllArgsConstructor
  15. public class AnalyzeController {
  16. private final AnalyzeFunction analyzeFunction;
  17. private final ObjectMapper objectMapper;
  18. @GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
  19. public Flux<ServerSentEvent<String>> analyzeTask(@RequestParam String path) {
  20. return analyzeFunction.analyze(path)
  21. .map(content -> ServerSentEvent.builder(toJson(content))
  22. .event("message")
  23. .build());
  24. }
  25. @SneakyThrows
  26. public String toJson(AnalyzeFunction.AnalyzeResult result) {
  27. return objectMapper.writeValueAsString(result);
  28. }
  29. }