|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "github.com/h2non/filetype" |
| 7 | + "gopkg.in/antage/eventsource.v1" |
| 8 | + "io/ioutil" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | + "sync" |
| 12 | +) |
| 13 | + |
| 14 | +var es eventsource.EventSource |
| 15 | +var lockState = make(map[string]bool) |
| 16 | +var lockMutex = &sync.Mutex{} |
| 17 | + |
| 18 | +func main() { |
| 19 | + es = eventsource.New(nil, func(request *http.Request) [][]byte { |
| 20 | + return [][]byte{[]byte("Access-Control-Allow-Origin: http://localhost:5000")} |
| 21 | + }) |
| 22 | + defer es.Close() |
| 23 | + port := flag.Int("port", 9009, "Listen port for Server") |
| 24 | + |
| 25 | + flag.Parse() |
| 26 | + |
| 27 | + http.HandleFunc("/client", clientEvent) |
| 28 | + http.HandleFunc("/unlock", unlock) |
| 29 | + http.HandleFunc("/is-locked", isLocked) |
| 30 | + http.Handle("/events", es) |
| 31 | + http.HandleFunc("/", serveStaticFile) |
| 32 | + |
| 33 | + err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil) |
| 34 | + if err != nil { |
| 35 | + log.Fatalln(err) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func clientEvent(w http.ResponseWriter, r *http.Request) { |
| 40 | + body, err := ioutil.ReadAll(r.Body) |
| 41 | + |
| 42 | + if err != nil { |
| 43 | + w.WriteHeader(500) |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + log.Println("Incomming request from PD") |
| 48 | + es.SendEventMessage(string(body), "message", "1") |
| 49 | + |
| 50 | + pdAction := r.Header.Get("pd-action") |
| 51 | + pdId := r.Header.Get("pd-id") |
| 52 | + |
| 53 | + if pdAction == "pause" && len(pdId) > 0 { |
| 54 | + lockMutex.Lock() |
| 55 | + lockState[pdId] = true |
| 56 | + lockMutex.Unlock() |
| 57 | + log.Printf("Received lock request for %s\n", pdId) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func isLocked(w http.ResponseWriter, r *http.Request) { |
| 62 | + lockMutex.Lock() |
| 63 | + entry, ok := lockState[r.Header.Get("pd-id")] |
| 64 | + defer lockMutex.Unlock() |
| 65 | + |
| 66 | + if !ok { |
| 67 | + w.Write([]byte("0")) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + if entry { |
| 72 | + w.Write([]byte("1")) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + w.Write([]byte("0")) |
| 77 | +} |
| 78 | + |
| 79 | +func unlock(w http.ResponseWriter, r *http.Request) { |
| 80 | + lockMutex.Lock() |
| 81 | + defer lockMutex.Unlock() |
| 82 | + |
| 83 | + id := r.Header.Get("pd-id") |
| 84 | + |
| 85 | + _, ok := lockState[id] |
| 86 | + |
| 87 | + if !ok { |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + delete(lockState, id) |
| 92 | + |
| 93 | + log.Printf("Removed lock for %s\n", id) |
| 94 | +} |
| 95 | + |
| 96 | +func serveStaticFile(w http.ResponseWriter, r *http.Request) { |
| 97 | + filePath := r.URL.Path |
| 98 | + |
| 99 | + if filePath == "/" { |
| 100 | + filePath = "/index.html" |
| 101 | + } |
| 102 | + |
| 103 | + file, err := Asset(fmt.Sprintf("public%s", filePath)) |
| 104 | + if err != nil { |
| 105 | + w.WriteHeader(404) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + kind := filetype.GetType(filePath) |
| 110 | + w.Header().Set("Content-Type", kind.MIME.Value) |
| 111 | + w.Write(file) |
| 112 | +} |
0 commit comments