Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

41 lines
1.5KB

  1. using Confluent.Kafka.Admin;
  2. using Microsoft.AspNetCore.Mvc;
  3. using TelpoKafkaConsole.Service;
  4. using TelpoKafkaConsole.WebApi.Controllers.Api;
  5. using TelpoKafkaConsole.WebApi.Model.Request;
  6. // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
  7. namespace TelpoKafkaConsole.WebApi.Controllers
  8. {
  9. [Route("api/[controller]")]
  10. [ApiController]
  11. public class TopicController : ControllerBase
  12. {
  13. private readonly KafkaAdminService _servicekafkaAdmin;
  14. public TopicController(KafkaAdminService kafkaAdminService) { _servicekafkaAdmin = kafkaAdminService; }
  15. [HttpGet("{topic}")]
  16. public async Task<ApiResponse<List<TopicDescription>>> Get(string topic)
  17. {
  18. var topicInfo = await _servicekafkaAdmin.DescribeTopicsAsync(new List<string>() { topic });
  19. return ApiResponse<List<TopicDescription>>.Success(topicInfo);
  20. }
  21. [HttpPost]
  22. public async Task<ApiResponse<string>> Post([FromBody] TopicReq topic)
  23. {
  24. await _servicekafkaAdmin.CreateTopic(topic.TopicName,TimeSpan.FromDays(3), topic.NumPartitions);
  25. return ApiResponse<string>.Success($"创建 Topic {topic} 成功");
  26. }
  27. [HttpDelete("{topic}")]
  28. public async Task<ApiResponse<string>> Delete(string topic)
  29. {
  30. await _servicekafkaAdmin.DeleteTopics(new List<string>() {topic});
  31. return ApiResponse<string>.Success($"删除 Topic {topic} 成功");
  32. }
  33. }
  34. }