Skip to content

Commit

Permalink
Add support /api/restart #652
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Oct 9, 2023
1 parent b252fca commit 9a2e9dd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
11 changes: 11 additions & 0 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sync"

"github.com/AlexxIT/go2rtc/internal/app"
"github.com/AlexxIT/go2rtc/pkg/shell"
"github.com/rs/zerolog"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"`
Expand Down
15 changes: 15 additions & 0 deletions pkg/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package shell

import (
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
"strings"
"syscall"
Expand Down Expand Up @@ -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())
}
2 changes: 1 addition & 1 deletion www/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 9a2e9dd

Please sign in to comment.