Files
Adrian Zürcher e188813adf
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
fix static bug and load main page
2025-08-05 20:09:28 +02:00

101 lines
2.3 KiB
Go

package handlers
import (
"embed"
"html/template"
"log"
"net/http"
"os"
"processSupervisor/models"
)
type MainPage struct {
templates *embed.FS
Supervisor *models.Supervisor
}
func NewMainPage(templates *embed.FS) (*MainPage, error) {
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
}
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 := template.Must(
// template.New("index.html").ParseFiles(
// "templates/index.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)
//http.Error(w, "Failed to start process", http.StatusInternalServerError)
//return
}
m.UpdateMainPage(w, r)
//w.WriteHeader(http.StatusOK)
}
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)
//http.Error(w, "Failed to stop process", http.StatusInternalServerError)
//return
}
m.UpdateMainPage(w, r)
//w.WriteHeader(http.StatusOK)
}
func (m *MainPage) RestartProcess(w http.ResponseWriter, r *http.Request) {
name := r.PostFormValue("name")
err := m.Supervisor.StopProcessByName(name)
if err != nil {
log.Println("Failed to start process", err)
//http.Error(w, "Failed to start process", http.StatusInternalServerError)
//return
}
err = m.Supervisor.StartProcessByName(name)
if err != nil {
log.Println("Failed to stop process", err)
// http.Error(w, "Failed to stop process", http.StatusInternalServerError)
// return
}
m.UpdateMainPage(w, r)
//w.WriteHeader(http.StatusOK)
}