6 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
18 changed files with 496 additions and 211 deletions

View File

@@ -30,9 +30,6 @@ jobs:
ext: "" ext: ""
steps: steps:
- name: Install zip
run: |
apt-get update && apt-get install -y zip
- name: Checkout repo - name: Checkout repo
uses: actions/checkout@v4 uses: actions/checkout@v4
@@ -86,13 +83,9 @@ jobs:
echo "Building $OUTPUT" echo "Building $OUTPUT"
GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }} go build -ldflags="-s -w" -trimpath -o "$OUTPUT" GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }} go build -ldflags="-s -w" -trimpath -o "$OUTPUT"
shell: bash shell: bash
- name: Zip artifact - name: Upload artifact
run: |
zip bin/${{ env.APP_NAME }}-${{ matrix.os }}-${{ matrix.arch }}.zip bin/${{ env.APP_NAME }}-*
- name: Upload zipped artifact
uses: actions/upload-artifact@v3 uses: actions/upload-artifact@v3
with: with:
name: ${{ env.APP_NAME }}-${{ matrix.os }}-${{ matrix.arch }} name: ${{ env.APP_NAME }}-${{ matrix.os }}-${{ matrix.arch }}
path: bin/${{ env.APP_NAME }}-${{ matrix.os }}-${{ matrix.arch }}.zip path: bin/

View File

@@ -1,10 +1,20 @@
{ {
"processes":[{ "processes":[{
"name":"Database DBM", "name":"Database DBM",
"description":"data description",
"executePath":"dist/test-windows-amd64.exe", "executePath":"dist/test-windows-amd64.exe",
"workingDirectory":".", "workingDirectory":".",
"startDelay":2, "startDelay":1000,
"priority":0 "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.

View File

@@ -1,47 +0,0 @@
package handlers
import (
"embed"
"fmt"
"html/template"
"net/http"
"os"
"processSupervisor/models"
)
type MainPage struct {
templates *embed.FS
Supervisor *models.Supervisor
}
func NewMainPage(templates *embed.FS) (*MainPage, error) {
fmt.Println(10)
fmt.Println(os.Getwd())
var supervisor models.Supervisor
if _, err := os.Stat("cfg/"); err != nil {
s, err := models.ReadTemplate("dist/supervisorTemplate.json")
if err != nil {
return nil, err
}
supervisor = s
}
fmt.Println(11)
if err := supervisor.StartProcesses(); err != nil {
return nil, err
}
return &MainPage{
templates: templates,
Supervisor: &supervisor,
}, nil
}
func (m *MainPage) UpdateMainPage(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(
template.New("index.html").ParseFS(
m.templates,
"templates/index.html",
),
)
tmpl.Execute(w, nil)
}

View File

@@ -1,26 +1,26 @@
package handlers package htop
import ( import (
"embed" "embed"
"encoding/json" "encoding/json"
"html/template" "html/template"
"net/http" "net/http"
"processSupervisor/models"
) )
//go:embed templates
var templatesFS embed.FS
type HTopHandler struct { type HTopHandler struct {
Table *models.HtopTable Table *HtopTable
templates *embed.FS
} }
func NewHTopHandler(templates *embed.FS) (*HTopHandler, error) { func NewHTopHandler() (*HTopHandler, error) {
table, err := models.NewTable() table, err := NewTable()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &HTopHandler{ return &HTopHandler{
Table: table, Table: table,
templates: templates,
}, nil }, nil
} }
@@ -42,8 +42,8 @@ func (h *HTopHandler) UpdateHTop(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("HX-Request") == "true" { if r.Header.Get("HX-Request") == "true" {
tmpl := template.Must( tmpl := template.Must(
template.New("table.html").Funcs(funcMap).ParseFS( template.New("table.html").Funcs(funcMap).ParseFS(
h.templates, templatesFS,
"templates/htop/table.html", "templates/table.html",
), ),
) )
tmpl.Execute(w, h.Table) tmpl.Execute(w, h.Table)
@@ -52,9 +52,9 @@ func (h *HTopHandler) UpdateHTop(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must( tmpl := template.Must(
template.New("htop.html").Funcs(funcMap).ParseFS( template.New("htop.html").Funcs(funcMap).ParseFS(
h.templates, templatesFS,
"templates/htop/htop.html", "templates/htop.html",
"templates/htop/table.html", "templates/table.html",
), ),
) )
tmpl.Execute(w, h.Table) tmpl.Execute(w, h.Table)
@@ -90,7 +90,7 @@ func toFloat64(v any) float64 {
// GET /taskmanager/usage // GET /taskmanager/usage
func UsageHandler(w http.ResponseWriter, r *http.Request) { func UsageHandler(w http.ResponseWriter, r *http.Request) {
cpu, mem, err := models.GetSystemUsage() cpu, mem, err := GetSystemUsage()
if err != nil { if err != nil {
http.Error(w, "Failed to get usage", 500) http.Error(w, "Failed to get usage", 500)
return return

View File

@@ -1,8 +1,9 @@
package models package htop
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"processSupervisor/htop/models"
"sort" "sort"
"strconv" "strconv"
@@ -11,13 +12,13 @@ import (
) )
type HtopTable struct { type HtopTable struct {
Processes []Process Processes []models.Process
CurrentSort string CurrentSort string
CurrentOrder string CurrentOrder string
} }
func NewTable() (*HtopTable, error) { func NewTable() (*HtopTable, error) {
processes, err := GetProcesses() processes, err := models.GetProcesses()
if err != nil { if err != nil {
return &HtopTable{}, err return &HtopTable{}, err
} }
@@ -28,7 +29,7 @@ func NewTable() (*HtopTable, error) {
func (t *HtopTable) UpdateTable() error { func (t *HtopTable) UpdateTable() error {
var err error var err error
t.Processes, err = GetProcesses() t.Processes, err = models.GetProcesses()
if err != nil { if err != nil {
return err return err
} }

View File

@@ -1,10 +1,10 @@
package handlers package htop
import ( import (
"html/template" "html/template"
"net/http" "net/http"
"os" "os"
"processSupervisor/models" "processSupervisor/htop/models"
"strconv" "strconv"
) )
@@ -35,6 +35,6 @@ func KillHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
tmpl := template.Must(template.ParseFiles("templates/htop/table.html")) tmpl := template.Must(template.ParseFiles("templates/table.html"))
tmpl.Execute(w, processes) tmpl.Execute(w, processes)
} }

View File

@@ -63,7 +63,7 @@
hx-get="/taskmanager/kill?pid={{.PID}}" hx-get="/taskmanager/kill?pid={{.PID}}"
hx-target="#process-table" hx-target="#process-table"
hx-swap="outerHTML" 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 Kill
</button> </button>
</td> </td>

31
main.go
View File

@@ -4,36 +4,45 @@ import (
"embed" "embed"
"flag" "flag"
"fmt" "fmt"
"io/fs"
"log" "log"
"net/http" "net/http"
"processSupervisor/handlers" "processSupervisor/htop"
"processSupervisor/mainPage"
) )
//go:embed templates/*.html templates/*/*.html
var templatesFS embed.FS
//go:embed static //go:embed static
var staticFS embed.FS var staticFS embed.FS
func main() { func main() {
port := flag.Uint("port", 9400, "listening port") port := flag.Uint("port", 9400, "listening port")
htop, err := handlers.NewHTopHandler(&templatesFS) cfgDir := flag.String("cfg", "./dist", "configuration directory")
htopHandler, err := htop.NewHTopHandler()
if err != nil { if err != nil {
panic(err) panic(err)
} }
mianPage, err := handlers.NewMainPage(&templatesFS) mP, err := mainPage.NewMainPage(*cfgDir)
if err != nil { if err != nil {
panic(err) panic(err)
} }
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS)))) staticSub, err := fs.Sub(staticFS, "static")
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/taskmanager/htop", htop.UpdateHTop) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticSub))))
http.HandleFunc("/", mianPage.UpdateMainPage)
http.HandleFunc("/taskmanager/kill", handlers.KillHandler) http.HandleFunc("/taskmanager/htop", htopHandler.UpdateHTop)
http.HandleFunc("/taskmanager/usage", handlers.UsageHandler) 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.Printf("Listening on http://localhost:%d\n", *port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)) 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,57 +0,0 @@
package models
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"sort"
)
type Supervisor struct {
Processes []*SupervisorProcess `json:"processes"`
}
type SupervisorProcess struct {
Name string `json:"name"`
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:"-"`
}
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 {
go func() {
p.process = exec.Command(p.ExecutePath, p.Arguments...)
//cmd.Env = append(cmd.Env, "MY_VAR=some_value")
p.process.Dir = p.WorkingDirectory
output, err := p.process.Output()
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println("Output:", string(output))
}()
}
fmt.Println(3)
return 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)
}

View File

@@ -1,62 +0,0 @@
<!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>
<style>
body {
font-family: sans-serif;
margin: 2rem;
}
.top-link {
display: block;
margin-bottom: 1rem;
font-size: 1.2rem;
}
ul#process-list {
list-style: none;
padding: 0;
}
ul#process-list li {
background: #f4f4f4;
margin: 0.5rem 0;
padding: 0.75rem;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="top-link-container">
<a href="/taskmanager/htop" class="top-link" target="_blank" rel="noopener noreferrer">
📈 Task Manager
</a>
</div>
<!-- Header -->
<h2>Future Processes</h2>
<!-- Placeholder List for Future Processes -->
<ul id="process-list">
<li>📌 Process A (placeholder)</li>
<li>📌 Process B (placeholder)</li>
<li>📌 Process C (placeholder)</li>
</ul>
<!-- Example Button for Future HTMX Action -->
<button
hx-get="/get-latest-processes"
hx-target="#process-list"
hx-swap="innerHTML">
🔄 Load Latest Processes
</button>
</body>
<style>
.top-link-container {
text-align: center;
margin-bottom: 1rem;
}
</style>
</html>