Skip to content

Commit 639bf60

Browse files
committed
Add Platform flag in Docker Image to show JVM or NATIVE in the Web UI
1 parent b68cde8 commit 639bf60

File tree

8 files changed

+54
-8
lines changed

8 files changed

+54
-8
lines changed

src/main/docker/Dockerfile.jvm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ COPY ../../.. ./
44
RUN mvn clean package --file pom.xml
55

66
FROM eclipse-temurin:17-jdk-jammy
7+
ENV RUNTIME_PLATFORM=JVM
78
COPY --from=build /app/target/spring-redis-websocket-*.jar app.jar
89
ENTRYPOINT ["java","-jar","/app.jar"]

src/main/docker/Dockerfile.native

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ COPY ../../.. ./
44
RUN ./mvnw -Pnative clean package -DskipNativeImage=false --file pom.xml
55

66
FROM ubuntu:jammy
7+
ENV RUNTIME_PLATFORM=NATIVE
78
COPY --from=build /app/target/spring-redis-websocket spring-redis-websocket
89
CMD ["/spring-redis-websocket"]

src/main/java/com/github/rawsanj/config/RuntimeHintsConfig.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.github.rawsanj.model.ChatMessage;
44
import com.github.rawsanj.model.Message;
5+
import com.github.rawsanj.model.Platform;
56
import org.springframework.aot.hint.RuntimeHints;
67
import org.springframework.aot.hint.RuntimeHintsRegistrar;
78
import org.springframework.context.annotation.ImportRuntimeHints;
@@ -17,7 +18,8 @@ static class SerdeRuntimeHints implements RuntimeHintsRegistrar {
1718
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
1819
hints.serialization()
1920
.registerType(ChatMessage.class)
20-
.registerType(Message.class);
21+
.registerType(Message.class)
22+
.registerType(Platform.class);
2123
}
2224
}
2325
}

src/main/java/com/github/rawsanj/handler/WebHttpHandler.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import com.github.rawsanj.messaging.RedisChatMessagePublisher;
44
import com.github.rawsanj.model.Message;
5+
import com.github.rawsanj.model.Platform;
56
import org.springframework.beans.factory.annotation.Value;
67
import org.springframework.context.annotation.Bean;
78
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.core.env.Environment;
810
import org.springframework.core.io.Resource;
911
import org.springframework.http.MediaType;
1012
import org.springframework.web.reactive.function.server.RouterFunction;
@@ -13,17 +15,21 @@
1315
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
1416
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
1517
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
16-
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
1718

1819
@Configuration(proxyBeanMethods = false)
1920
public class WebHttpHandler {
2021

2122
@Bean
22-
public RouterFunction<ServerResponse> htmlRouter(@Value("classpath:/static/index.html") Resource html, RedisChatMessagePublisher redisChatMessagePublisher) {
23-
return route(GET("/"), request -> ok().contentType(MediaType.TEXT_HTML).bodyValue(html))
23+
public RouterFunction<ServerResponse> htmlRouter(@Value("classpath:/static/index.html") Resource html,
24+
RedisChatMessagePublisher redisChatMessagePublisher, Environment environment) {
25+
return route(GET("/"), request -> ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue(html))
2426
.andRoute(POST("/message"), request -> request.bodyToMono(Message.class)
2527
.flatMap(message -> redisChatMessagePublisher.publishChatMessage(message.getMessage()))
26-
.flatMap(aLong -> ServerResponse.ok().bodyValue(new Message("Message Sent Successfully!."))));
28+
.flatMap(aLong -> ServerResponse.ok().bodyValue(new Message("Message Sent Successfully!.")))
29+
)
30+
.andRoute(
31+
GET("/platform"), request -> ServerResponse.ok().bodyValue(new Platform(environment.getProperty("RUNTIME_PLATFORM", "JVM")))
32+
);
2733
}
2834

2935
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.rawsanj.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.io.Serializable;
9+
10+
@Data
11+
@AllArgsConstructor
12+
@NoArgsConstructor
13+
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
14+
public class Platform implements Serializable {
15+
private String name;
16+
}

src/main/resources/static/app.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
$(document).ready(function () {
22

3+
initPlatform()
4+
35
// define selectors to avoid duplication
46
let $alert = $('#websocket-disconnected');
57
let $userConnected = $("#connect-alert");
@@ -132,7 +134,7 @@ $(document).ready(function () {
132134
});
133135

134136
$("#send-http-message").click(function () {
135-
var chatMessage = $chatMessage.val();
137+
const chatMessage = $chatMessage.val();
136138
$.ajax({
137139
url: "/message",
138140
type: "POST",
@@ -150,7 +152,7 @@ $(document).ready(function () {
150152
});
151153

152154
$('#chat-message').keypress(function (e) {
153-
var key = e.which;
155+
const key = e.which;
154156
if (key === 13) {
155157
sendMessage();
156158
}
@@ -173,5 +175,21 @@ $(document).ready(function () {
173175
}
174176
}
175177

178+
function initPlatform() {
179+
$.ajax({
180+
url: "/platform",
181+
type: "GET",
182+
dataType: "json",
183+
contentType: "application/json",
184+
success: function (response) {
185+
console.log(response);
186+
$("#platform").text(response.name);
187+
},
188+
error: function (err) {
189+
console.log(err);
190+
}
191+
})
192+
}
193+
176194
});
177195

src/main/resources/static/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<div class="col-sm-8">
3939
<div class="card border-danger">
4040
<div class="card-header">
41-
<h4>Spring Reactive Redis Chat - Native</h4>
41+
<h4>Spring Reactive Redis Chat - <span id="platform">JVM</span></h4>
4242
</div>
4343
<div class="card-body">
4444

src/test/java/com/github/rawsanj/config/RuntimeHintsConfigTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.github.rawsanj.model.ChatMessage;
44
import com.github.rawsanj.model.Message;
5+
import com.github.rawsanj.model.Platform;
56
import org.junit.jupiter.api.Test;
67
import org.springframework.aot.hint.RuntimeHints;
78
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
@@ -16,5 +17,6 @@ void registerHintsTest() {
1617
new RuntimeHintsConfig.SerdeRuntimeHints().registerHints(hints, getClass().getClassLoader());
1718
assertThat(RuntimeHintsPredicates.serialization().onType(ChatMessage.class)).accepts(hints);
1819
assertThat(RuntimeHintsPredicates.serialization().onType(Message.class)).accepts(hints);
20+
assertThat(RuntimeHintsPredicates.serialization().onType(Platform.class)).accepts(hints);
1921
}
2022
}

0 commit comments

Comments
 (0)