Compare commits
16 Commits
v0.0.7
...
20912ba17c
Author | SHA1 | Date | |
---|---|---|---|
![]() |
20912ba17c | ||
![]() |
dd5ebb60fd | ||
![]() |
0e8864d4da | ||
![]() |
490fae7d5b | ||
![]() |
ca9d8af13f | ||
![]() |
2537d5f1c8 | ||
![]() |
79ad631494 | ||
![]() |
a0046e1848 | ||
![]() |
d5f314dad2 | ||
![]() |
988a332249 | ||
29d91d1133 | |||
f40c54ebc1 | |||
![]() |
c47a664f1f | ||
9a1bb814ee | |||
d2a563588c | |||
![]() |
fabbb230d8 |
@@ -1,51 +0,0 @@
|
||||
name: Build Process Supervisor
|
||||
|
||||
on: [push]
|
||||
|
||||
env:
|
||||
APP_NAME: processSupervisor
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout repo
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: 'stable'
|
||||
|
||||
- name: Build for Windows amd64
|
||||
run: |
|
||||
GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o bin/${APP_NAME}-windows-amd64.exe ./...
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: ${{env.APP_NAME}}-windows-amd64.exe
|
||||
path: bin/${{env.APP_NAME}}-windows-amd64.exe
|
||||
|
||||
- name: Build for Linux amd64
|
||||
run: |
|
||||
GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o bin/${APP_NAME}-linux-amd64 ./...
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: ${{env.APP_NAME}}-linux-amd64
|
||||
path: bin/${{env.APP_NAME}}-linux-amd64
|
||||
|
||||
- name: Build for Linux ARM64
|
||||
run: |
|
||||
GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -trimpath -o bin/${APP_NAME}-linux-arm64 ./...
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: ${{env.APP_NAME}}-linux-arm64
|
||||
path: bin/${{env.APP_NAME}}-linux-arm64
|
||||
|
||||
- name: Build for Linux ARMv6 (Raspberry Pi)
|
||||
run: |
|
||||
GOOS=linux GOARCH=arm GOARM=6 go build -ldflags="-s -w" -trimpath -o bin/${APP_NAME}-linux-armv6 ./...
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: ${{env.APP_NAME}}-linux-armv6
|
||||
path: bin/${{env.APP_NAME}}-linux-armv6
|
98
.gitea/workflows/build.yaml
Normal file
98
.gitea/workflows/build.yaml
Normal file
@@ -0,0 +1,98 @@
|
||||
name: Build Process Supervisor
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- '*'
|
||||
|
||||
env:
|
||||
APP_NAME: processSupervisor
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- os: windows
|
||||
arch: amd64
|
||||
ext: .exe
|
||||
- os: linux
|
||||
arch: amd64
|
||||
ext: ""
|
||||
- os: linux
|
||||
arch: arm64
|
||||
ext: ""
|
||||
- os: linux
|
||||
arch: arm
|
||||
arm_version: 6
|
||||
ext: ""
|
||||
|
||||
steps:
|
||||
- name: Install zip
|
||||
run: |
|
||||
apt-get update && apt-get install -y zip
|
||||
|
||||
- name: Checkout repo
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Ensure latest Go is installed in /data/go
|
||||
run: |
|
||||
export GOROOT=/data/go/go
|
||||
export PATH=$GOROOT/bin:$PATH
|
||||
export GOCACHE=/data/gocache
|
||||
export GOMODCACHE=/data/gomodcache
|
||||
mkdir -p $GOCACHE $GOMODCACHE
|
||||
|
||||
if [ ! -x "$GOROOT/bin/go" ]; then
|
||||
echo "Go not found in $GOROOT, downloading latest stable..."
|
||||
|
||||
GO_VERSION=$(curl -s https://go.dev/VERSION?m=text | head -n1)
|
||||
echo "Latest version is $GO_VERSION"
|
||||
|
||||
mkdir -p /data/go
|
||||
curl -sSL "https://go.dev/dl/${GO_VERSION}.linux-amd64.tar.gz" -o /tmp/go.tar.gz
|
||||
tar -C /data/go -xzf /tmp/go.tar.gz
|
||||
else
|
||||
echo "Using cached Go from $GOROOT"
|
||||
fi
|
||||
|
||||
go version
|
||||
|
||||
- name: Download Go dependencies
|
||||
run: |
|
||||
export GOROOT=/data/go/go
|
||||
export PATH=$GOROOT/bin:$PATH
|
||||
export GOCACHE=/data/gocache
|
||||
export GOMODCACHE=/data/gomodcache
|
||||
mkdir -p $GOCACHE $GOMODCACHE
|
||||
go mod download
|
||||
|
||||
- name: Build binary
|
||||
run: |
|
||||
export GOROOT=/data/go/go
|
||||
export PATH=$GOROOT/bin:$PATH
|
||||
export GOCACHE=/data/gocache
|
||||
export GOMODCACHE=/data/gomodcache
|
||||
mkdir -p $GOCACHE $GOMODCACHE
|
||||
|
||||
OUTPUT="bin/${APP_NAME}-${{ matrix.os }}-${{ matrix.arch }}"
|
||||
if [ -n "${{ matrix.arm_version }}" ]; then
|
||||
OUTPUT="${OUTPUT}v${{ matrix.arm_version }}"
|
||||
export GOARM=${{ matrix.arm_version }}
|
||||
fi
|
||||
OUTPUT="${OUTPUT}${{ matrix.ext }}"
|
||||
echo "Building $OUTPUT"
|
||||
GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }} go build -ldflags="-s -w" -trimpath -o "$OUTPUT"
|
||||
shell: bash
|
||||
|
||||
- name: Zip artifact
|
||||
run: |
|
||||
zip bin/${{ env.APP_NAME }}-${{ matrix.os }}-${{ matrix.arch }}.zip bin/${{ env.APP_NAME }}-*
|
||||
|
||||
- name: Upload zipped artifact
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: ${{ env.APP_NAME }}-${{ matrix.os }}-${{ matrix.arch }}
|
||||
path: bin/${{ env.APP_NAME }}-${{ matrix.os }}-${{ matrix.arch }}.zip
|
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
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"net/http"
|
||||
@@ -9,15 +10,17 @@ import (
|
||||
|
||||
type HTopHandler struct {
|
||||
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,
|
||||
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",
|
||||
),
|
||||
|
@@ -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)
|
||||
}
|
||||
|
25
main.go
25
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))
|
||||
}
|
||||
|
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