Skip to content

Commit

Permalink
Simplify restart func
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 29, 2024
1 parent c7af502 commit cf4f646
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
11 changes: 9 additions & 2 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"time"

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

Expand Down Expand Up @@ -256,7 +255,15 @@ func restartHandler(w http.ResponseWriter, r *http.Request) {
return
}

go shell.Restart()
path, err := os.Executable()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

log.Debug().Msgf("[api] restart %s", path)

go syscall.Exec(path, os.Args, os.Environ())
}

func logHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
19 changes: 0 additions & 19 deletions pkg/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package shell

import (
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
"strings"
"syscall"
Expand Down Expand Up @@ -70,20 +68,3 @@ func RunUntilSignal() {
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
println("exit with signal:", (<-sigs).String())
}

// Restart idea taken from https://github.com/tillberg/autorestart
// Copyright (c) 2015, Dan Tillberg
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)
if err = syscall.Exec(path, os.Args, os.Environ()); err != nil {
panic(err)
}
}

0 comments on commit cf4f646

Please sign in to comment.