add and modify new index page
This commit is contained in:
@@ -2,8 +2,8 @@ package handlers
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"processSupervisor/models"
|
||||
@@ -15,8 +15,6 @@ type MainPage struct {
|
||||
}
|
||||
|
||||
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")
|
||||
@@ -25,7 +23,6 @@ func NewMainPage(templates *embed.FS) (*MainPage, error) {
|
||||
}
|
||||
supervisor = s
|
||||
}
|
||||
fmt.Println(11)
|
||||
|
||||
if err := supervisor.StartProcesses(); err != nil {
|
||||
return nil, err
|
||||
@@ -37,11 +34,67 @@ func NewMainPage(templates *embed.FS) (*MainPage, error) {
|
||||
}
|
||||
|
||||
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").ParseFS(
|
||||
m.templates,
|
||||
template.New("index.html").ParseFiles(
|
||||
"templates/index.html",
|
||||
),
|
||||
)
|
||||
tmpl.Execute(w, nil)
|
||||
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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user