forked from robbi5/gomumblesoundboard
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttp.go
118 lines (98 loc) · 2.52 KB
/
http.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"embed"
"encoding/json"
"flag"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"strconv"
)
//go:embed public
var Assets embed.FS
func (s *sb) setupHTTP() {
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.FS(FSPrefixer("public", Assets))))
mux.HandleFunc("/restart", func(_ http.ResponseWriter, _ *http.Request) {
os.Exit(255)
})
mux.HandleFunc("/rescan", func(w http.ResponseWriter, r *http.Request) {
scanDirs(flag.Args())
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
})
mux.HandleFunc("/files.json", func(w http.ResponseWriter, r *http.Request) {
files := make([]File, 0)
for _, f := range soundfiles {
files = append(files, File{
Name: f.Name,
Folder: f.Folder,
})
}
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(files)
})
mux.HandleFunc("/stop", func(w http.ResponseWriter, r *http.Request) {
s.iChan <- Interaction{
Stop: true,
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
})
mux.HandleFunc("/volume", func(w http.ResponseWriter, r *http.Request) {
strVol := r.URL.Query().Get("vol")
vol, err := strconv.Atoi(strVol)
if err != nil {
http.Error(w, fmt.Sprintf("couldn't convert %s to integer: %v", strVol, err), http.StatusBadRequest)
return
}
if vol < 0 || vol > 100 {
http.Error(w, fmt.Sprintf("number too small or too large: %s", strVol), http.StatusBadRequest)
return
}
volume := float32(vol) / 100 * s.maxVol
s.iChan <- Interaction{
Volume: volume,
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(fmt.Sprintf("volume set to %.1f%%", volume*100)))
})
mux.HandleFunc("/play", func(w http.ResponseWriter, r *http.Request) {
unescape := r.URL.Query().Get("file")
f, ok := soundfiles[unescape]
if !ok {
http.Error(w, fmt.Sprintf("%s: file not found", unescape), http.StatusNotFound)
return
}
if !s.mtx.TryLock() {
http.Error(w, "already playing a sound, gtfo", http.StatusBadRequest)
return
}
s.iChan <- Interaction{
File: &f,
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(fmt.Sprintf("Playing %s\n", f.FullPath)))
})
go func() {
log.Fatal(http.ListenAndServe(":3000", mux))
}()
}
type FuncFS func(name string) (fs.File, error)
func (f FuncFS) Open(name string) (fs.File, error) {
return f(name)
}
func FSPrefixer(prefix string, f fs.FS) fs.FS {
return FuncFS(func(name string) (fs.File, error) {
if name == "." {
name = ""
}
if name == "" {
name = prefix
} else {
name = prefix + "/" + name
}
return f.Open(name)
})
}