2024-06-22 16:55:03 +10:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-06-22 17:34:01 +10:00
|
|
|
"bytes"
|
2024-06-22 16:55:03 +10:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2024-06-22 17:34:01 +10:00
|
|
|
"os/exec"
|
2024-06-22 16:55:03 +10:00
|
|
|
"path/filepath"
|
2024-06-22 17:34:01 +10:00
|
|
|
"time"
|
2024-06-22 16:55:03 +10:00
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
)
|
|
|
|
|
2024-06-22 17:34:01 +10:00
|
|
|
const logFilePath = "monitor.log"
|
|
|
|
const testFilePath = "test.sh"
|
|
|
|
|
2024-06-22 16:55:03 +10:00
|
|
|
func main() {
|
|
|
|
// Load the .env file
|
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
|
|
|
// Try to load the .env.example file
|
|
|
|
fmt.Println("Error loading .env file, trying .env.example")
|
|
|
|
err = godotenv.Load(".env.example")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error loading .env.example file")
|
|
|
|
}
|
|
|
|
}
|
2024-06-22 17:34:01 +10:00
|
|
|
|
|
|
|
// Start ticker
|
|
|
|
go startTicker()
|
|
|
|
|
|
|
|
// Start the server
|
2024-06-22 16:55:03 +10:00
|
|
|
http.HandleFunc("/", mainHandler)
|
|
|
|
http.HandleFunc("/assets/", assetHandler)
|
|
|
|
|
2024-06-22 17:34:01 +10:00
|
|
|
fmt.Println("Starting server on http://127.0.0.1:3000 ...")
|
2024-06-22 16:55:03 +10:00
|
|
|
log.Fatal(http.ListenAndServe(":3000", nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func mainHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Sanitize the path to prevent directory traversal attacks
|
|
|
|
relPath := r.URL.Path
|
|
|
|
if relPath == "/" {
|
|
|
|
relPath = "/index"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check method
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
getHandler(relPath, w, r)
|
|
|
|
case http.MethodPost:
|
|
|
|
postHandler(relPath, w, r)
|
|
|
|
default:
|
|
|
|
errorPage(405, w, r)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHandler(relPath string, w http.ResponseWriter, r *http.Request) {
|
|
|
|
tmplPath := filepath.Join("templates", filepath.Clean(relPath)+".html")
|
|
|
|
|
|
|
|
// Get message from Env
|
|
|
|
message := os.Getenv("MESSAGE")
|
|
|
|
|
|
|
|
// Parse the template
|
|
|
|
tmpl, err := template.ParseFiles(tmplPath)
|
|
|
|
if err != nil {
|
|
|
|
errorPage(404, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-22 17:34:01 +10:00
|
|
|
// Try to get monitor.log file
|
|
|
|
logData, err := os.ReadFile(logFilePath)
|
|
|
|
if err != nil {
|
|
|
|
logData = []byte("No log file found")
|
|
|
|
}
|
|
|
|
|
2024-06-22 16:55:03 +10:00
|
|
|
// Create a map to hold the data
|
2024-06-22 17:34:01 +10:00
|
|
|
requestData := map[string]interface{}{"envMessage": message, "logContent": string(logData)}
|
2024-06-22 16:55:03 +10:00
|
|
|
|
|
|
|
// Execute the template
|
|
|
|
err = tmpl.Execute(w, requestData)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Error rendering template", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func postHandler(relPath string, w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch relPath {
|
|
|
|
case "/index":
|
|
|
|
indexPostHandler(w, r)
|
|
|
|
default:
|
|
|
|
errorPage(405, w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexPostHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Parse the form data
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
errorPage(500, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the form data
|
|
|
|
message := r.FormValue("message")
|
|
|
|
|
|
|
|
// Print the message to the console
|
|
|
|
log.Printf("Message: %s", message)
|
|
|
|
|
|
|
|
// Create a map to hold the data
|
|
|
|
requestData := map[string]interface{}{
|
|
|
|
"message": message,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a page with message
|
|
|
|
tmpl, err := template.ParseFiles("templates/index.html")
|
|
|
|
if err != nil {
|
|
|
|
errorPage(500, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tmpl.Execute(w, requestData)
|
2024-06-22 17:34:01 +10:00
|
|
|
if err != nil {
|
|
|
|
errorPage(500, w, r)
|
|
|
|
}
|
2024-06-22 16:55:03 +10:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func errorPage(errorCode int, w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(errorCode)
|
|
|
|
tmpl, err := template.ParseFiles("templates/error.html")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error loading error page template: %v", err)
|
|
|
|
http.Error(w, "Error loading error page", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errorMsgs := map[int]string{
|
|
|
|
404: "Page not found for " + r.URL.Path,
|
|
|
|
405: "Method not allowed",
|
|
|
|
500: "Internal server error",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if errorcode isn't in the map
|
|
|
|
if _, ok := errorMsgs[errorCode]; !ok {
|
|
|
|
errorCode = 500
|
|
|
|
}
|
|
|
|
|
|
|
|
// Correcting the initialization of the errorData map
|
|
|
|
errorData := map[string]interface{}{
|
|
|
|
"short": errorCode,
|
|
|
|
"long": errorMsgs[errorCode],
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tmpl.Execute(w, errorData)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error executing error page template: %v", err)
|
|
|
|
http.Error(w, "Error rendering error page", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)))
|
|
|
|
}
|
2024-06-22 17:34:01 +10:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|