-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
141 lines (111 loc) · 2.72 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"embed"
"flag"
"fmt"
"io/fs"
"io/ioutil"
"log"
"net/http"
"os/exec"
"runtime"
"sync"
"gopkg.in/antage/eventsource.v1"
)
//go:embed public/*
var static embed.FS
var es eventsource.EventSource
var lockState = make(map[string]bool)
var lockMutex = &sync.Mutex{}
func main() {
es = eventsource.New(nil, func(request *http.Request) [][]byte {
return [][]byte{[]byte("Access-Control-Allow-Origin: http://localhost:5000")}
})
defer es.Close()
host := flag.String("host", "", "Listen on")
port := flag.Int("port", 9009, "Listen port for Server")
flag.Parse()
http.HandleFunc("/client", clientEvent)
http.HandleFunc("/unlock", unlock)
http.HandleFunc("/is-locked", isLocked)
http.Handle("/events", es)
http.Handle("/", http.FileServer(getFileSystem()))
log.Printf("Starting webserver at %s:%d\n", *host, *port)
url := fmt.Sprintf("localhost:%d", *port)
if *host != "" {
url = fmt.Sprintf("%s:%d", *host, *port)
}
openbrowser(url)
err := http.ListenAndServe(fmt.Sprintf("%s:%d", *host, *port), nil)
if err != nil {
log.Fatalln(err)
}
}
func clientEvent(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(500)
return
}
log.Println("Incoming request from pd()")
es.SendEventMessage(string(body), "message", "1")
pdAction := r.Header.Get("pd-action")
pdId := r.Header.Get("pd-id")
if pdAction == "pause" && len(pdId) > 0 {
lockMutex.Lock()
lockState[pdId] = true
lockMutex.Unlock()
log.Printf("Received lock request for %s\n", pdId)
}
}
func isLocked(w http.ResponseWriter, r *http.Request) {
lockMutex.Lock()
entry, ok := lockState[r.Header.Get("pd-id")]
defer lockMutex.Unlock()
if !ok {
w.Write([]byte("0"))
return
}
if entry {
w.Write([]byte("1"))
return
}
w.Write([]byte("0"))
}
func unlock(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:5000")
w.Header().Set("Access-Control-Allow-Headers", "pd-id")
lockMutex.Lock()
defer lockMutex.Unlock()
id := r.Header.Get("pd-id")
_, ok := lockState[id]
if !ok {
return
}
delete(lockState, id)
log.Printf("Removed lock for %s\n", id)
}
func openbrowser(url string) {
var err error
log.Printf("Try to opening URL %s in browser\n", url)
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Run()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Run()
case "darwin":
err = exec.Command("open", url).Run()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Println(err)
}
}
func getFileSystem() http.FileSystem {
fsys, err := fs.Sub(static, "public")
if err != nil {
log.Fatal(err)
}
return http.FS(fsys)
}