From 13169b8452bd293975f5cd0fa5d2104fdec3417e Mon Sep 17 00:00:00 2001 From: Mark Nellemann Date: Thu, 2 Mar 2023 14:26:42 +0100 Subject: [PATCH] Fix hostname --- src/main/java/hello/HelloController.java | 40 +++++++++++++++--------- src/main/resources/views/index.html | 2 +- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/main/java/hello/HelloController.java b/src/main/java/hello/HelloController.java index 59fe6ea..882c6fd 100644 --- a/src/main/java/hello/HelloController.java +++ b/src/main/java/hello/HelloController.java @@ -2,13 +2,14 @@ package hello; import io.micronaut.http.MediaType; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.ArrayDeque; import java.util.Deque; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import io.micronaut.core.annotation.Nullable; import io.micronaut.core.util.CollectionUtils; import io.micronaut.http.HttpResponse; import io.micronaut.http.annotation.Consumes; @@ -23,7 +24,6 @@ public class HelloController { private static final Logger log = LoggerFactory.getLogger(HelloController.class); - private static long counter = 0; private static Deque msgDeque = new ArrayDeque(); private static String hostname; @@ -32,10 +32,17 @@ public class HelloController { @PostConstruct void initialize() { log.info("initialize()"); - hostname = System.getenv("HOSTNAME"); - + try { + InetAddress inetadd = InetAddress.getLocalHost(); + hostname = inetadd.getHostName(); + } catch (UnknownHostException e) { + log.error("", e); + hostname = System.getenv("HOSTNAME"); + } + } + @Get("/") @View("index") public HttpResponse index() { @@ -50,20 +57,23 @@ public class HelloController { } + @Get("/ping") + @View("pong") + public HttpResponse ping() { + return HttpResponse.ok(CollectionUtils.mapOf("hostname", hostname, "counter", counter, "messages", msgDeque)); + } + + @Post("/ping") @View("pong") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public HttpResponse ping(@Nullable String message) { - if(message != null && !message.isEmpty()) { - log.info(message); - counter++; - msgDeque.push(message); - //msgDeque.add(message); - //messages.add(0, message); - if(msgDeque.size() > 25) { - log.debug("Message deque: {}", msgDeque.size()); - msgDeque.removeLast(); - } + public HttpResponse ping(String message) { + log.info(message); + counter++; + msgDeque.push(message); + if(msgDeque.size() > 25) { + log.debug("Message deque: {}", msgDeque.size()); + msgDeque.removeLast(); } return HttpResponse.ok(CollectionUtils.mapOf("hostname", hostname, "counter", counter, "messages", msgDeque)); } diff --git a/src/main/resources/views/index.html b/src/main/resources/views/index.html index 94522f2..ebdd5c1 100644 --- a/src/main/resources/views/index.html +++ b/src/main/resources/views/index.html @@ -39,7 +39,7 @@
-
+