From 5bbabd693f27f64fec23d8c79c9f75d270900e16 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Erik=20Hedenstr=C3=B6m?= <erik@hedenstroem.com>
Date: Mon, 21 Mar 2022 14:41:47 +0100
Subject: [PATCH] Added a minimal gui

---
 main.go                           | 13 ++++++++++--
 public/index.html                 | 33 +++++++++++++++++++++++++++++--
 public/style.css                  |  6 +++++-
 test/docker/dropwizard/Dockerfile |  1 +
 test/docker/java/Dockerfile       |  1 +
 test/docker/postgres/Dockerfile   |  1 +
 6 files changed, 50 insertions(+), 5 deletions(-)

diff --git a/main.go b/main.go
index 6341d5c..fcdb1c9 100644
--- a/main.go
+++ b/main.go
@@ -5,6 +5,7 @@ import (
 	"log"
 	"net/http"
 	"os"
+	"regexp"
 
 	"github.com/gofiber/fiber/v2"
 	"github.com/gofiber/fiber/v2/middleware/filesystem"
@@ -14,8 +15,13 @@ import (
 //go:embed public/*
 var public embed.FS
 
+const pattern = `^(@?\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})|([+-]\d+([.,]\d+)?[mhdy]{1}(\s[xi]\d+([.,]\d+)?)?)$`
+
 func main() {
 	_ = godotenv.Load(".env")
+	if _, err := os.Stat(os.Getenv("FAKETIME_TIMESTAMP_FILE")); os.IsNotExist(err) {
+		os.WriteFile(os.Getenv("FAKETIME_TIMESTAMP_FILE"), []byte("0d\n"), 0644)
+	}
 	app := fiber.New()
 	app.Use("/", filesystem.New(filesystem.Config{
 		Root:       http.FS(public),
@@ -27,8 +33,11 @@ func main() {
 		return c.SendFile(os.Getenv("FAKETIME_TIMESTAMP_FILE"))
 	})
 	app.Post("/api/timestamp", func(c *fiber.Ctx) error {
-		value := []byte(c.FormValue("value"))
-		return os.WriteFile(os.Getenv("FAKETIME_TIMESTAMP_FILE"), value, 0644)
+		if matched, _ := regexp.MatchString(pattern, c.FormValue("value")); matched {
+			value := []byte(c.FormValue("value"))
+			return os.WriteFile(os.Getenv("FAKETIME_TIMESTAMP_FILE"), value, 0644)
+		}
+		return fiber.NewError(fiber.StatusBadRequest, "Malformed time string")
 	})
 	log.Fatal(app.Listen(os.Getenv("HTTP_ADDR")))
 }
diff --git a/public/index.html b/public/index.html
index e2e8947..7b1f018 100644
--- a/public/index.html
+++ b/public/index.html
@@ -1,8 +1,37 @@
 <!DOCTYPE html>
 <head>
     <link href="style.css" rel="stylesheet" type="text/css">
+    <script>
+        function getTimestamp() {
+            document.getElementById("error").innerHTML = "";
+            fetch('/api/timestamp')
+                .then(response => response.text())
+                .then(value => document.getElementById("timestamp").value = value);
+        }
+        function setTimestamp(event) {
+            event.preventDefault();
+            let formData = new FormData();
+            formData.append("value", document.getElementById("timestamp").value);
+            fetch('/api/timestamp', {
+                method: 'POST',
+                body: formData
+            })
+                .then(response => response.status == 200 ? "" : response.text())
+                .then(text => document.getElementById("error").innerHTML = text);
+        }
+    </script>
 </head>
-<body>
-    <h1>{{.Title}}</h1>
+<body onload="getTimestamp()">
+    <form onsubmit="setTimestamp(event)">
+        <input id="timestamp" type="text" name="timestamp">
+        <input type="submit" value="Submit">
+        <input type="reset" value="Reset" onclick="getTimestamp()">
+    </form>
+    <p id="error"></p>
+    <pre>
+absolute: YYYY-MM-DD hh:mm:ss
+start at: @YYYY-MM-DD hh:mm:ss
+relative: (+|-)120(m,h,d,y)
+    </pre>
 </body>
 </html>
diff --git a/public/style.css b/public/style.css
index 69fad6d..333b2d0 100644
--- a/public/style.css
+++ b/public/style.css
@@ -1,3 +1,7 @@
 html {
-    background-color: red;
+    background-color: white;
+}
+
+#error {
+    color: red;
 }
diff --git a/test/docker/dropwizard/Dockerfile b/test/docker/dropwizard/Dockerfile
index 32dac7b..a351aa9 100644
--- a/test/docker/dropwizard/Dockerfile
+++ b/test/docker/dropwizard/Dockerfile
@@ -12,6 +12,7 @@ FROM openjdk:17-jdk-slim
 ENV DEBIAN_FRONTEND noninteractive
 RUN apt-get update && apt-get install libfaketime
 ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1
+ENV FAKETIME_DONT_RESET=1
 ENV FAKETIME_CACHE_DURATION=1
 ENV FAKETIME_DONT_FAKE_MONOTONIC=1
 ENV FAKETIME_TIMESTAMP_FILE=/var/lib/faketime/faketimerc
diff --git a/test/docker/java/Dockerfile b/test/docker/java/Dockerfile
index 6711f80..6189840 100644
--- a/test/docker/java/Dockerfile
+++ b/test/docker/java/Dockerfile
@@ -2,6 +2,7 @@ FROM openjdk:17-jdk-slim
 ENV DEBIAN_FRONTEND noninteractive
 RUN apt-get update && apt-get install libfaketime
 ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1
+ENV FAKETIME_DONT_RESET=1
 ENV FAKETIME_CACHE_DURATION=1
 ENV FAKETIME_DONT_FAKE_MONOTONIC=1
 ENV FAKETIME_TIMESTAMP_FILE=/var/lib/faketime/faketimerc
diff --git a/test/docker/postgres/Dockerfile b/test/docker/postgres/Dockerfile
index dcf3f88..88187c4 100644
--- a/test/docker/postgres/Dockerfile
+++ b/test/docker/postgres/Dockerfile
@@ -3,6 +3,7 @@ FROM postgres:bullseye
 ENV DEBIAN_FRONTEND noninteractive
 RUN apt-get update && apt-get install libfaketime
 ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1
+ENV FAKETIME_DONT_RESET=1
 ENV FAKETIME_CACHE_DURATION=1
 ENV FAKETIME_TIMESTAMP_FILE=/var/lib/faketime/faketimerc
 ENTRYPOINT ["/bin/sh"]
-- 
GitLab