Skip to content

Commit eccefe5

Browse files
committed
Spring Application v1
1 parent 3d092a3 commit eccefe5

12 files changed

Lines changed: 285 additions & 51 deletions

File tree

heroes/pom.xml

Lines changed: 80 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,87 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
5-
<parent>
6-
<groupId>ru.mifi.practice</groupId>
7-
<artifactId>AaDS</artifactId>
8-
<version>${revision}</version>
9-
<relativePath>../pom.xml</relativePath>
10-
</parent>
11-
<artifactId>heroes</artifactId>
12-
<name>heroes</name>
13-
<description>Модуль heroes с поддержкой LongPolling и SSE.</description>
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>ru.mifi.practice</groupId>
7+
<artifactId>AaDS</artifactId>
8+
<version>${revision}</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>heroes</artifactId>
12+
<name>heroes</name>
13+
<description>Модуль heroes с поддержкой LongPolling и SSE.</description>
1414

15+
<dependencyManagement>
1516
<dependencies>
16-
<dependency>
17-
<groupId>com.google.guava</groupId>
18-
<artifactId>guava</artifactId>
19-
</dependency>
20-
<dependency>
21-
<groupId>io.projectreactor</groupId>
22-
<artifactId>reactor-core</artifactId>
23-
<version>3.7.12</version>
24-
</dependency>
25-
<dependency>
26-
<groupId>io.projectreactor.netty</groupId>
27-
<artifactId>reactor-netty-http</artifactId>
28-
<version>1.2.2</version>
29-
</dependency>
30-
<dependency>
31-
<groupId>com.fasterxml.jackson.core</groupId>
32-
<artifactId>jackson-databind</artifactId>
33-
<version>2.17.2</version>
34-
</dependency>
35-
<dependency>
36-
<groupId>com.fasterxml.jackson.datatype</groupId>
37-
<artifactId>jackson-datatype-jsr310</artifactId>
38-
<version>2.19.2</version>
39-
</dependency>
40-
<dependency>
41-
<groupId>org.slf4j</groupId>
42-
<artifactId>slf4j-api</artifactId>
43-
<version>2.0.16</version>
44-
</dependency>
45-
<dependency>
46-
<groupId>ch.qos.logback</groupId>
47-
<artifactId>logback-classic</artifactId>
48-
<version>1.5.16</version>
49-
</dependency>
50-
<dependency>
51-
<groupId>io.projectreactor</groupId>
52-
<artifactId>reactor-test</artifactId>
53-
<version>3.7.9</version>
54-
<scope>test</scope>
55-
</dependency>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-dependencies</artifactId>
20+
<version>4.0.1</version>
21+
<type>pom</type>
22+
<scope>import</scope>
23+
</dependency>
5624
</dependencies>
25+
</dependencyManagement>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-webflux</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.session</groupId>
34+
<artifactId>spring-session-core</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.google.guava</groupId>
38+
<artifactId>guava</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>io.projectreactor</groupId>
42+
<artifactId>reactor-core</artifactId>
43+
<version>3.7.12</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>io.projectreactor.netty</groupId>
47+
<artifactId>reactor-netty-http</artifactId>
48+
<version>1.2.2</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.fasterxml.jackson.core</groupId>
52+
<artifactId>jackson-databind</artifactId>
53+
<version>2.17.2</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.fasterxml.jackson.datatype</groupId>
57+
<artifactId>jackson-datatype-jsr310</artifactId>
58+
<version>2.19.2</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.slf4j</groupId>
62+
<artifactId>slf4j-api</artifactId>
63+
<version>2.0.16</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>ch.qos.logback</groupId>
67+
<artifactId>logback-classic</artifactId>
68+
<version>1.5.16</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.telegram</groupId>
72+
<artifactId>telegrambots-spring-boot-starter</artifactId>
73+
<version>6.9.7.1</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.telegram</groupId>
77+
<artifactId>telegrambots-abilities</artifactId>
78+
<version>9.2.0</version>
79+
</dependency>
80+
<dependency>
81+
<groupId>io.projectreactor</groupId>
82+
<artifactId>reactor-test</artifactId>
83+
<version>3.7.9</version>
84+
<scope>test</scope>
85+
</dependency>
86+
</dependencies>
5787
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ru.mifi.practice.voln;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Import;
6+
import ru.mifi.practice.voln.configuration.ApplicationConfiguration;
7+
import ru.mifi.practice.voln.configuration.SessionConfiguration;
8+
import ru.mifi.practice.voln.configuration.TelegramConfiguration;
9+
10+
@SuppressWarnings("PMD.UseUtilityClass")
11+
@SpringBootApplication
12+
@Import({ApplicationConfiguration.class, SessionConfiguration.class, TelegramConfiguration.class})
13+
public class Application {
14+
public static void main(String[] args) {
15+
SpringApplication.run(Application.class, args);
16+
}
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ru.mifi.practice.voln.api;
2+
3+
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
4+
import org.telegram.telegrambots.meta.api.objects.Update;
5+
6+
public interface TelegramHandler {
7+
8+
void received(TelegramLongPollingBot bot, Update update);
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ru.mifi.practice.voln.configuration;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan(basePackages = "ru.mifi.practice.voln.service")
8+
public class ApplicationConfiguration {
9+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ru.mifi.practice.voln.configuration;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.session.ReactiveMapSessionRepository;
6+
import org.springframework.session.ReactiveSessionRepository;
7+
import org.springframework.session.config.annotation.web.server.EnableSpringWebSession;
8+
9+
import java.util.concurrent.ConcurrentHashMap;
10+
11+
@Configuration
12+
@EnableSpringWebSession
13+
public class SessionConfiguration {
14+
@Bean
15+
public ReactiveSessionRepository<?> reactiveSessionRepository() {
16+
return new ReactiveMapSessionRepository(new ConcurrentHashMap<>());
17+
}
18+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ru.mifi.practice.voln.configuration;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.Import;
6+
import org.springframework.core.env.Environment;
7+
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
8+
import org.telegram.telegrambots.meta.api.objects.Update;
9+
import org.telegram.telegrambots.meta.generics.LongPollingBot;
10+
import org.telegram.telegrambots.starter.TelegramBotStarterConfiguration;
11+
import ru.mifi.practice.voln.api.TelegramHandler;
12+
13+
@Configuration
14+
@Import(TelegramBotStarterConfiguration.class)
15+
public class TelegramConfiguration {
16+
17+
@Bean
18+
public LongPollingBot heroesBot(Environment environment, TelegramHandler handler) {
19+
return new TelegramLongPollingBot(environment.getRequiredProperty("app.telegram.bot.token")) {
20+
@Override
21+
public void onUpdateReceived(Update update) {
22+
handler.received(this, update);
23+
}
24+
25+
@Override
26+
public String getBotUsername() {
27+
return environment.getProperty("app.telegram.bot.username", "");
28+
}
29+
};
30+
}
31+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package ru.mifi.practice.voln.controller;
2+
3+
import org.springframework.http.MediaType;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestHeader;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
import reactor.core.publisher.Flux;
11+
import reactor.core.publisher.Mono;
12+
import ru.mifi.practice.voln.controller.model.JoinResult;
13+
import ru.mifi.practice.voln.controller.model.PingResult;
14+
15+
import java.util.UUID;
16+
17+
@RestController
18+
@RequestMapping("/api/rooms")
19+
public class RoomController {
20+
21+
@GetMapping(path = "/{room-id}/join", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
22+
public Flux<String> stream(@RequestHeader("X-User-ID") Long userId, @PathVariable("room-id") UUID id) {
23+
return Flux.empty();
24+
}
25+
26+
@PostMapping(path = "/{room-id}/ping")
27+
public Mono<PingResult> ping(@RequestHeader("X-User-ID") Long userId, @PathVariable("room-id") UUID id) {
28+
return Mono.empty();
29+
}
30+
31+
@PostMapping(path = "/join", produces = MediaType.APPLICATION_JSON_VALUE)
32+
public Mono<JoinResult> join(@RequestHeader("X-User-ID") Long userId) {
33+
return Mono.empty();
34+
}
35+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mifi.practice.voln.controller.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import lombok.Builder;
6+
7+
import java.util.UUID;
8+
9+
@Builder(toBuilder = true)
10+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
11+
public record JoinResult(@JsonProperty("room_id") UUID roomId, @JsonProperty("retry_sec") Long retrySec) {
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ru.mifi.practice.voln.controller.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import lombok.Builder;
5+
6+
@Builder(toBuilder = true)
7+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
8+
public record PingResult() {
9+
}

heroes/src/main/java/ru/mifi/practice/voln/event/Event.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import java.time.LocalDateTime;
66

77
@Builder(toBuilder = true)
8-
public record Event(long streamId, long offset, LocalDateTime createdAt, Data data, Type type, Object source, Object target) {
8+
public record Event(long streamId, long offset, LocalDateTime createdAt, Data data, Type type, Object source,
9+
Object target) {
910

1011
public Event(long id, long offset, Data data) {
1112
this(id, offset, LocalDateTime.now(), data, Type.PUBLIC, null, null);

0 commit comments

Comments
 (0)