101 lines
2.3 KiB
Go
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)
|
|
}
|