Add support /api/restart #652
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/AlexxIT/go2rtc/internal/app"
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/shell"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -48,6 +49,7 @@ func Init() {
|
|||||||
HandleFunc("api", apiHandler)
|
HandleFunc("api", apiHandler)
|
||||||
HandleFunc("api/config", configHandler)
|
HandleFunc("api/config", configHandler)
|
||||||
HandleFunc("api/exit", exitHandler)
|
HandleFunc("api/exit", exitHandler)
|
||||||
|
HandleFunc("api/restart", restartHandler)
|
||||||
|
|
||||||
// ensure we can listen without errors
|
// ensure we can listen without errors
|
||||||
var err error
|
var err error
|
||||||
@@ -222,6 +224,15 @@ func exitHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
os.Exit(code)
|
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 {
|
type Source struct {
|
||||||
ID string `json:"id,omitempty"`
|
ID string `json:"id,omitempty"`
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package shell
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
@@ -75,3 +77,16 @@ func RunUntilSignal() {
|
|||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
println("exit with signal:", (<-sigs).String())
|
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
@@ -46,7 +46,7 @@
|
|||||||
r = await fetch('api/config', {method: 'POST', body: editor.getValue()});
|
r = await fetch('api/config', {method: 'POST', body: editor.getValue()});
|
||||||
if (r.ok) {
|
if (r.ok) {
|
||||||
alert('OK');
|
alert('OK');
|
||||||
fetch('api/exit?code=100', {method: 'POST'});
|
fetch('api/restart', {method: 'POST'});
|
||||||
} else {
|
} else {
|
||||||
alert(await r.text());
|
alert(await r.text());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user