Переглянути джерело

Added endpoint /rest/info to provide 'uptime', 'appversion' and 'buildversion'; edited login webpage to provide build version

Lukas Cerny 4 роки тому
батько
коміт
db353f6c88

+ 8 - 1
pom.xml

@@ -14,6 +14,7 @@
         <resources>
             <resource>
                 <directory>./src/main/resources</directory>
+                <filtering>true</filtering>
             </resource>
         </resources>
         <testResources>
@@ -226,8 +227,12 @@
         <artifactId>mimepull</artifactId>
         <version>1.9.3</version>
     </dependency>
-    
 
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-core</artifactId>
+            <version>2.12.3</version>
+        </dependency>
 
         <dependency>
             <groupId>commons-io</groupId>
@@ -268,6 +273,8 @@
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.resourceEncoding>UTF-8</project.build.resourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+       <timestamp>${maven.build.timestamp}</timestamp>
+       <maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>
     </properties>
 
     <reporting>

+ 49 - 0
src/main/java/cz/hsrs/core/ApplicationInfo.java

@@ -0,0 +1,49 @@
+package cz.hsrs.core;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public final class ApplicationInfo {
+
+    private static final Logger logger = Logger.getLogger(ApplicationInfo.class.getSimpleName());
+
+    private static final long startAppEpoch;
+    private static final Properties properties;
+
+    static {
+        startAppEpoch = System.currentTimeMillis();
+        properties = loadVersionProperties("version.txt");
+    }
+
+    private static Properties loadVersionProperties(String fileName) {
+        ClassLoader classLoader = ApplicationInfo.class.getClassLoader();
+        InputStream inputStream = classLoader.getResourceAsStream(fileName);
+        if (inputStream != null) {
+            try {
+                Properties properties = new Properties();
+                properties.load(inputStream);
+                return properties;
+            } catch (IOException e) {
+                logger.log(Level.SEVERE, e.getMessage());
+                return new Properties();
+            }
+        } else {
+            return new Properties();
+        }
+    }
+
+    public static long uptime() {
+        return System.currentTimeMillis() - startAppEpoch;
+    }
+
+    public static String appVersion() {
+        return properties.getProperty("version", "unknown");
+    }
+
+    public static String buildVersion() {
+        return properties.getProperty("build.date", "unknown");
+    }
+}

+ 9 - 0
src/main/java/cz/hsrs/rest/SCListener.java

@@ -1,11 +1,16 @@
 package cz.hsrs.rest;
 
 import cz.hsrs.db.pool.SQLExecutor;
+import cz.hsrs.rest.provider.InfoRest;
 
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
 import java.io.FileInputStream;
 import java.util.Properties;
+import java.util.logging.Logger;
+
+import static cz.hsrs.core.ApplicationInfo.appVersion;
+import static cz.hsrs.core.ApplicationInfo.buildVersion;
 
 /**
  * 
@@ -14,8 +19,12 @@ import java.util.Properties;
  */
 public class SCListener implements ServletContextListener {
 
+    private static final Logger logger = Logger.getLogger(SCListener.class.getSimpleName());
+
     @Override
     public void contextInitialized(ServletContextEvent sce) {
+        logger.info(String.format("Running application version: %s build: %s.", appVersion(), buildVersion()));
+
         String propFile = sce.getServletContext().getRealPath("WEB-INF/database.properties");
         Properties prop = new Properties();
         try {

+ 36 - 0
src/main/java/cz/hsrs/rest/provider/InfoRest.java

@@ -0,0 +1,36 @@
+package cz.hsrs.rest.provider;
+
+import cz.hsrs.core.ApplicationInfo;
+import net.sf.json.JSONObject;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Response;
+import java.util.concurrent.TimeUnit;
+
+import static cz.hsrs.core.ApplicationInfo.appVersion;
+import static cz.hsrs.core.ApplicationInfo.buildVersion;
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+
+@Path("/info")
+public class InfoRest {
+
+    @GET @Produces(APPLICATION_JSON)
+    public Response getOverallInfo() {
+        long uptimeMillis = ApplicationInfo.uptime();
+        String uptime = String.format("%02d min %02d sec",
+                TimeUnit.MILLISECONDS.toMinutes(uptimeMillis),
+                TimeUnit.MILLISECONDS.toSeconds(uptimeMillis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(uptimeMillis))
+        );
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put("uptime", uptime);
+        jsonObject.put("appVersion", appVersion());
+        jsonObject.put("buildVersion", buildVersion());
+
+        return Response
+                .status(200)
+                .entity(jsonObject.toString())
+                .build();
+    }
+}

+ 2 - 0
src/main/resources/version.txt

@@ -0,0 +1,2 @@
+version=${pom.version}
+build.date=${timestamp}

+ 2 - 1
src/main/webapp/signin.jsp

@@ -2,6 +2,7 @@
     pageEncoding="UTF-8" %>
     <%@ page import = "cz.hsrs.servlet.security.*"%>
     <%@ page import = "cz.hsrs.db.pool.*"%>
+<%@ page import="cz.hsrs.core.ApplicationInfo" %>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
     <head>
@@ -49,7 +50,7 @@
         <div style="clear:both"></div>
         <div id="footer">
             &copy; CCSS, <a href="http://www.ccss.cz/">http://www.ccss.cz/</a>
-         version: <%=ControllerServlet.VERSION%>  build: <%=ControllerServlet.BUILD%>
+         version: <%=ApplicationInfo.appVersion()%>  build: <%=ApplicationInfo.buildVersion()%>
         </div>
     </body>
 </html>