Removed dist and added cache to monitor
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
# NodeJS
|
# NodeJS
|
||||||
node_modules/
|
node_modules/
|
||||||
|
dist/
|
||||||
|
|
||||||
# Server
|
# Server
|
||||||
server
|
server
|
||||||
|
|||||||
-1
@@ -1 +0,0 @@
|
|||||||
const l=function(){const t=document.createElement("link").relList;if(t&&t.supports&&t.supports("modulepreload"))return;for(const e of document.querySelectorAll('link[rel="modulepreload"]'))r(e);new MutationObserver(e=>{for(const i of e)if(i.type==="childList")for(const n of i.addedNodes)n.tagName==="LINK"&&n.rel==="modulepreload"&&r(n)}).observe(document,{childList:!0,subtree:!0});function s(e){const i={};return e.integrity&&(i.integrity=e.integrity),e.referrerpolicy&&(i.referrerPolicy=e.referrerpolicy),e.crossorigin==="use-credentials"?i.credentials="include":e.crossorigin==="anonymous"?i.credentials="omit":i.credentials="same-origin",i}function r(e){if(e.ep)return;e.ep=!0;const i=s(e);fetch(e.href,i)}};l();async function f(){(await fetch("/api/status")).ok&&console.log("Server online")}f();
|
|
||||||
-1
@@ -1 +0,0 @@
|
|||||||
*,*:before,*:after{box-sizing:border-box}body{margin:0;width:100%;min-height:100vh;font-family:Inter,Segoe UI,Helvetica,Arial,sans-serif;font-size:16px}h1{margin:0;font-size:56.32212978px;line-height:1.5}h2{margin:0;font-size:42.347466px;line-height:1.5}h3{margin:0;font-size:31.8402px;line-height:1.5}h4{margin:0;font-size:23.94px;line-height:1.5}h5{margin:0;font-size:18px;line-height:1.5}
|
|
||||||
Vendored
-18
@@ -1,18 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>Homepage</title>
|
|
||||||
|
|
||||||
|
|
||||||
<script type="module" crossorigin src="/assets/index.37324337.js"></script>
|
|
||||||
<link rel="stylesheet" href="/assets/index.9acd5e50.css">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>Homepage</h1>
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { toChildArray } from 'preact'
|
import { createContext, toChildArray } from 'preact'
|
||||||
import { useCallback, useEffect, useRef, useState } from 'preact/hooks'
|
import { useCallback, useContext, useEffect, useRef, useState } from 'preact/hooks'
|
||||||
|
|
||||||
export function hashCode(s) {
|
export function hashCode(s) {
|
||||||
s = s.toString() + "seed iniziale dell'hash"
|
s = s.toString() + "seed iniziale dell'hash"
|
||||||
|
|||||||
+29
-7
@@ -3,6 +3,7 @@ package config
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
@@ -13,19 +14,37 @@ var (
|
|||||||
BaseURL string
|
BaseURL string
|
||||||
|
|
||||||
AdminPassword string
|
AdminPassword string
|
||||||
|
|
||||||
|
MonitorScriptsDir string
|
||||||
|
MonitorCacheTimeout time.Duration
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadEnv(key string, defaultValue ...string) string {
|
func loadEnv(key string, defaultValue ...string) string {
|
||||||
env := os.Getenv(key)
|
env, present := os.LookupEnv(key)
|
||||||
|
|
||||||
if len(defaultValue) > 0 && env == "" {
|
if len(defaultValue) > 0 && !present {
|
||||||
env = defaultValue[0]
|
env = defaultValue[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Environment variable %s = %q", key, env)
|
log.Printf("Env variable %s = %q", key, env)
|
||||||
return env
|
return env
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseEnv[T any](key, defaultValue string, parseFunc func(string) (T, error)) T {
|
||||||
|
env := loadEnv(key, defaultValue)
|
||||||
|
|
||||||
|
v, err := parseFunc(env)
|
||||||
|
if err != nil {
|
||||||
|
v, err = parseFunc(defaultValue)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Unable to parse default value %q to type %T", defaultValue, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf(`Env variable %s parsed from %q to %T of value %v`, key, env, v, v)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Setup logger
|
// Setup logger
|
||||||
log.SetFlags(log.Lshortfile | log.Ltime | log.Ldate)
|
log.SetFlags(log.Lshortfile | log.Ltime | log.Ldate)
|
||||||
@@ -33,9 +52,12 @@ func init() {
|
|||||||
// Load Config
|
// Load Config
|
||||||
godotenv.Load()
|
godotenv.Load()
|
||||||
|
|
||||||
Mode = loadEnv(os.Getenv("MODE"), "development")
|
Mode = loadEnv("MODE", "development")
|
||||||
Host = loadEnv(os.Getenv("HOST"), ":4000")
|
Host = loadEnv("HOST", ":4000")
|
||||||
BaseURL = loadEnv(os.Getenv("HOST"), "http://localhost:4000")
|
BaseURL = loadEnv("HOST", "http://localhost:4000")
|
||||||
|
|
||||||
AdminPassword = loadEnv(os.Getenv("ADMIN_PASSWORD"), "secret")
|
AdminPassword = loadEnv("ADMIN_PASSWORD", "secret")
|
||||||
|
|
||||||
|
MonitorScriptsDir = loadEnv("MONITOR_SCRIPTS_DIR", "./scripts")
|
||||||
|
MonitorCacheTimeout = parseEnv("MONITOR_CACHE_TIMEOUT", "5s", time.ParseDuration)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ func main() {
|
|||||||
|
|
||||||
monitorService := monitor.NewService(&monitor.Config{
|
monitorService := monitor.NewService(&monitor.Config{
|
||||||
ScriptsDir: "./scripts",
|
ScriptsDir: "./scripts",
|
||||||
|
CacheTimeout: config.MonitorCacheTimeout,
|
||||||
})
|
})
|
||||||
|
|
||||||
if err := monitorService.LoadScripts(); err != nil {
|
if err := monitorService.LoadScripts(); err != nil {
|
||||||
|
|||||||
+31
-10
@@ -1,27 +1,38 @@
|
|||||||
package monitor
|
package monitor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
ScriptsDir string `json:"scriptsDir"`
|
ScriptsDir string
|
||||||
|
CacheTimeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
type cacheEntry struct {
|
||||||
|
creationTime time.Time
|
||||||
|
|
||||||
|
output string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
Config *Config
|
Config *Config
|
||||||
|
|
||||||
scriptPaths map[string]string
|
scriptPaths map[string]string
|
||||||
|
scriptOutputCache map[string]cacheEntry
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewService(config *Config) *Service {
|
func NewService(config *Config) *Service {
|
||||||
return &Service{
|
return &Service{
|
||||||
Config: config,
|
Config: config,
|
||||||
|
|
||||||
|
scriptPaths: map[string]string{},
|
||||||
|
scriptOutputCache: map[string]cacheEntry{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,21 +52,31 @@ func (s *Service) LoadScripts() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add caching
|
func (s *Service) GetOutput(command string) (string, error) {
|
||||||
func (s *Service) GetLastOutput(command string) (string, error) {
|
// check if output is present in cache
|
||||||
|
if entry, found := s.scriptOutputCache[command]; found {
|
||||||
|
// check if cached value is expired
|
||||||
|
if time.Now().Before(entry.creationTime.Add(s.Config.CacheTimeout)) {
|
||||||
|
log.Printf("Retrieving output of %q from cache", command)
|
||||||
|
return entry.output, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
args := strings.Fields(command)
|
args := strings.Fields(command)
|
||||||
|
|
||||||
log.Printf("Running script %q with args %v", args[0], args[1:])
|
log.Printf("Running script %q with args %v", args[0], args[1:])
|
||||||
|
|
||||||
scriptPath := s.scriptPaths[args[0]]
|
scriptPath := s.scriptPaths[args[0]]
|
||||||
|
|
||||||
cmd := exec.Command(scriptPath, args[1:]...)
|
bytes, err := exec.Command(scriptPath, args[1:]...).Output()
|
||||||
var b bytes.Buffer
|
if err != nil {
|
||||||
cmd.Stdout = &b
|
|
||||||
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return b.String(), nil
|
output := string(bytes)
|
||||||
|
|
||||||
|
log.Printf("Caching output of %q", command)
|
||||||
|
s.scriptOutputCache[command] = cacheEntry{time.Now(), output}
|
||||||
|
|
||||||
|
return output, nil
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-2
@@ -11,8 +11,7 @@ func (r *Router) ApiMonitor(api fiber.Router) {
|
|||||||
// - "/api/monitor/status?script=SCRIPT_NAME" where SCRIPT_NAME is the name of a file inside "./scripts"
|
// - "/api/monitor/status?script=SCRIPT_NAME" where SCRIPT_NAME is the name of a file inside "./scripts"
|
||||||
api.Get("/status", func(c *fiber.Ctx) error {
|
api.Get("/status", func(c *fiber.Ctx) error {
|
||||||
if qScript := c.Query("script"); qScript != "" {
|
if qScript := c.Query("script"); qScript != "" {
|
||||||
|
output, err := r.Monitor.GetOutput(qScript)
|
||||||
output, err := r.Monitor.GetLastOutput(qScript)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user