You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.5 KiB
70 lines
1.5 KiB
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"log"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"errors"
|
|
"lcthw.dev/go/ozai/config"
|
|
)
|
|
|
|
func LaunchLogger(in io.Reader, out io.Writer, err error) {
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
go func() {
|
|
if _, err := io.Copy(out, in)
|
|
|
|
err != nil { log.Printf("LOGGER: %v", err) }
|
|
}()
|
|
}
|
|
|
|
func LaunchProcess(proc *config.Process) {
|
|
for {
|
|
proc.ExecCmd = exec.Command(proc.Command, proc.Args...)
|
|
if errors.Is(proc.ExecCmd.Err, exec.ErrDot) {
|
|
proc.ExecCmd.Err = nil
|
|
}
|
|
|
|
fmt.Println("STARTING", proc.URL)
|
|
|
|
stderr, err := proc.ExecCmd.StderrPipe();
|
|
LaunchLogger(stderr, os.Stdout, err)
|
|
|
|
stdout, err := proc.ExecCmd.StdoutPipe();
|
|
LaunchLogger(stdout, os.Stdout, err)
|
|
|
|
proc.ExecCmd.Start()
|
|
fmt.Println("WAITING for", proc.URL)
|
|
|
|
fmt.Println("SENDING READY on channel")
|
|
proc.Ready<- "ready"
|
|
proc.ExecCmd.Wait()
|
|
fmt.Println("PROCESS", proc.URL, "EXITED")
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
config.Load()
|
|
|
|
fmt.Println("CONFIG:", config.Settings.ConfigPath)
|
|
fmt.Println("COMMANDs:", config.Settings.Processes)
|
|
|
|
for name, proc := range config.Settings.Processes {
|
|
fmt.Println("PROCESS:", name)
|
|
|
|
proc.Ready = make(chan string)
|
|
go LaunchProcess(&proc)
|
|
|
|
http.HandleFunc(proc.URL, func(w http.ResponseWriter, r *http.Request) {
|
|
<-proc.Ready
|
|
fmt.Println("!!!!!!!!!!!!!!!!!!! TIME TO DIE!!!!!")
|
|
err := proc.ExecCmd.Process.Kill()
|
|
if err != nil { log.Printf("killer says: %v", err) }
|
|
})
|
|
}
|
|
|
|
log.Fatal(http.ListenAndServe(":9999", nil))
|
|
}
|
|
|