watch stdin, kill server is stdin is closed

This commit is contained in:
sawka 2022-10-30 13:05:53 -07:00
parent 260fc2d313
commit f35adf1da0

View File

@ -12,6 +12,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"syscall"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
@ -364,6 +365,19 @@ func test() error {
return nil return nil
} }
// watch stdin, kill server if stdin is closed
func stdinReadWatch() {
buf := make([]byte, 1024)
for {
_, err := os.Stdin.Read(buf)
if err != nil {
fmt.Printf("stdin closed/error, shutting down: %v\n", err)
time.Sleep(1 * time.Second)
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}
}
}
func main() { func main() {
if len(os.Args) >= 2 && os.Args[1] == "--test" { if len(os.Args) >= 2 && os.Args[1] == "--test" {
fmt.Printf("running test fn\n") fmt.Printf("running test fn\n")
@ -419,6 +433,7 @@ func main() {
fmt.Printf("[error] calling HUP on all running commands\n") fmt.Printf("[error] calling HUP on all running commands\n")
} }
go stdinReadWatch()
go runWebSocketServer() go runWebSocketServer()
gr := mux.NewRouter() gr := mux.NewRouter()
gr.HandleFunc("/api/ptyout", HandleGetPtyOut) gr.HandleFunc("/api/ptyout", HandleGetPtyOut)