This commit is contained in:
@@ -1,18 +1,33 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"processSupervisor/models"
|
||||
)
|
||||
|
||||
func HtopHandler(w http.ResponseWriter, r *http.Request) {
|
||||
table, err := models.GetTable()
|
||||
type HTopHandler struct {
|
||||
Table *models.HtopTable
|
||||
}
|
||||
|
||||
func NewHTopHandler() (*HTopHandler, error) {
|
||||
table, err := models.NewTable()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &HTopHandler{
|
||||
Table: table,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *HTopHandler) UpdateHTop(w http.ResponseWriter, r *http.Request) {
|
||||
err := h.Table.UpdateTable()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to get processes", 500)
|
||||
return
|
||||
}
|
||||
table.Sort(r)
|
||||
h.Table.Sort(r)
|
||||
|
||||
funcMap := template.FuncMap{
|
||||
"div": divide,
|
||||
@@ -23,19 +38,19 @@ func HtopHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// 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"),
|
||||
template.New("table.html").Funcs(funcMap).ParseFiles("templates/htop/table.html"),
|
||||
)
|
||||
tmpl.Execute(w, table)
|
||||
tmpl.Execute(w, h.Table)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl := template.Must(
|
||||
template.New("htop.html").Funcs(funcMap).ParseFiles(
|
||||
"templates/htop.html",
|
||||
"templates/partials/table.html",
|
||||
"templates/htop/htop.html",
|
||||
"templates/htop/table.html",
|
||||
),
|
||||
)
|
||||
tmpl.Execute(w, table)
|
||||
tmpl.Execute(w, h.Table)
|
||||
}
|
||||
|
||||
func divide(a any, b any) float64 {
|
||||
@@ -65,3 +80,21 @@ func toFloat64(v any) float64 {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// GET /taskmanager/usage
|
||||
func UsageHandler(w http.ResponseWriter, r *http.Request) {
|
||||
cpu, mem, err := models.GetSystemUsage()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to get usage", 500)
|
||||
return
|
||||
}
|
||||
|
||||
usage := struct {
|
||||
CPU float64 `json:"cpu"`
|
||||
Mem float64 `json:"mem"`
|
||||
}{
|
||||
CPU: cpu,
|
||||
Mem: mem,
|
||||
}
|
||||
json.NewEncoder(w).Encode(usage)
|
||||
}
|
||||
|
@@ -35,6 +35,6 @@ func KillHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
tmpl := template.Must(template.ParseFiles("templates/partials/table.html"))
|
||||
tmpl := template.Must(template.ParseFiles("templates/htop/table.html"))
|
||||
tmpl.Execute(w, processes)
|
||||
}
|
||||
|
17
handlers/mainPage.go
Normal file
17
handlers/mainPage.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type MainPage struct {
|
||||
}
|
||||
|
||||
func UpdateMainPage(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
tmpl := template.Must(
|
||||
template.New("index.html").ParseFiles("templates/index.html"),
|
||||
)
|
||||
tmpl.Execute(w, nil)
|
||||
}
|
Reference in New Issue
Block a user