diff --git a/main.go b/main.go index 0a3377e..4651e4e 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,22 @@ package main import ( + "bytes" "fmt" "html/template" "log" "net/http" "os" + "os/exec" "path/filepath" + "time" "github.com/joho/godotenv" ) +const logFilePath = "monitor.log" +const testFilePath = "test.sh" + func main() { // Load the .env file err := godotenv.Load() @@ -22,10 +28,15 @@ func main() { log.Fatal("Error loading .env.example file") } } + + // Start ticker + go startTicker() + + // Start the server http.HandleFunc("/", mainHandler) http.HandleFunc("/assets/", assetHandler) - fmt.Println("Starting server on :3000 ...") + fmt.Println("Starting server on http://127.0.0.1:3000 ...") log.Fatal(http.ListenAndServe(":3000", nil)) } @@ -62,8 +73,14 @@ func getHandler(relPath string, w http.ResponseWriter, r *http.Request) { return } + // Try to get monitor.log file + logData, err := os.ReadFile(logFilePath) + if err != nil { + logData = []byte("No log file found") + } + // Create a map to hold the data - requestData := map[string]interface{}{"envMessage": message} + requestData := map[string]interface{}{"envMessage": message, "logContent": string(logData)} // Execute the template err = tmpl.Execute(w, requestData) @@ -108,6 +125,9 @@ func indexPostHandler(w http.ResponseWriter, r *http.Request) { } err = tmpl.Execute(w, requestData) + if err != nil { + errorPage(500, w, r) + } } @@ -148,3 +168,30 @@ func assetHandler(w http.ResponseWriter, r *http.Request) { // Serve static files from templates/assets http.ServeFile(w, r, filepath.Join("templates", filepath.Clean(r.URL.Path))) } + +func startTicker() { + ticker := time.NewTicker(10 * time.Second) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + runBashScript() + } + } +} + +func runBashScript() { + // Log the time + log.Printf("Running script at %v", time.Now()) + + cmd := exec.Command("bash", testFilePath) + var out bytes.Buffer + cmd.Stdout = &out + cmd.Stderr = &out + err := cmd.Run() + if err != nil { + log.Printf("Error running script: %v", err) + return + } +} diff --git a/monitor.log b/monitor.log new file mode 100644 index 0000000..83ebc27 --- /dev/null +++ b/monitor.log @@ -0,0 +1,18 @@ +Sat 22 Jun 2024 17:29:46 AEST - Test +Sat 22 Jun 2024 17:30:35 AEST - Test +Sat 22 Jun 2024 17:30:50 AEST - Test +Sat 22 Jun 2024 17:31:01 AEST - Test +Sat 22 Jun 2024 17:31:14 AEST - Test +Sat 22 Jun 2024 17:31:25 AEST - Test +Sat 22 Jun 2024 17:31:38 AEST - Test +Sat 22 Jun 2024 17:31:51 AEST - Test +Sat 22 Jun 2024 17:32:04 AEST - Test +Sat 22 Jun 2024 17:32:09 AEST - kdig +tls-ca +https failed for NODE_IP=172.105.120.203 +Sat 22 Jun 2024 17:32:20 AEST - Test +Sat 22 Jun 2024 17:32:33 AEST - Test +Sat 22 Jun 2024 17:32:47 AEST - Test +Sat 22 Jun 2024 17:32:59 AEST - Test +Sat 22 Jun 2024 17:33:11 AEST - Test +Sat 22 Jun 2024 17:33:25 AEST - Test +Sat 22 Jun 2024 17:33:37 AEST - Test +Sat 22 Jun 2024 17:33:51 AEST - Test diff --git a/templates/about.html b/templates/about.html deleted file mode 100644 index 15acde2..0000000 --- a/templates/about.html +++ /dev/null @@ -1,13 +0,0 @@ - - -
- - -{{.envMessage}}
+{{.logContent}}{{end}} - {{if .message}} -
{{.message}}
- {{end}} - + +