Browse Source

Minor refactoring of ServerRuntime to include reference to system timer.

Matt Clark 1 year ago
parent
commit
d41377bb6f

+ 0 - 54
README.md

@@ -34,60 +34,6 @@ curl https://barcodeapi.org/api/128/abc123
 curl https://barcodeapi.org/api/qr/abc123
 ```
 
-#### Response Headers
-
-The server will add several headers related to the barcode including the type and encoded contents.
-
-```
-$ curl --head https://barcodeapi.org/api/auto/abc123
-
-X-Barcode-Type: Code128
-X-Barcode-Content: abc123
-Content-Type: image/png;charset=utf-8
-Content-Disposition: filename=abc123.png
-```
-
-#### Control Characters
-
-Barcodes will frequently contain control characters for various reasons; as they can be difficult to enter in a text field, the API server has implemented a special mechanism for allowing users to easily generate barcodes containing these characters. Using a supported barcode, the prefix `$$` will shift any character value by -64. See the below table for examples.
-
-```
-@ -> NUL
-A -> SOH
-B -> STX
-...
-```
-
-Refer to [ascii.cl](https://ascii.cl/) for a complete table.
-
-### Server Statistics
-
-The server will keep counters for all handlers and caches, these statistics are available at the `/stats/` endpoint.
-
-### Caching
-
-The server implements a basic cache for rendered images, details about the current cache state can be found at the `/cache/` endpoint.
-
-Provided the cached object has not expired and been evicted, a request for a previously rendered barcode will will be rapidly served from memory insted of being rendered; the cache will only render requests matching a certain criteria.
-
-### Sessions
-
-A simple session cache will track all user actions; this can be used to provide the user with a list of their most used barcodes; this information is available at the `/session/` endpoint.
-
-### Multi-Barcode Generation
-
-Some users will want to generate a large number of barcodes with one request - a basic JavaScript utility is provided at `/multi.html` which will generate as many images as requested then prepare the file to be printed.
-
-```
-https://barcodeapi.org/multi.html?Barcode1&Barcode2&dm/A%20Data%20Matrix&qr/And%20QR/Automatic
-```
-
-### Add Admin
-
-```
-java -cp server.jar org.barcodeapi.core.utils.AuthUtils username pa@ssw0rd >> config/authlist.conf
-```
-
 ## Third-Party
 
 BarcodeAPI.org is only made possible with the use of third-party software.

+ 10 - 13
src/main/java/org/barcodeapi/core/ServerLoader.java

@@ -1,6 +1,5 @@
 package org.barcodeapi.core;
 
-import java.util.Timer;
 import java.util.concurrent.TimeUnit;
 
 import org.barcodeapi.server.admin.CacheDumpHandler;
@@ -37,8 +36,8 @@ import org.eclipse.jetty.server.handler.HandlerCollection;
  */
 public class ServerLoader {
 
-	// Background task timer
-	private final Timer timer = new Timer();
+	// Initialize server runtime and get ID
+	private final String _ID = ServerRuntime.getRuntimeID();
 
 	// The port for the servers to bind to.
 	private int serverPort = 8080;
@@ -70,8 +69,6 @@ public class ServerLoader {
 	 */
 	public void launch() throws Exception {
 
-		Log.out(LOG.SERVER, "Initializing: " + ServerRuntime.getRuntimeID());
-
 		initApiServer();
 
 		initSystemTasks();
@@ -119,7 +116,8 @@ public class ServerLoader {
 	private void initApiServer() throws Exception {
 
 		// initialize API server
-		Log.out(LOG.SERVER, "Initializing Jetty API Server");
+		Log.out(LOG.SERVER, "Initializing: " + _ID);
+		Log.out(LOG.SERVER, "Starting Jetty API Server");
 		handlers = new HandlerCollection();
 		server = new Server(serverPort);
 		server.setHandler(handlers);
@@ -156,9 +154,8 @@ public class ServerLoader {
 	 */
 	private void initHandler(String path, Class<? extends RestHandler> clazz) throws Exception {
 
-		Log.out(LOG.SERVER, "Initializing handler: " + path);
-
 		// Instantiate the handler
+		Log.out(LOG.SERVER, "Initializing handler: " + path);
 		RestHandler handler = clazz.getConstructor().newInstance();
 
 		// Add it to the handler collection
@@ -175,27 +172,27 @@ public class ServerLoader {
 
 		// run watch-dog every 1 minute
 		WatchdogTask watchdogTask = new WatchdogTask();
-		timer.schedule(watchdogTask, 0, //
+		ServerRuntime.getSystemTimer().schedule(watchdogTask, 0, //
 				TimeUnit.MILLISECONDS.convert(15, TimeUnit.SECONDS));
 
 		// print stats to log every 5 minutes
 		StatsDumpTask statsTask = new StatsDumpTask(telemetry);
-		timer.schedule(statsTask, 0, //
+		ServerRuntime.getSystemTimer().schedule(statsTask, 0, //
 				TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES));
 
 		// cleanup sessions every 15 minutes
 		SessionCleanupTask sessionCleanup = new SessionCleanupTask();
-		timer.schedule(sessionCleanup, 0, //
+		ServerRuntime.getSystemTimer().schedule(sessionCleanup, 0, //
 				TimeUnit.MILLISECONDS.convert(15, TimeUnit.MINUTES));
 
 		// cleanup barcodes every hour
 		BarcodeCleanupTask barcodeCleanup = new BarcodeCleanupTask();
-		timer.schedule(barcodeCleanup, 0, //
+		ServerRuntime.getSystemTimer().schedule(barcodeCleanup, 0, //
 				TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS));
 
 		// rotate logs every 24h
 		LogRotateTask logRotate = new LogRotateTask();
-		timer.schedule(logRotate, Log.timeTillRotate(), //
+		ServerRuntime.getSystemTimer().schedule(logRotate, Log.timeTillRotate(), //
 				TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));
 	}
 

+ 11 - 1
src/main/java/org/barcodeapi/core/ServerRuntime.java

@@ -2,18 +2,22 @@ package org.barcodeapi.core;
 
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.util.Timer;
 import java.util.UUID;
 
 import org.barcodeapi.server.statistics.StatsCollector;
 
 public class ServerRuntime {
 
-	// System runtime identifier
+	// System runtime information
 	private static final String _RUNTIME_ID;
 	private static final long _RUNTIME_TIMESTART;
 	private static final String _RUNTIME_VERSION;
 	private static final String _RUNTIME_HOST;
 
+	// Background task timer
+	private static final Timer _SYS_TIMER;
+
 	static {
 
 		_RUNTIME_ID = UUID.randomUUID().toString();
@@ -35,6 +39,8 @@ public class ServerRuntime {
 		} catch (UnknownHostException e) {
 			throw new RuntimeException(e);
 		}
+
+		_SYS_TIMER = new Timer();
 	}
 
 	public static final String getRuntimeID() {
@@ -56,4 +62,8 @@ public class ServerRuntime {
 	public static final String getHostname() {
 		return _RUNTIME_HOST;
 	}
+
+	public static final Timer getSystemTimer() {
+		return _SYS_TIMER;
+	}
 }