From 9a2e9dd6d11756813fb1b93ac917440a4d2e761b Mon Sep 17 00:00:00 2001 From: Alex X Date: Mon, 9 Oct 2023 17:11:57 +0300 Subject: [PATCH] Add support /api/restart #652 --- internal/api/api.go | 11 +++++++++++ pkg/shell/shell.go | 15 +++++++++++++++ www/editor.html | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/internal/api/api.go b/internal/api/api.go index 02fb9ab8..814aedd8 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -12,6 +12,7 @@ import ( "sync" "github.com/AlexxIT/go2rtc/internal/app" + "github.com/AlexxIT/go2rtc/pkg/shell" "github.com/rs/zerolog" ) @@ -48,6 +49,7 @@ func Init() { HandleFunc("api", apiHandler) HandleFunc("api/config", configHandler) HandleFunc("api/exit", exitHandler) + HandleFunc("api/restart", restartHandler) // ensure we can listen without errors var err error @@ -222,6 +224,15 @@ func exitHandler(w http.ResponseWriter, r *http.Request) { os.Exit(code) } +func restartHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + http.Error(w, "", http.StatusBadRequest) + return + } + + go shell.Restart() +} + type Source struct { ID string `json:"id,omitempty"` Name string `json:"name,omitempty"` diff --git a/pkg/shell/shell.go b/pkg/shell/shell.go index d24b880a..d9dfeb79 100644 --- a/pkg/shell/shell.go +++ b/pkg/shell/shell.go @@ -2,7 +2,9 @@ package shell import ( "os" + "os/exec" "os/signal" + "path/filepath" "regexp" "strings" "syscall" @@ -75,3 +77,16 @@ func RunUntilSignal() { signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) println("exit with signal:", (<-sigs).String()) } + +func Restart() { + path, err := exec.LookPath(os.Args[0]) + if err != nil { + return + } + path, err = filepath.Abs(path) + if err != nil { + return + } + path = filepath.Clean(path) + _ = syscall.Exec(path, os.Args, os.Environ()) +} diff --git a/www/editor.html b/www/editor.html index 58943c66..c24e7f95 100644 --- a/www/editor.html +++ b/www/editor.html @@ -46,7 +46,7 @@ r = await fetch('api/config', {method: 'POST', body: editor.getValue()}); if (r.ok) { alert('OK'); - fetch('api/exit?code=100', {method: 'POST'}); + fetch('api/restart', {method: 'POST'}); } else { alert(await r.text()); }