diff --git a/src/device/input/parse/v01/http.go b/src/device/input/parse/v01/http.go new file mode 100644 index 0000000..978c876 --- /dev/null +++ b/src/device/input/parse/v01/http.go @@ -0,0 +1,58 @@ +package v01 + +import ( + "io" + "log" + "mayhem-party/src/device/input/wrap" + "net/http" + "sync" + "syscall" +) + +func (v01 *V01) listen() { + if v01.cfg.Feedback.Addr == "" { + return + } + mutex := &sync.RWMutex{} + s := &http.Server{ + Addr: v01.cfg.Feedback.Addr, + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet { + mutex.RLock() + defer mutex.RUnlock() + } else { + mutex.Lock() + defer mutex.Unlock() + } + if r.Method == http.MethodGet { + r = r.WithContext(v01.ctx) + user, ok := v01.cfg.Users[r.URL.Query().Get("user")] + if !ok { + user = v01.cfg.Users["broadcast"] + } + w.Write([]byte(user.Message)) + } else if r.URL.Path == "/broadcast" { + b, _ := io.ReadAll(r.Body) + v := v01.cfg.Users["broadcast"] + v.Message = string(b) + v01.cfg.Users["broadcast"] = v + } + if _, ok := r.URL.Query()["refresh"]; ok { + select { + case wrap.ChSigUsr1 <- syscall.SIGUSR1: + default: + } + } + }), + } + go func() { + <-v01.ctx.Done() + log.Println("closing v01 server") + s.Close() + }() + log.Println("starting v01 server") + if err := s.ListenAndServe(); err != nil && v01.ctx.Err() == nil { + log.Println("err with v01 server", err) + panic(err) + } +} diff --git a/src/device/input/parse/v01/testdata/v01.yaml b/src/device/input/parse/v01/testdata/v01.yaml index 9da3858..c5e4b3f 100644 --- a/src/device/input/parse/v01/testdata/v01.yaml +++ b/src/device/input/parse/v01/testdata/v01.yaml @@ -1,3 +1,5 @@ +feedback: + addr: :17071 users: bel: player: 0 diff --git a/src/device/input/parse/v01/v01.go b/src/device/input/parse/v01/v01.go index 2b84a6d..916d35d 100644 --- a/src/device/input/parse/v01/v01.go +++ b/src/device/input/parse/v01/v01.go @@ -3,16 +3,11 @@ package v01 import ( "context" "encoding/json" - "io" "io/ioutil" "log" "mayhem-party/src/device/input/button" "mayhem-party/src/device/input/raw" - "mayhem-party/src/device/input/wrap" - "net/http" "os" - "sync" - "syscall" "time" "gopkg.in/yaml.v2" @@ -66,54 +61,6 @@ func NewV01(ctx context.Context, src raw.Raw) *V01 { return result } -func (v01 *V01) listen() { - if v01.cfg.Feedback.Addr == "" { - return - } - mutex := &sync.RWMutex{} - s := &http.Server{ - Addr: v01.cfg.Feedback.Addr, - Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Method == http.MethodGet { - mutex.RLock() - defer mutex.RUnlock() - } else { - mutex.Lock() - defer mutex.Unlock() - } - if r.Method == http.MethodGet { - r = r.WithContext(v01.ctx) - user, ok := v01.cfg.Users[r.URL.Query().Get("user")] - if !ok { - user = v01.cfg.Users["broadcast"] - } - w.Write([]byte(user.Message)) - } else if r.URL.Path == "/broadcast" { - b, _ := io.ReadAll(r.Body) - v := v01.cfg.Users["broadcast"] - v.Message = string(b) - v01.cfg.Users["broadcast"] = v - } - if _, ok := r.URL.Query()["refresh"]; ok { - select { - case wrap.ChSigUsr1 <- syscall.SIGUSR1: - default: - } - } - }), - } - go func() { - <-v01.ctx.Done() - log.Println("closing v01 server") - s.Close() - }() - log.Println("starting v01 server") - if err := s.ListenAndServe(); err != nil && v01.ctx.Err() == nil { - log.Println("err with v01 server", err) - panic(err) - } -} - func (v01 *V01) CloseWrap() raw.Raw { v01.can() return v01.src