Add support /api/restart #652

This commit is contained in:
Alex X
2023-10-09 17:11:57 +03:00
parent b252fcaaa1
commit 9a2e9dd6d1
3 changed files with 27 additions and 1 deletions
+11
View File
@@ -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"`
+15
View File
@@ -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())
}
+1 -1
View File
@@ -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());
}