Files
tecamino-proccessSupervisor/mainPage/mainPage.go
Adrian Zürcher 9f262c4a55
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
more abstraction and simple dashboard for processes
2025-08-06 13:57:34 +02:00

97 lines
2.0 KiB
Go

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)
}