Skip to content

Commit

Permalink
Add Platform flag in Docker Image to show JVM or NATIVE in the Web UI
Browse files Browse the repository at this point in the history
  • Loading branch information
RawSanj committed Dec 23, 2023
1 parent b68cde8 commit 639bf60
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main/docker/Dockerfile.jvm
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ COPY ../../.. ./
RUN mvn clean package --file pom.xml

FROM eclipse-temurin:17-jdk-jammy
ENV RUNTIME_PLATFORM=JVM
COPY --from=build /app/target/spring-redis-websocket-*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
1 change: 1 addition & 0 deletions src/main/docker/Dockerfile.native
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ COPY ../../.. ./
RUN ./mvnw -Pnative clean package -DskipNativeImage=false --file pom.xml

FROM ubuntu:jammy
ENV RUNTIME_PLATFORM=NATIVE
COPY --from=build /app/target/spring-redis-websocket spring-redis-websocket
CMD ["/spring-redis-websocket"]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.rawsanj.model.ChatMessage;
import com.github.rawsanj.model.Message;
import com.github.rawsanj.model.Platform;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.context.annotation.ImportRuntimeHints;
Expand All @@ -17,7 +18,8 @@ static class SerdeRuntimeHints implements RuntimeHintsRegistrar {
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.serialization()
.registerType(ChatMessage.class)
.registerType(Message.class);
.registerType(Message.class)
.registerType(Platform.class);
}
}
}
14 changes: 10 additions & 4 deletions src/main/java/com/github/rawsanj/handler/WebHttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.github.rawsanj.messaging.RedisChatMessagePublisher;
import com.github.rawsanj.model.Message;
import com.github.rawsanj.model.Platform;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
Expand All @@ -13,17 +15,21 @@
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;

@Configuration(proxyBeanMethods = false)
public class WebHttpHandler {

@Bean
public RouterFunction<ServerResponse> htmlRouter(@Value("classpath:/static/index.html") Resource html, RedisChatMessagePublisher redisChatMessagePublisher) {
return route(GET("/"), request -> ok().contentType(MediaType.TEXT_HTML).bodyValue(html))
public RouterFunction<ServerResponse> htmlRouter(@Value("classpath:/static/index.html") Resource html,
RedisChatMessagePublisher redisChatMessagePublisher, Environment environment) {
return route(GET("/"), request -> ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue(html))
.andRoute(POST("/message"), request -> request.bodyToMono(Message.class)
.flatMap(message -> redisChatMessagePublisher.publishChatMessage(message.getMessage()))
.flatMap(aLong -> ServerResponse.ok().bodyValue(new Message("Message Sent Successfully!."))));
.flatMap(aLong -> ServerResponse.ok().bodyValue(new Message("Message Sent Successfully!.")))
)
.andRoute(
GET("/platform"), request -> ServerResponse.ok().bodyValue(new Platform(environment.getProperty("RUNTIME_PLATFORM", "JVM")))
);
}

}
16 changes: 16 additions & 0 deletions src/main/java/com/github/rawsanj/model/Platform.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.rawsanj.model;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class Platform implements Serializable {
private String name;
}
22 changes: 20 additions & 2 deletions src/main/resources/static/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
$(document).ready(function () {

initPlatform()

// define selectors to avoid duplication
let $alert = $('#websocket-disconnected');
let $userConnected = $("#connect-alert");
Expand Down Expand Up @@ -132,7 +134,7 @@ $(document).ready(function () {
});

$("#send-http-message").click(function () {
var chatMessage = $chatMessage.val();
const chatMessage = $chatMessage.val();
$.ajax({
url: "/message",
type: "POST",
Expand All @@ -150,7 +152,7 @@ $(document).ready(function () {
});

$('#chat-message').keypress(function (e) {
var key = e.which;
const key = e.which;
if (key === 13) {
sendMessage();
}
Expand All @@ -173,5 +175,21 @@ $(document).ready(function () {
}
}

function initPlatform() {
$.ajax({
url: "/platform",
type: "GET",
dataType: "json",
contentType: "application/json",
success: function (response) {
console.log(response);
$("#platform").text(response.name);
},
error: function (err) {
console.log(err);
}
})
}

});

2 changes: 1 addition & 1 deletion src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<div class="col-sm-8">
<div class="card border-danger">
<div class="card-header">
<h4>Spring Reactive Redis Chat - Native</h4>
<h4>Spring Reactive Redis Chat - <span id="platform">JVM</span></h4>
</div>
<div class="card-body">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.rawsanj.model.ChatMessage;
import com.github.rawsanj.model.Message;
import com.github.rawsanj.model.Platform;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
Expand All @@ -16,5 +17,6 @@ void registerHintsTest() {
new RuntimeHintsConfig.SerdeRuntimeHints().registerHints(hints, getClass().getClassLoader());
assertThat(RuntimeHintsPredicates.serialization().onType(ChatMessage.class)).accepts(hints);
assertThat(RuntimeHintsPredicates.serialization().onType(Message.class)).accepts(hints);
assertThat(RuntimeHintsPredicates.serialization().onType(Platform.class)).accepts(hints);
}
}

0 comments on commit 639bf60

Please sign in to comment.