-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlaunch.go
54 lines (43 loc) · 1.09 KB
/
launch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"log"
"net/http"
"os/exec"
"sync"
"syscall"
"go.tmthrgd.dev/spork/dbus"
"go.tmthrgd.dev/spork/web"
)
func launchHandler() http.HandlerFunc {
redirect := func(w http.ResponseWriter, r *http.Request) error {
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return nil
}
waitStarted, isRunning, err := dbus.WaitForAudacious()
if err != nil {
log.Printf("dbus.WaitForAudacious failed (launch endpoint impacted): %v", err)
}
var mu sync.Mutex
return web.ErrorHandler(func(w http.ResponseWriter, r *http.Request) error {
mu.Lock()
defer mu.Unlock()
if isRunning() {
// Audacious is already running.
return redirect(w, r)
}
cmd := exec.Command("audacious")
cmd.SysProcAttr = &syscall.SysProcAttr{
// Setpgid causes audacious to have a different process
// group and not be terminated when the parent process
// is killed with ^C.
Setpgid: true,
}
if err := cmd.Start(); err != nil {
return err
}
// Wait until Audacious has started and it's D-Bus interface is
// active.
waitStarted()
return redirect(w, r)
})
}