diff --git a/handlers/htopHandler.go b/handlers/htopHandler.go index 11d1eb7..83beff8 100644 --- a/handlers/htopHandler.go +++ b/handlers/htopHandler.go @@ -1,6 +1,7 @@ package handlers import ( + "embed" "encoding/json" "html/template" "net/http" @@ -8,16 +9,18 @@ import ( ) type HTopHandler struct { - Table *models.HtopTable + Table *models.HtopTable + templates *embed.FS } -func NewHTopHandler() (*HTopHandler, error) { +func NewHTopHandler(templates *embed.FS) (*HTopHandler, error) { table, err := models.NewTable() if err != nil { return nil, err } return &HTopHandler{ - Table: table, + Table: table, + templates: templates, }, nil } @@ -38,14 +41,18 @@ func (h *HTopHandler) UpdateHTop(w http.ResponseWriter, r *http.Request) { // Detect HTMX request via the HX-Request header if r.Header.Get("HX-Request") == "true" { tmpl := template.Must( - template.New("table.html").Funcs(funcMap).ParseFiles("templates/htop/table.html"), + template.New("table.html").Funcs(funcMap).ParseFS( + h.templates, + "templates/htop/table.html", + ), ) tmpl.Execute(w, h.Table) return } tmpl := template.Must( - template.New("htop.html").Funcs(funcMap).ParseFiles( + template.New("htop.html").Funcs(funcMap).ParseFS( + h.templates, "templates/htop/htop.html", "templates/htop/table.html", ), diff --git a/handlers/mainPage.go b/handlers/mainPage.go index 1233acd..a3ea54d 100644 --- a/handlers/mainPage.go +++ b/handlers/mainPage.go @@ -1,17 +1,47 @@ package handlers import ( + "embed" + "fmt" "html/template" "net/http" + "os" + "processSupervisor/models" ) type MainPage struct { + templates *embed.FS + Supervisor *models.Supervisor } -func UpdateMainPage(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/html; charset=utf-8") +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").ParseFiles("templates/index.html"), + template.New("index.html").ParseFS( + m.templates, + "templates/index.html", + ), ) tmpl.Execute(w, nil) } diff --git a/main.go b/main.go index 775f3c9..bae38ba 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "embed" "flag" "fmt" "log" @@ -8,24 +9,32 @@ import ( "processSupervisor/handlers" ) +//go:embed templates/*.html templates/*/*.html +var templatesFS embed.FS + +//go:embed static +var staticFS embed.FS + func main() { - port := flag.Uint("port", 9400, "listenig port") - flag.Parse() - - htop, err := handlers.NewHTopHandler() + port := flag.Uint("port", 9400, "listening port") + htop, err := handlers.NewHTopHandler(&templatesFS) if err != nil { panic(err) } - fs := http.FileServer(http.Dir("static")) - http.Handle("/static/", http.StripPrefix("/static/", fs)) + mianPage, err := handlers.NewMainPage(&templatesFS) + if err != nil { + panic(err) + } + + http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS)))) http.HandleFunc("/taskmanager/htop", htop.UpdateHTop) - http.HandleFunc("/", handlers.UpdateMainPage) + http.HandleFunc("/", mianPage.UpdateMainPage) http.HandleFunc("/taskmanager/kill", handlers.KillHandler) http.HandleFunc("/taskmanager/usage", handlers.UsageHandler) - log.Printf("Listening on http://localhost:%d/taskmanager/htop\n", *port) + log.Printf("Listening on http://localhost:%d\n", *port) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)) }