Skip to content

Commit 9fc07af

Browse files
committed
Add autorestart functionality to the API and update the config saving logic in the editor
1 parent 02bde2c commit 9fc07af

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ require (
2222
gopkg.in/yaml.v3 v3.0.1
2323
)
2424

25+
require (
26+
github.com/tillberg/stringset v0.0.0-20220429204947-12df33ab1cd6 // indirect
27+
github.com/tillberg/watcher v0.0.0-20200309151633-e9412552417a // indirect
28+
gopkg.in/fsnotify/fsnotify.v1 v1.4.7 // indirect
29+
)
30+
2531
require (
2632
github.com/davecgh/go-spew v1.1.1 // indirect
2733
github.com/google/uuid v1.3.1 // indirect
@@ -37,6 +43,7 @@ require (
3743
github.com/pion/transport/v2 v2.2.4 // indirect
3844
github.com/pion/turn/v2 v2.1.3 // indirect
3945
github.com/pmezard/go-difflib v1.0.0 // indirect
46+
github.com/tillberg/autorestart v0.0.0-20220524165049-22c3f5bc7fce
4047
golang.org/x/mod v0.12.0 // indirect
4148
golang.org/x/net v0.15.0 // indirect
4249
golang.org/x/sys v0.12.0 // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
114114
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
115115
github.com/tadglines/go-pkgs v0.0.0-20210623144937-b983b20f54f9 h1:aeN+ghOV0b2VCmKKO3gqnDQ8mLbpABZgRR2FVYx4ouI=
116116
github.com/tadglines/go-pkgs v0.0.0-20210623144937-b983b20f54f9/go.mod h1:roo6cZ/uqpwKMuvPG0YmzI5+AmUiMWfjCBZpGXqbTxE=
117+
github.com/tillberg/autorestart v0.0.0-20220524165049-22c3f5bc7fce h1:NcQ3uInL+f6lY1A75Cupbla2BVhbOIR0VnhX3vAqau4=
118+
github.com/tillberg/autorestart v0.0.0-20220524165049-22c3f5bc7fce/go.mod h1:ZDC6T6DzgNA725fdY3teO4XGeK1jfSFeeZ5Hvlnc+O8=
119+
github.com/tillberg/stringset v0.0.0-20220429204947-12df33ab1cd6 h1:9QVeacXfCTL1mS2tClma3psYYcOIYSOQn0/BD/viH/Q=
120+
github.com/tillberg/stringset v0.0.0-20220429204947-12df33ab1cd6/go.mod h1:fYcF9dZnzutLCGBoA9x0Umi4kqTf5PsSXTW+M4P0qaE=
121+
github.com/tillberg/watcher v0.0.0-20200309151633-e9412552417a h1:bEuy52O1mMAemrWT8/fHsvYYc43jaZ2RRmsj+1aYtUg=
122+
github.com/tillberg/watcher v0.0.0-20200309151633-e9412552417a/go.mod h1:5jDyx/qS4SXr0VPTpPikj9jLQAIR3cqdtZy2f5M2KuA=
117123
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
118124
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
119125
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -227,6 +233,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
227233
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
228234
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
229235
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
236+
gopkg.in/fsnotify/fsnotify.v1 v1.4.7 h1:XNNYLJHt73EyYiCZi6+xjupS9CpvmiDgjPTAjrBlQbo=
237+
gopkg.in/fsnotify/fsnotify.v1 v1.4.7/go.mod h1:Fyux9zXlo4rWoMSIzpn9fDAYjalPqJ/K1qJ27s+7ltE=
230238
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
231239
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
232240
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

internal/api/api.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import (
77
"net"
88
"net/http"
99
"os"
10+
"runtime"
1011
"strconv"
1112
"strings"
1213
"sync"
1314

1415
"github.com/AlexxIT/go2rtc/internal/app"
1516
"github.com/rs/zerolog"
17+
"github.com/tillberg/autorestart"
1618
)
1719

1820
func Init() {
@@ -48,6 +50,7 @@ func Init() {
4850
HandleFunc("api", apiHandler)
4951
HandleFunc("api/config", configHandler)
5052
HandleFunc("api/exit", exitHandler)
53+
HandleFunc("api/restart", restartHandler)
5154

5255
// ensure we can listen without errors
5356
var err error
@@ -222,6 +225,19 @@ func exitHandler(w http.ResponseWriter, r *http.Request) {
222225
os.Exit(code)
223226
}
224227

228+
func restartHandler(w http.ResponseWriter, r *http.Request) {
229+
if r.Method != "POST" {
230+
http.Error(w, "", http.StatusBadRequest)
231+
return
232+
}
233+
if runtime.GOOS != "windows" {
234+
go autorestart.RestartViaExec()
235+
} else {
236+
os.Exit(0)
237+
}
238+
239+
}
240+
225241
type Source struct {
226242
ID string `json:"id,omitempty"`
227243
Name string `json:"name,omitempty"`

www/editor.html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
<body>
2525
<script src="main.js"></script>
2626
<div>
27-
<button id="save">Save & Restart</button>
27+
<button id="save">Save</button>
28+
<button id="saverestart">Save & Restart</button>
2829
</div>
2930
<br>
3031
<div id="config"></div>
@@ -42,11 +43,19 @@
4243
alert('Config was changed from another place. Refresh the page and make changes again');
4344
return;
4445
}
46+
});
47+
48+
document.getElementById('saverestart').addEventListener('click', async () => {
49+
let r = await fetch('api/config', {cache: 'no-cache'});
50+
if (r.ok && dump !== await r.text()) {
51+
alert('Config was changed from another place. Refresh the page and make changes again');
52+
return;
53+
}
4554

4655
r = await fetch('api/config', {method: 'POST', body: editor.getValue()});
4756
if (r.ok) {
4857
alert('OK');
49-
fetch('api/exit?code=100', {method: 'POST'});
58+
fetch('api/restart', {method: 'POST'});
5059
} else {
5160
alert(await r.text());
5261
}

0 commit comments

Comments
 (0)