Skip to content

Commit

Permalink
refactor: make more readable dispatcher module
Browse files Browse the repository at this point in the history
  • Loading branch information
BulatRuslanovich committed Oct 16, 2024
1 parent 29d3264 commit f1592bf
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 295 deletions.
39 changes: 7 additions & 32 deletions dispatcher/src/main/java/com/bipbup/config/KafkaTopicConfig.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,34 @@
package com.bipbup.config;

import org.apache.kafka.clients.admin.AdminClientConfig;
import lombok.RequiredArgsConstructor;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.config.TopicBuilder;
import org.springframework.kafka.core.KafkaAdmin;

import java.util.HashMap;
import java.util.Map;

@Configuration
@RequiredArgsConstructor
public class KafkaTopicConfig {

@Value("${spring.kafka.topics.answer-topic}")
private String answerTopic;

@Value("${spring.kafka.topics.text-update-topic}")
private String textUpdateTopic;

@Value("${spring.kafka.topics.callback-query-update-topic}")
private String callbackQueryUpdateTopic;

@Value("${spring.kafka.topics.edit-topic}")
private String editTopic;

@Value("${spring.kafka.bootstrap-servers}")
private String server;

@Bean
public KafkaAdmin admin() {
Map<String, Object> configs = new HashMap<>();
configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, server);

return new KafkaAdmin(configs);
}
private final KafkaTopicProperties kafkaTopicProperties;

@Bean
public NewTopic answerTopic() {
return TopicBuilder.name(answerTopic).build();
return TopicBuilder.name(kafkaTopicProperties.getAnswerTopic()).build();
}

@Bean
public NewTopic textUpdateTopic() {
return TopicBuilder.name(textUpdateTopic).build();
return TopicBuilder.name(kafkaTopicProperties.getTextUpdateTopic()).build();
}

@Bean
public NewTopic callbackQueryUpdateTopic() {
return TopicBuilder.name(callbackQueryUpdateTopic).build();
return TopicBuilder.name(kafkaTopicProperties.getCallbackQueryUpdateTopic()).build();
}

@Bean
public NewTopic editTopic() {
return TopicBuilder.name(editTopic).build();
return TopicBuilder.name(kafkaTopicProperties.getEditTopic()).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.bipbup.config;


import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "topics")
public class KafkaTopicProperties {

private String answerTopic;

private String editTopic;

private String textUpdateTopic;

private String callbackQueryUpdateTopic;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.bipbup.config;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "bot")
public class TelegramBotProperties {

private String username;

private String url;

private String token;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.bipbup.controllers;

import com.bipbup.config.TelegramBotProperties;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.bots.TelegramWebhookBot;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
Expand All @@ -12,20 +12,16 @@

@Slf4j
@Component
public class MyTelegramBot extends TelegramWebhookBot {
public class HeadHunterBot extends TelegramWebhookBot {

private final UpdateProcessor updateProcessor;

@Value("${bot.uri}")
private String botUri;
private final TelegramBotProperties telegramBotProperties;

@Value("${bot.username}")
private String botUsername;

public MyTelegramBot(final UpdateProcessor updateProcessor,
final @Value("${bot.token}") String botToken) {
super(botToken);
public HeadHunterBot(UpdateProcessor updateProcessor, TelegramBotProperties telegramBotProperties) {
super(telegramBotProperties.getToken());
this.updateProcessor = updateProcessor;
this.telegramBotProperties = telegramBotProperties;
}

@PostConstruct
Expand All @@ -34,7 +30,7 @@ private void init() {

try {
var webhook = SetWebhook.builder()
.url(botUri)
.url(telegramBotProperties.getUrl())
.build();
this.setWebhook(webhook);
} catch (TelegramApiException e) {
Expand All @@ -44,7 +40,7 @@ private void init() {

@Override
public String getBotUsername() {
return botUsername;
return telegramBotProperties.getUsername();
}

@Override
Expand Down
Loading

0 comments on commit f1592bf

Please sign in to comment.