-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
57 lines (46 loc) · 1.12 KB
/
server.go
File metadata and controls
57 lines (46 loc) · 1.12 KB
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
package main
import (
"fmt"
"net"
"net/http"
"strings"
"time"
)
func commandHandler(w http.ResponseWriter, r *http.Request) {
command := strings.TrimPrefix(r.URL.Path, "/command/")
port := strings.TrimPrefix(r.URL.RawQuery, "fsport=")
response, _ := fluidsynthCommand("localhost:"+port, command)
fmt.Fprint(w, string(response))
}
func main() {
http.HandleFunc("/command/", commandHandler)
http.Handle("/", http.FileServer(http.Dir("/opt/lib/fluidweb/www")))
http.ListenAndServe(":9999", nil)
}
func fluidsynthCommand(servAddr string, command string) ([]byte, error) {
tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
if err != nil {
return nil, err
}
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
return nil, err
}
conn.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
_, err = conn.Write([]byte(command + "\n"))
if err != nil {
return nil, err
}
response := make([]byte, 0)
reply := make([]byte, 8196)
goon := true
for goon {
l, err := conn.Read(reply)
response = append(response, reply[:l]...)
if err != nil {
goon = false
}
}
conn.Close()
return response, nil
}