Fix hostname

This commit is contained in:
Mark Nellemann 2023-03-02 14:26:42 +01:00
parent b364647134
commit 13169b8452
2 changed files with 26 additions and 16 deletions

View file

@ -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<String> msgDeque = new ArrayDeque<String>();
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));
}

View file

@ -39,7 +39,7 @@
</div>
<div class="columns mt-4">
<div class="collumn" id="pong" hx-post="/ping" hx-trigger="load, queue:last, every 5s">
<div class="collumn" id="pong" hx-get="/ping" hx-trigger="load, queue:last, every 3s">
</div>
</div>