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 io.micronaut.http.MediaType;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.Deque; import java.util.Deque;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.util.CollectionUtils; import io.micronaut.core.util.CollectionUtils;
import io.micronaut.http.HttpResponse; import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Consumes; import io.micronaut.http.annotation.Consumes;
@ -23,7 +24,6 @@ public class HelloController {
private static final Logger log = LoggerFactory.getLogger(HelloController.class); private static final Logger log = LoggerFactory.getLogger(HelloController.class);
private static long counter = 0; private static long counter = 0;
private static Deque<String> msgDeque = new ArrayDeque<String>(); private static Deque<String> msgDeque = new ArrayDeque<String>();
private static String hostname; private static String hostname;
@ -32,10 +32,17 @@ public class HelloController {
@PostConstruct @PostConstruct
void initialize() { void initialize() {
log.info("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("/") @Get("/")
@View("index") @View("index")
public HttpResponse<?> 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") @Post("/ping")
@View("pong") @View("pong")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public HttpResponse<?> ping(@Nullable String message) { public HttpResponse<?> ping(String message) {
if(message != null && !message.isEmpty()) { log.info(message);
log.info(message); counter++;
counter++; msgDeque.push(message);
msgDeque.push(message); if(msgDeque.size() > 25) {
//msgDeque.add(message); log.debug("Message deque: {}", msgDeque.size());
//messages.add(0, message); msgDeque.removeLast();
if(msgDeque.size() > 25) {
log.debug("Message deque: {}", msgDeque.size());
msgDeque.removeLast();
}
} }
return HttpResponse.ok(CollectionUtils.mapOf("hostname", hostname, "counter", counter, "messages", msgDeque)); return HttpResponse.ok(CollectionUtils.mapOf("hostname", hostname, "counter", counter, "messages", msgDeque));
} }

View file

@ -39,7 +39,7 @@
</div> </div>
<div class="columns mt-4"> <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>
</div> </div>