Compare commits
3 Commits
v0.0.18
...
20912ba17c
Author | SHA1 | Date | |
---|---|---|---|
![]() |
20912ba17c | ||
![]() |
dd5ebb60fd | ||
![]() |
0e8864d4da |
@@ -31,7 +31,8 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Install zip
|
- name: Install zip
|
||||||
run: sudo apt -get update && sudo apt-get install -y zip
|
run: |
|
||||||
|
apt-get update && apt-get install -y zip
|
||||||
|
|
||||||
- name: Checkout repo
|
- name: Checkout repo
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
10
dist/supervisorTemplate.json
vendored
Normal file
10
dist/supervisorTemplate.json
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"processes":[{
|
||||||
|
"name":"Database DBM",
|
||||||
|
"executePath":"dist/test-windows-amd64.exe",
|
||||||
|
"workingDirectory":".",
|
||||||
|
"startDelay":2,
|
||||||
|
"priority":0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@@ -1,6 +1,7 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -8,16 +9,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type HTopHandler struct {
|
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()
|
table, err := models.NewTable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &HTopHandler{
|
return &HTopHandler{
|
||||||
Table: table,
|
Table: table,
|
||||||
|
templates: templates,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,14 +41,18 @@ func (h *HTopHandler) UpdateHTop(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Detect HTMX request via the HX-Request header
|
// Detect HTMX request via the HX-Request header
|
||||||
if r.Header.Get("HX-Request") == "true" {
|
if r.Header.Get("HX-Request") == "true" {
|
||||||
tmpl := template.Must(
|
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)
|
tmpl.Execute(w, h.Table)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl := template.Must(
|
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/htop.html",
|
||||||
"templates/htop/table.html",
|
"templates/htop/table.html",
|
||||||
),
|
),
|
||||||
|
@@ -1,17 +1,47 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"processSupervisor/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MainPage struct {
|
type MainPage struct {
|
||||||
|
templates *embed.FS
|
||||||
|
Supervisor *models.Supervisor
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateMainPage(w http.ResponseWriter, r *http.Request) {
|
func NewMainPage(templates *embed.FS) (*MainPage, error) {
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
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(
|
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)
|
tmpl.Execute(w, nil)
|
||||||
}
|
}
|
||||||
|
25
main.go
25
main.go
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@@ -8,24 +9,32 @@ import (
|
|||||||
"processSupervisor/handlers"
|
"processSupervisor/handlers"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed templates/*.html templates/*/*.html
|
||||||
|
var templatesFS embed.FS
|
||||||
|
|
||||||
|
//go:embed static
|
||||||
|
var staticFS embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
port := flag.Uint("port", 9400, "listenig port")
|
port := flag.Uint("port", 9400, "listening port")
|
||||||
flag.Parse()
|
htop, err := handlers.NewHTopHandler(&templatesFS)
|
||||||
|
|
||||||
htop, err := handlers.NewHTopHandler()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fs := http.FileServer(http.Dir("static"))
|
mianPage, err := handlers.NewMainPage(&templatesFS)
|
||||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))))
|
||||||
|
|
||||||
http.HandleFunc("/taskmanager/htop", htop.UpdateHTop)
|
http.HandleFunc("/taskmanager/htop", htop.UpdateHTop)
|
||||||
http.HandleFunc("/", handlers.UpdateMainPage)
|
http.HandleFunc("/", mianPage.UpdateMainPage)
|
||||||
http.HandleFunc("/taskmanager/kill", handlers.KillHandler)
|
http.HandleFunc("/taskmanager/kill", handlers.KillHandler)
|
||||||
http.HandleFunc("/taskmanager/usage", handlers.UsageHandler)
|
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))
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
|
||||||
}
|
}
|
||||||
|
57
models/supervisor.go
Normal file
57
models/supervisor.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Supervisor struct {
|
||||||
|
Processes []*SupervisorProcess `json:"processes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SupervisorProcess struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ExecutePath string `json:"executePath"`
|
||||||
|
WorkingDirectory string `json:"workingDirectory,omitempty"`
|
||||||
|
StartDelay int `json:"startDelay,omitempty"`
|
||||||
|
Priority int `json:"priority,omitempty"`
|
||||||
|
Arguments []string `json:"arguments,omitempty"`
|
||||||
|
process *exec.Cmd `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadTemplate(path string) (supervisor Supervisor, err error) {
|
||||||
|
content, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(content, &supervisor)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Supervisor) StartProcesses() error {
|
||||||
|
sort.Slice(s.Processes, func(i, j int) bool {
|
||||||
|
return s.Processes[i].Priority < s.Processes[j].Priority
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, p := range s.Processes {
|
||||||
|
go func() {
|
||||||
|
p.process = exec.Command(p.ExecutePath, p.Arguments...)
|
||||||
|
//cmd.Env = append(cmd.Env, "MY_VAR=some_value")
|
||||||
|
p.process.Dir = p.WorkingDirectory
|
||||||
|
|
||||||
|
output, err := p.process.Output()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Output:", string(output))
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
fmt.Println(3)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Reference in New Issue
Block a user