initial commit
This commit is contained in:
67
handlers/htopHandler.go
Normal file
67
handlers/htopHandler.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"processSupervisor/models"
|
||||
)
|
||||
|
||||
func HtopHandler(w http.ResponseWriter, r *http.Request) {
|
||||
table, err := models.GetTable()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to get processes", 500)
|
||||
return
|
||||
}
|
||||
table.Sort(r)
|
||||
|
||||
funcMap := template.FuncMap{
|
||||
"div": divide,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
|
||||
// Detect HTMX request via the HX-Request header
|
||||
if r.Header.Get("HX-Request") == "true" {
|
||||
tmpl := template.Must(
|
||||
template.New("table.html").Funcs(funcMap).ParseFiles("templates/partials/table.html"),
|
||||
)
|
||||
tmpl.Execute(w, table)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl := template.Must(
|
||||
template.New("htop.html").Funcs(funcMap).ParseFiles(
|
||||
"templates/htop.html",
|
||||
"templates/partials/table.html",
|
||||
),
|
||||
)
|
||||
tmpl.Execute(w, table)
|
||||
}
|
||||
|
||||
func divide(a any, b any) float64 {
|
||||
af := toFloat64(a)
|
||||
bf := toFloat64(b)
|
||||
if bf == 0 {
|
||||
return 0
|
||||
}
|
||||
return af / bf
|
||||
}
|
||||
|
||||
func toFloat64(v any) float64 {
|
||||
switch val := v.(type) {
|
||||
case int:
|
||||
return float64(val)
|
||||
case int32:
|
||||
return float64(val)
|
||||
case int64:
|
||||
return float64(val)
|
||||
case uint64:
|
||||
return float64(val)
|
||||
case float32:
|
||||
return float64(val)
|
||||
case float64:
|
||||
return val
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
40
handlers/killHandler.go
Normal file
40
handlers/killHandler.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
"processSupervisor/models"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func KillHandler(w http.ResponseWriter, r *http.Request) {
|
||||
pidStr := r.URL.Query().Get("pid")
|
||||
pid, err := strconv.Atoi(pidStr)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid PID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
proc, err := os.FindProcess(pid)
|
||||
if err != nil {
|
||||
http.Error(w, "Process not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
err = proc.Kill()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to kill process", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Return updated table partial (same as HTMX request)
|
||||
processes, err := models.GetProcesses()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to get processes", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl := template.Must(template.ParseFiles("templates/partials/table.html"))
|
||||
tmpl.Execute(w, processes)
|
||||
}
|
Reference in New Issue
Block a user