package no.cantara.cs.health;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.URL;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Properties;

/**
 * Simple health endpoint for checking the server is running
 *
 * @author <a href="mailto:asbjornwillersrud@gmail.com">Asbjørn Willersrud</a> 30/03/2016.
 */
@Path(HealthResource.HEALTH_PATH)
public class HealthResource {
    public static final String HEALTH_PATH = "/health";
    private static final Logger log = LoggerFactory.getLogger(HealthResource.class);

    @GET
    public Response healthCheck() {
        log.trace("healthCheck");
        String response = String.format("ConfigService OK, version %s, now=%s, running since %s",
                getVersion(), Instant.now(), getRunningSince());
        return Response.ok(response).build();
    }

    private String getRunningSince() {
        long uptimeInMillis = ManagementFactory.getRuntimeMXBean().getUptime();
        return Instant.now().minus(uptimeInMillis, ChronoUnit.MILLIS).toString();
    }

    private String getVersion() {
        Properties mavenProperties = new Properties();
        String resourcePath = "/META-INF/maven/no.cantara.jau/configservice/pom.properties";
        URL mavenVersionResource = this.getClass().getResource(resourcePath);
        if (mavenVersionResource != null) {
            try {
                mavenProperties.load(mavenVersionResource.openStream());
                return mavenProperties.getProperty("version", "missing version info in " + resourcePath);
            } catch (IOException e) {
                log.warn("Problem reading version resource from classpath: ", e);
            }
        }
        return "(DEV VERSION)";
    }
}
@@ -131,16 +131,45 @@
 s.Path;%0A
+import javax.ws.rs.Produces;%0A
 import j
@@ -756,185 +756,367 @@
 s);%0A
-%0A    @GET%0A    public Response healthCheck() %7B%0A        log.trace(%22healthCheck%22);%0A        String response = String.format(%22ConfigService OK, version %25s, now=%25s, r
+    private final static String MAVEN_ARTIFACT_ID = %22configservice%22;%0A%0A    @GET%0A    @Produces(%22application/json%22)%0A    public Response healthCheck() %7B%0A        String json = %22%7B%22 +%0A                %22%5C%22service%5C%22:%5C%22%22 + MAVEN_ARTIFACT_ID%0A                + %22%5C%22,%5C%22timestamp%5C%22:%5C%22%22 + Instant.now().toString()%0A                + %22%5C%22,%5C%22runningSince%5C%22:%5C%22%22 + getR
 unning
- s
+S
 ince
- %25s%22,
+()
 %0A   
@@ -1132,55 +1132,68 @@
     
-getVersion(), Instant.now(), getRunningSince())
++ %22%5C%22,%5C%22version%5C%22:%5C%22%22 + getVersion()%0A                + %22%5C%22%7D%22
 ;%0A  
@@ -1221,16 +1221,12 @@
 .ok(
-response
+json
 ).bu

Return JSON from health endpoint