25 Commits

Author SHA1 Message Date
81ac4d4db5 remove zip and place executable
All checks were successful
Build Process Supervisor / build (amd64, .exe, windows) (push) Successful in 2m16s
Build Process Supervisor / build (amd64, , linux) (push) Successful in 2m25s
Build Process Supervisor / build (arm, 6, , linux) (push) Successful in 2m1s
Build Process Supervisor / build (arm64, , linux) (push) Successful in 1m58s
2025-08-06 22:09:31 +02:00
Adrian Zürcher
398a932bcb Merge branch 'main' of https://gitea.tecamino.com/paadi/tecamino-proccessSupervisor 2025-08-06 13:58:02 +02:00
Adrian Zürcher
9f262c4a55 more abstraction and simple dashboard for processes
All checks were successful
Build Process Supervisor / build (amd64, .exe, windows) (push) Successful in 2m24s
Build Process Supervisor / build (amd64, , linux) (push) Successful in 2m36s
Build Process Supervisor / build (arm, 6, , linux) (push) Successful in 2m19s
Build Process Supervisor / build (arm64, , linux) (push) Successful in 2m14s
2025-08-06 13:57:34 +02:00
eb4fae52ab improve zipping 2025-08-05 20:29:50 +02:00
Adrian Zürcher
e188813adf fix static bug and load main page
All checks were successful
Build Process Supervisor / build (amd64, .exe, windows) (push) Successful in 2m32s
Build Process Supervisor / build (amd64, , linux) (push) Successful in 2m41s
Build Process Supervisor / build (arm, 6, , linux) (push) Successful in 2m35s
Build Process Supervisor / build (arm64, , linux) (push) Successful in 2m31s
2025-08-05 20:09:28 +02:00
Adrian Zürcher
e1ba6e49a8 add and modify new index page 2025-08-05 18:45:19 +02:00
Adrian Zürcher
20912ba17c add first supervisor process 2025-08-05 17:17:55 +02:00
Adrian Zürcher
dd5ebb60fd move template in bin 2025-08-05 17:17:02 +02:00
Adrian Zürcher
0e8864d4da remove sudo
All checks were successful
Build Process Supervisor / build (amd64, .exe, windows) (push) Successful in 2m32s
Build Process Supervisor / build (amd64, , linux) (push) Successful in 2m45s
Build Process Supervisor / build (arm, 6, , linux) (push) Successful in 2m32s
Build Process Supervisor / build (arm64, , linux) (push) Successful in 2m34s
2025-08-05 15:50:52 +02:00
Adrian Zürcher
490fae7d5b fix install zip
Some checks failed
Build Process Supervisor / build (amd64, , linux) (push) Failing after 16s
Build Process Supervisor / build (amd64, .exe, windows) (push) Failing after 16s
Build Process Supervisor / build (arm64, , linux) (push) Has been cancelled
Build Process Supervisor / build (arm, 6, , linux) (push) Has been cancelled
2025-08-05 15:48:01 +02:00
Adrian Zürcher
ca9d8af13f add install zip
Some checks failed
Build Process Supervisor / build (amd64, , linux) (push) Failing after 1m19s
Build Process Supervisor / build (amd64, .exe, windows) (push) Failing after 1m16s
Build Process Supervisor / build (arm, 6, , linux) (push) Failing after 1m17s
Build Process Supervisor / build (arm64, , linux) (push) Has been cancelled
2025-08-05 15:44:09 +02:00
Adrian Zürcher
2537d5f1c8 second fix wrong go version
Some checks failed
Build Process Supervisor / build (amd64, .exe, windows) (push) Failing after 6m39s
Build Process Supervisor / build (amd64, , linux) (push) Failing after 6m52s
Build Process Supervisor / build (arm, 6, , linux) (push) Failing after 6m42s
Build Process Supervisor / build (arm64, , linux) (push) Failing after 6m33s
2025-08-05 15:29:28 +02:00
Adrian Zürcher
79ad631494 fix wrong go version
Some checks failed
Build Process Supervisor / build (amd64, , linux) (push) Failing after 22s
Build Process Supervisor / build (amd64, .exe, windows) (push) Failing after 23s
Build Process Supervisor / build (arm, 6, , linux) (push) Failing after 22s
Build Process Supervisor / build (arm64, , linux) (push) Failing after 22s
2025-08-05 15:23:16 +02:00
Adrian Zürcher
a0046e1848 try to cache go
Some checks failed
Build Process Supervisor / build (amd64, , linux) (push) Failing after 2m52s
Build Process Supervisor / build (amd64, .exe, windows) (push) Failing after 2m53s
Build Process Supervisor / build (arm, 6, , linux) (push) Failing after 26s
Build Process Supervisor / build (arm64, , linux) (push) Failing after 26s
2025-08-05 15:17:58 +02:00
Adrian Zürcher
d5f314dad2 fix yaml
Some checks failed
Build Process Supervisor / build (amd64, , linux) (push) Successful in 20m56s
Build Process Supervisor / build (arm, 6, , linux) (push) Has been cancelled
Build Process Supervisor / build (arm64, , linux) (push) Has been cancelled
Build Process Supervisor / build (amd64, .exe, windows) (push) Has been cancelled
2025-08-05 14:16:20 +02:00
Adrian Zürcher
988a332249 cache golang 2025-08-05 14:15:21 +02:00
29d91d1133 .gitea/workflows/build.yaml aktualisiert
Some checks failed
Build Process Supervisor / build (amd64, , linux) (push) Successful in 10m13s
Build Process Supervisor / build (arm, 6, , linux) (push) Has been cancelled
Build Process Supervisor / build (arm64, , linux) (push) Has been cancelled
Build Process Supervisor / build (amd64, .exe, windows) (push) Has been cancelled
2025-08-05 13:54:42 +02:00
f40c54ebc1 set go version and fix goarm 2025-08-05 13:53:47 +02:00
Adrian Zürcher
c47a664f1f simplify code by adding matrix
Some checks failed
Build Process Supervisor / build (amd64, , linux) (push) Failing after 4m42s
Build Process Supervisor / build (amd64, .exe, windows) (push) Failing after 4m43s
Build Process Supervisor / build (arm, 6, , linux) (push) Has been cancelled
Build Process Supervisor / build (arm64, , linux) (push) Failing after 5m12s
2025-08-05 13:43:22 +02:00
9a1bb814ee .gitea/workflows/build.yaml aktualisiert
All checks were successful
Build Process Supervisor / build (push) Successful in 13m4s
2025-08-05 13:11:24 +02:00
d2a563588c Remove dir change
Some checks failed
Build Process Supervisor / build (push) Failing after 7m14s
2025-08-05 12:38:10 +02:00
Adrian Zürcher
fabbb230d8 fix and rename build.yaml
Some checks failed
Build Process Supervisor / build (push) Failing after 4m47s
2025-08-05 12:05:21 +02:00
Adrian Zürcher
b9efeb670d first running version
Some checks failed
Build Process Supervisor / build (push) Failing after 1s
2025-08-05 12:01:59 +02:00
Adrian Zürcher
5712929f23 another try with checkout v4
Some checks failed
Build Go Multi-Platform / build (push) Failing after 7m26s
2025-08-04 17:31:43 +02:00
Adrian Zürcher
afb44caf52 another fix yaml tab
Some checks failed
Build Go Multi-Platform / build (push) Failing after 11s
2025-08-04 17:21:03 +02:00
20 changed files with 912 additions and 195 deletions

View File

@@ -1,40 +0,0 @@
name: Build Go Multi-Platform
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
run: |
git clone https://gitea.tecamino.com/paadi/tecamino-proccessSupervisor.git .
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 'stable'
- name: Build for Windows amd64
run: |
GOOS=windows GOARCH=amd64 go build -o build/myapp-windows-amd64.exe ./...
- name: Build for Linux amd64
run: |
GOOS=linux GOARCH=amd64 go build -o build/myapp-linux-amd64 ./...
- name: Build for Linux ARM64
run: |
GOOS=linux GOARCH=arm64 go build -o build/myapp-linux-arm64 ./...
- name: Build for Linux ARMv6 (Raspberry Pi)
run: |
GOOS=linux GOARCH=arm GOARM=6 go build -o build/myapp-linux-armv6 ./...
- name: List build artifacts
run: |
ls -lh build

View File

@@ -0,0 +1,91 @@
name: Build Process Supervisor
on:
push:
tags:
- '*'
env:
APP_NAME: processSupervisor
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- os: windows
arch: amd64
ext: .exe
- os: linux
arch: amd64
ext: ""
- os: linux
arch: arm64
ext: ""
- os: linux
arch: arm
arm_version: 6
ext: ""
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Ensure latest Go is installed in /data/go
run: |
export GOROOT=/data/go/go
export PATH=$GOROOT/bin:$PATH
export GOCACHE=/data/gocache
export GOMODCACHE=/data/gomodcache
mkdir -p $GOCACHE $GOMODCACHE
if [ ! -x "$GOROOT/bin/go" ]; then
echo "Go not found in $GOROOT, downloading latest stable..."
GO_VERSION=$(curl -s https://go.dev/VERSION?m=text | head -n1)
echo "Latest version is $GO_VERSION"
mkdir -p /data/go
curl -sSL "https://go.dev/dl/${GO_VERSION}.linux-amd64.tar.gz" -o /tmp/go.tar.gz
tar -C /data/go -xzf /tmp/go.tar.gz
else
echo "Using cached Go from $GOROOT"
fi
go version
- name: Download Go dependencies
run: |
export GOROOT=/data/go/go
export PATH=$GOROOT/bin:$PATH
export GOCACHE=/data/gocache
export GOMODCACHE=/data/gomodcache
mkdir -p $GOCACHE $GOMODCACHE
go mod download
- name: Build binary
run: |
export GOROOT=/data/go/go
export PATH=$GOROOT/bin:$PATH
export GOCACHE=/data/gocache
export GOMODCACHE=/data/gomodcache
mkdir -p $GOCACHE $GOMODCACHE
OUTPUT="bin/${APP_NAME}-${{ matrix.os }}-${{ matrix.arch }}"
if [ -n "${{ matrix.arm_version }}" ]; then
OUTPUT="${OUTPUT}v${{ matrix.arm_version }}"
export GOARM=${{ matrix.arm_version }}
fi
OUTPUT="${OUTPUT}${{ matrix.ext }}"
echo "Building $OUTPUT"
GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }} go build -ldflags="-s -w" -trimpath -o "$OUTPUT"
shell: bash
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: ${{ env.APP_NAME }}-${{ matrix.os }}-${{ matrix.arch }}
path: bin/

20
dist/supervisorTemplate.json vendored Normal file
View File

@@ -0,0 +1,20 @@
{
"processes":[{
"name":"Database DBM",
"description":"data description",
"executePath":"dist/test-windows-amd64.exe",
"workingDirectory":".",
"startDelay":1000,
"priority":0,
"arguments":["-sleep 5"]
},
{
"name":"Pau",
"description":"my lovely wife",
"executePath":"explorer.exe",
"workingDirectory":".",
"startDelay":1000,
"priority":2
}
]
}

BIN
dist/test-windows-amd64.exe vendored Normal file

Binary file not shown.

3
go.mod
View File

@@ -2,11 +2,12 @@ module processSupervisor
go 1.24.5
require github.com/shirou/gopsutil/v3 v3.24.5
require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect

12
go.sum
View File

@@ -1,14 +1,24 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=
github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
@@ -22,3 +32,5 @@ golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -1,67 +0,0 @@
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
}
}

107
htop/htopHandler.go Normal file
View File

@@ -0,0 +1,107 @@
package htop
import (
"embed"
"encoding/json"
"html/template"
"net/http"
)
//go:embed templates
var templatesFS embed.FS
type HTopHandler struct {
Table *HtopTable
}
func NewHTopHandler() (*HTopHandler, error) {
table, err := 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
}
h.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).ParseFS(
templatesFS,
"templates/table.html",
),
)
tmpl.Execute(w, h.Table)
return
}
tmpl := template.Must(
template.New("htop.html").Funcs(funcMap).ParseFS(
templatesFS,
"templates/htop.html",
"templates/table.html",
),
)
tmpl.Execute(w, h.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
}
}
// GET /taskmanager/usage
func UsageHandler(w http.ResponseWriter, r *http.Request) {
cpu, mem, err := 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)
}

View File

@@ -1,63 +1,39 @@
package models
package htop
import (
"fmt"
"net/http"
"processSupervisor/htop/models"
"sort"
"strconv"
ps "github.com/shirou/gopsutil/v3/process"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/mem"
)
type Process struct {
PID string
User string
Cmd string
CPU float64
Memory uint64
type HtopTable struct {
Processes []models.Process
CurrentSort string
CurrentOrder string
}
func GetProcesses() ([]Process, error) {
processes, err := ps.Processes()
func NewTable() (*HtopTable, error) {
processes, err := models.GetProcesses()
if err != nil {
return nil, err
return &HtopTable{}, err
}
return &HtopTable{
Processes: processes,
}, nil
}
var result []Process
for _, p := range processes {
pid := p.Pid
name, err := p.Name()
if err != nil {
continue
}
user, err := p.Username()
if err != nil {
user = "unknown"
}
cpu, err := p.CPUPercent()
if err != nil {
cpu = 0
}
memInfo, err := p.MemoryInfo()
if err != nil {
continue
}
result = append(result, Process{
PID: fmt.Sprintf("%d", pid),
User: user,
Cmd: name,
CPU: cpu,
Memory: memInfo.RSS,
})
func (t *HtopTable) UpdateTable() error {
var err error
t.Processes, err = models.GetProcesses()
if err != nil {
return err
}
return result, nil
return nil
}
func (t *HtopTable) Sort(r *http.Request) {
@@ -106,3 +82,21 @@ func (t *HtopTable) Sort(r *http.Request) {
}
})
}
func GetSystemUsage() (cpuPercent float64, memPercent float64, err error) {
cpuPercents, err := cpu.Percent(0, false)
if err != nil {
return 0, 0, err
}
if len(cpuPercents) == 0 {
return 0, 0, fmt.Errorf("cpu.Percent returned empty slice")
}
vmStat, err := mem.VirtualMemory()
if err != nil {
return 0, 0, err
}
return cpuPercents[0], vmStat.UsedPercent, nil
}

View File

@@ -1,10 +1,10 @@
package handlers
package htop
import (
"html/template"
"net/http"
"os"
"processSupervisor/models"
"processSupervisor/htop/models"
"strconv"
)
@@ -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/table.html"))
tmpl.Execute(w, processes)
}

58
htop/models/process.go Normal file
View File

@@ -0,0 +1,58 @@
package models
import (
"fmt"
ps "github.com/shirou/gopsutil/v3/process"
)
type Process struct {
PID string
User string
Cmd string
CPU float64
Memory uint64
}
func GetProcesses() ([]Process, error) {
processes, err := ps.Processes()
if err != nil {
return nil, err
}
var result []Process
for _, p := range processes {
pid := p.Pid
name, err := p.Name()
if err != nil {
continue
}
user, err := p.Username()
if err != nil {
user = "unknown"
}
cpu, err := p.CPUPercent()
if err != nil {
cpu = 0
}
memInfo, err := p.MemoryInfo()
if err != nil {
continue
}
result = append(result, Process{
PID: fmt.Sprintf("%d", pid),
User: user,
Cmd: name,
CPU: cpu,
Memory: memInfo.RSS,
})
}
return result, nil
}

View File

@@ -92,19 +92,104 @@
a:hover {
text-decoration: underline;
}
.usage-bar {
position: relative;
background-color: #333;
border-radius: 8px;
height: 20px;
width: 100%;
overflow: hidden;
}
.usage-fill {
height: 100%;
width: 0%;
background-color: #00e676; /* initial */
transition: width 0.5s ease, background-color 0.4s ease;
border-radius: 8px 0 0 8px;
}
.usage-text {
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
font-weight: bold;
color: #fff;
font-size: 14px;
line-height: 20px;
user-select: none;
text-shadow: 0 0 5px rgba(0,0,0,0.7);
}
</style>
</head>
<body>
<div id="system-stats" class="card" style="margin-bottom: 1.5rem;">
<div>
<label>CPU Usage</label>
<div class="usage-bar">
<div id="cpu-bar" class="usage-fill"></div>
<span id="cpu-percent" class="usage-text">0%</span>
</div>
</div>
<div style="margin-top: 1rem;">
<label>Memory Usage</label>
<div class="usage-bar">
<div id="mem-bar" class="usage-fill"></div>
<span id="mem-percent" class="usage-text">0%</span>
</div>
</div>
</div>
<h1 class="page-title">🧠 Task Manager</h1>
<div
id="htop-wrapper"
hx-get="/taskmanager/htop?sort={{.CurrentSort}}&order={{.CurrentOrder}}"
hx-trigger="load, every 1s"
hx-target="#process-table"
hx-swap="innerHTML">
<div id="process-table" class="card">
{{template "table.html" .}}
</div>
</body>
<script>
function updateUsage() {
fetch("/taskmanager/usage")
.then(res => res.json())
.then(data => {
const cpu = Math.round(data.cpu);
const mem = Math.round(data.mem);
updateBar('cpu-bar', 'cpu-percent', cpu);
updateBar('mem-bar', 'mem-percent', mem);
})
.catch(console.error);
}
function updateBar(barId, textId, percent) {
const bar = document.getElementById(barId);
const text = document.getElementById(textId);
bar.style.width = percent + '%';
text.textContent = percent + '%';
// Set interpolated color from green to red
bar.style.backgroundColor = getInterpolatedColor(percent);
}
function getInterpolatedColor(percent) {
// Clamp between 0100
percent = Math.max(0, Math.min(100, percent));
let r, g;
if (percent <= 50) {
// green (0%) to yellow (50%)
r = Math.floor(255 * (percent / 50)); // 0 → 255
g = 230; // stay green
} else {
// yellow (50%) to red (100%)
r = 255;
g = Math.floor(230 - 230 * ((percent - 50) / 50)); // 230 → 0
}
return `rgb(${r},${g},0)`;
}
setInterval(updateUsage, 5000);
updateUsage();
</script>
</html>

View File

@@ -1,39 +1,43 @@
<div id="process-table" class="card">
<div id="process-table" class="card"
hx-get="/taskmanager/htop?sort={{.CurrentSort}}&order={{.CurrentOrder}}"
hx-trigger="every 2s"
hx-target="#process-table"
hx-swap="innerHTML">
<table >
<thead>
<tr>
<th>
<a hx-get="/taskmanager/htop?sort=pid&order={{if and (eq .CurrentSort "pid") (eq .CurrentOrder "asc")}}desc{{else}}asc{{end}}"
hx-target="#process-table"
hx-swap="outerHTML">
hx-swap="innerHTML">
PID {{if eq .CurrentSort "pid"}}{{if eq .CurrentOrder "asc"}}↑{{else}}↓{{end}}{{end}}
</a>
</th>
<th>
<a hx-get="/taskmanager/htop?sort=user&order={{if and (eq .CurrentSort "user") (eq .CurrentOrder "asc")}}desc{{else}}asc{{end}}"
hx-target="#process-table"
hx-swap="outerHTML">
hx-swap="innerHTML">
User {{if eq .CurrentSort "user"}}{{if eq .CurrentOrder "asc"}}↑{{else}}↓{{end}}{{end}}
</a>
</th>
<th>
<a hx-get="/taskmanager/htop?sort=cmd&order={{if and (eq .CurrentSort "cmd") (eq .CurrentOrder "asc")}}desc{{else}}asc{{end}}"
hx-target="#process-table"
hx-swap="outerHTML">
hx-swap="innerHTML">
Command {{if eq .CurrentSort "cmd"}}{{if eq .CurrentOrder "asc"}}↑{{else}}↓{{end}}{{end}}
</a>
</th>
<th>
<a hx-get="/taskmanager/htop?sort=cpu&order={{if and (eq .CurrentSort "cpu") (eq .CurrentOrder "asc")}}desc{{else}}asc{{end}}"
hx-target="#process-table"
hx-swap="outerHTML">
hx-swap="innerHTML">
CPU %{{if eq .CurrentSort "cpu"}}{{if eq .CurrentOrder "asc"}}↑{{else}}↓{{end}}{{end}}
</a>
</th>
<th>
<a hx-get="/taskmanager/htop?sort=memory&order={{if and (eq .CurrentSort "memory") (eq .CurrentOrder "asc")}}desc{{else}}asc{{end}}"
hx-target="#process-table"
hx-swap="outerHTML">
hx-swap="innerHTML">
Memory {{if eq .CurrentSort "memory"}}{{if eq .CurrentOrder "asc"}}↑{{else}}↓{{end}}{{end}}
</a>
</th>
@@ -59,7 +63,7 @@
hx-get="/taskmanager/kill?pid={{.PID}}"
hx-target="#process-table"
hx-swap="outerHTML"
hx-confirm="Are you sure you want to kill this process?">
hx-confirm="Are you sure you want to kill {{.Cmd}} process?">
Kill
</button>
</td>
@@ -67,4 +71,4 @@
{{end}}
</tbody>
</table>
</div>
</div>

45
main.go
View File

@@ -1,18 +1,49 @@
package main
import (
"embed"
"flag"
"fmt"
"io/fs"
"log"
"net/http"
"processSupervisor/handlers"
"processSupervisor/htop"
"processSupervisor/mainPage"
)
//go:embed static
var staticFS embed.FS
func main() {
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/taskmanager/htop", handlers.HtopHandler)
http.HandleFunc("/taskmanager/kill", handlers.KillHandler)
port := flag.Uint("port", 9400, "listening port")
cfgDir := flag.String("cfg", "./dist", "configuration directory")
htopHandler, err := htop.NewHTopHandler()
if err != nil {
panic(err)
}
log.Println("Listening on http://localhost:8080/taskmanager/htop")
log.Fatal(http.ListenAndServe(":8080", nil))
mP, err := mainPage.NewMainPage(*cfgDir)
if err != nil {
panic(err)
}
staticSub, err := fs.Sub(staticFS, "static")
if err != nil {
log.Fatal(err)
}
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticSub))))
http.HandleFunc("/taskmanager/htop", htopHandler.UpdateHTop)
http.HandleFunc("/", mP.LoadMainPage)
http.HandleFunc("/start-process", mP.StartProcess)
http.HandleFunc("/stop-process", mP.StopProcess)
http.HandleFunc("/restart-process", mP.RestartProcess)
http.HandleFunc("/processes", mP.UpdateMainPage)
http.HandleFunc("/taskmanager/kill", htop.KillHandler)
http.HandleFunc("/taskmanager/usage", htop.UsageHandler)
log.Printf("Listening on http://localhost:%d\n", *port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
}

96
mainPage/mainPage.go Normal file
View File

@@ -0,0 +1,96 @@
package mainPage
import (
"embed"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"processSupervisor/supervisor"
"time"
)
//go:embed templates
var templatesFS embed.FS
type MainPage struct {
Supervisor *supervisor.Supervisor
}
func NewMainPage(cfgDir string) (*MainPage, error) {
var sv supervisor.Supervisor
file := filepath.Join(cfgDir, "supervisorTemplate.json")
if _, err := os.Stat(file); err == nil {
sv, err = supervisor.ReadTemplate(file)
if err != nil {
return nil, err
}
}
if err := sv.StartProcesses(); err != nil {
return nil, err
}
m := MainPage{
Supervisor: &sv,
}
return &m, nil
}
func (m *MainPage) LoadMainPage(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(
template.New("index.html").ParseFS(
templatesFS,
"templates/index.html",
"templates/processes.html",
),
)
tmpl.Execute(w, m)
}
func (m *MainPage) UpdateMainPage(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(
template.New("processes.html").ParseFS(
templatesFS,
"templates/processes.html",
),
)
tmpl.Execute(w, m)
}
func (m *MainPage) StartProcess(w http.ResponseWriter, r *http.Request) {
name := r.PostFormValue("name")
// This is where you trigger the .Start method
err := m.Supervisor.StartProcessByName(name)
if err != nil {
log.Println("Failed to start process", err)
}
m.UpdateMainPage(w, r)
}
func (m *MainPage) StopProcess(w http.ResponseWriter, r *http.Request) {
name := r.PostFormValue("name")
err := m.Supervisor.StopProcessByName(name)
if err != nil {
log.Println("Failed to stop process", err)
}
m.UpdateMainPage(w, r)
}
func (m *MainPage) RestartProcess(w http.ResponseWriter, r *http.Request) {
name := r.PostFormValue("name")
err := m.Supervisor.StopProcessByName(name)
if err != nil {
log.Println(err)
}
time.Sleep(time.Second)
err = m.Supervisor.StartProcessByName(name)
if err != nil {
log.Println(err)
}
m.UpdateMainPage(w, r)
}

View File

@@ -0,0 +1,56 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Process Dashboard</title>
<script src="https://unpkg.com/htmx.org@1.9.5"></script>
</head>
<body>
<ul id="process-page">
<div class="top-link-container">
<a href="/taskmanager/htop" class="top-link" target="_blank" rel="noopener noreferrer">
📈 Task Manager
</a>
</div>
<!-- Header -->
<h2>Processes</h2>
<ul id="process-list"
hx-get="/processes"
hx-trigger="every 5s"
hx-swap="innerHTML">
{{ template "processes.html" . }}
</ul>
</body>
<style>
body {
font-family: "Segoe UI", Roboto, sans-serif;
margin: 2rem;
background-color: #f7f9fb;
color: #222;
}
.top-link-container {
text-align: center;
margin-bottom: 2rem;
}
.top-link {
font-size: 1.2rem;
text-decoration: none;
color: #4a90e2;
font-weight: 500;
}
.top-link:hover {
text-decoration: underline;
}
h2 {
text-align: center;
color: #444;
margin-bottom: 1.5rem;
}
</style>
</html>

View File

@@ -0,0 +1,135 @@
{{ range .Supervisor.GetProcesses }}
<li>
<span class="process-name">📦
{{ if .Running }}
🟢 {{ .Name }}{{if ne .Description ""}} <span class="process-description">{{.Description}}</span>{{end}}
{{ else }}
🔴 {{ .Name }}{{if ne .Description ""}} <span class="process-description">{{.Description}}</span>{{end}}
{{ end }}
</span>
<div class="dropdown">
<button class="dot-button"></button>
<div class="dropdown-content">
{{ if not .Running }}
<button
hx-post="/start-process"
hx-vals='{"name": "{{ .Name }}"}'
hx-trigger="click"
hx-target="#process-list"
hx-swap="innerHTML">
▶️ Start
</button>
{{else}}
<button
hx-post="/stop-process"
hx-vals='{"name": "{{ .Name }}"}'
hx-trigger="click"
hx-target="#process-list"
hx-swap="innerHTML">
⏹️ Stop
</button>
<button
hx-post="/restart-process"
hx-vals='{"name": "{{ .Name }}"}'
hx-trigger="click"
hx-target="#process-list"
hx-swap="innerHTML">
🔁 Restart
</button>
{{ end }}
</div>
</div>
</li>
{{ end }}
<style>
ul#process-list {
list-style: none;
padding: 0;
max-width: 600px;
margin: 0 auto;
}
ul#process-list li {
background: #fff;
margin: 0.75rem 0;
padding: 1rem 1.25rem;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
display: flex;
justify-content: space-between;
align-items: center;
transition: box-shadow 0.25s ease;
}
ul#process-list li:hover {
box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08);
}
.process-name {
font-weight: 600;
font-size: 1rem;
color: #2c3e50;
}
.process-description {
font-weight: 400; /* normal or lighter */
color: #2c3e50; /* optional: same color or adjust if needed */
}
.dot-button {
background: none;
border: none;
font-size: 1.5rem;
cursor: pointer;
color: #777;
padding: 0 4px;
transition: color 0.2s;
}
.dot-button:hover {
color: #333;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
right: 0;
top: 1.8rem;
background-color: #fff;
min-width: 130px;
box-shadow: 0 6px 12px rgba(0,0,0,0.1);
border-radius: 6px;
overflow: hidden;
z-index: 100;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown-content button {
width: 100%;
padding: 10px 12px;
text-align: left;
background: none;
border: none;
cursor: pointer;
font-size: 0.95rem;
color: #333;
transition: background 0.2s, color 0.2s;
}
.dropdown-content button:hover {
background-color: #f0f0f0;
color: #000;
}
</style>
</html>

View File

@@ -1,17 +0,0 @@
package models
type HtopTable struct {
Processes []Process
CurrentSort string
CurrentOrder string
}
func GetTable() (*HtopTable, error) {
processes, err := GetProcesses()
if err != nil {
return &HtopTable{}, err
}
return &HtopTable{
Processes: processes,
}, nil
}

93
supervisor/process.go Normal file
View File

@@ -0,0 +1,93 @@
package supervisor
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"time"
)
type Process struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
ExecutePath string `json:"executePath"`
WorkingDirectory string `json:"workingDirectory,omitempty"`
StartDelay int `json:"startDelay,omitempty"`
Priority int `json:"priority,omitempty"`
Arguments []string `json:"arguments,omitempty"`
process *exec.Cmd `json:"-"`
cancel context.CancelFunc `json:"-"`
Running bool `json:"-"`
}
var Init bool
func (p *Process) Start() error {
if p.Running {
return fmt.Errorf("process %s already running", p.Name)
}
ctx, cancel := context.WithCancel(context.Background())
p.cancel = cancel
var args []string
for _, arg := range p.Arguments {
fields := strings.Fields(arg)
args = append(args, fields...)
}
cmd := exec.CommandContext(ctx, p.ExecutePath, args...)
cmd.Dir = p.WorkingDirectory
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
p.process = cmd
p.Running = true
if !Init {
time.Sleep(time.Duration(p.StartDelay) * time.Millisecond)
Init = true
}
if err := cmd.Start(); err != nil {
p.Running = false
p.process = nil
p.cancel = nil
return fmt.Errorf("failed to start process %s: %w", p.Name, err)
}
go func() {
err := cmd.Wait()
if err != nil {
} else {
p.Running = false
p.process = nil
p.cancel = nil
}
p.Running = false
}()
return nil
}
func (p *Process) Stop() error {
if !p.Running {
return fmt.Errorf("process %s is not running", p.Name)
}
// Cancel the context
p.cancel()
err := p.process.Process.Kill()
if err != nil {
return fmt.Errorf("failed to kill process %s: %w", p.Name, err)
}
p.Running = false
p.process = nil
p.cancel = nil
return nil
}

58
supervisor/supervisor.go Normal file
View File

@@ -0,0 +1,58 @@
package supervisor
import (
"encoding/json"
"fmt"
"os"
"sort"
)
type Supervisor struct {
Processes []*Process `json:"processes"`
}
func ReadTemplate(path string) (supervisor Supervisor, err error) {
content, err := os.ReadFile(path)
if err != nil {
return
}
err = json.Unmarshal(content, &supervisor)
return
}
func (s *Supervisor) StartProcesses() error {
sort.Slice(s.Processes, func(i, j int) bool {
return s.Processes[i].Priority < s.Processes[j].Priority
})
for _, p := range s.Processes {
p.Start()
}
return nil
}
func (s *Supervisor) GetProcesses() (processes []Process) {
for _, p := range s.Processes {
processes = append(processes, *p)
}
return
}
func (s *Supervisor) StartProcessByName(name string) error {
for _, p := range s.Processes {
if p.Name == name {
return p.Start()
}
}
return fmt.Errorf("process %s not found", name)
}
func (s *Supervisor) StopProcessByName(name string) error {
for _, p := range s.Processes {
if p.Name == name {
return p.Stop()
}
}
return fmt.Errorf("process %s not found", name)
}