accept PUT /broadcast to change the broadcast message
parent
9073658e12
commit
bd5654128e
|
|
@ -7,5 +7,5 @@ import (
|
||||||
|
|
||||||
func TestParser(t *testing.T) {
|
func TestParser(t *testing.T) {
|
||||||
var _ button.Parser = button.Plaintext{}
|
var _ button.Parser = button.Plaintext{}
|
||||||
var _ button.Parser = button.V01{}
|
var _ button.Parser = &button.V01{}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package button
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"mayhem-party/src/device/input/raw"
|
"mayhem-party/src/device/input/raw"
|
||||||
|
|
@ -46,12 +47,12 @@ type (
|
||||||
v01Transformation map[string]string
|
v01Transformation map[string]string
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewV01(ctx context.Context, src raw.Raw) V01 {
|
func NewV01(ctx context.Context, src raw.Raw) *V01 {
|
||||||
var cfg v01Cfg
|
var cfg v01Cfg
|
||||||
b, _ := ioutil.ReadFile(FlagButtonV01Config)
|
b, _ := ioutil.ReadFile(FlagButtonV01Config)
|
||||||
yaml.Unmarshal(b, &cfg)
|
yaml.Unmarshal(b, &cfg)
|
||||||
ctx, can := context.WithCancel(ctx)
|
ctx, can := context.WithCancel(ctx)
|
||||||
result := V01{
|
result := &V01{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
can: can,
|
can: can,
|
||||||
src: src,
|
src: src,
|
||||||
|
|
@ -61,19 +62,26 @@ func NewV01(ctx context.Context, src raw.Raw) V01 {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v01 V01) listen() {
|
func (v01 *V01) listen() {
|
||||||
if v01.cfg.Feedback.Addr == "" {
|
if v01.cfg.Feedback.Addr == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s := &http.Server{
|
s := &http.Server{
|
||||||
Addr: v01.cfg.Feedback.Addr,
|
Addr: v01.cfg.Feedback.Addr,
|
||||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
r = r.WithContext(v01.ctx)
|
if r.Method == http.MethodGet {
|
||||||
user, ok := v01.cfg.Users[r.URL.Query().Get("user")]
|
r = r.WithContext(v01.ctx)
|
||||||
if !ok {
|
user, ok := v01.cfg.Users[r.URL.Query().Get("user")]
|
||||||
user = v01.cfg.Users["broadcast"]
|
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
|
||||||
}
|
}
|
||||||
w.Write([]byte(user.Message))
|
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
|
|
@ -88,17 +96,17 @@ func (v01 V01) listen() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v01 V01) CloseWrap() raw.Raw {
|
func (v01 *V01) CloseWrap() raw.Raw {
|
||||||
v01.can()
|
v01.can()
|
||||||
return v01.src
|
return v01.src
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v01 V01) Close() {
|
func (v01 *V01) Close() {
|
||||||
v01.can()
|
v01.can()
|
||||||
v01.src.Close()
|
v01.src.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v01 V01) Read() []Button {
|
func (v01 *V01) Read() []Button {
|
||||||
line := v01.src.Read()
|
line := v01.src.Read()
|
||||||
var msg v01Msg
|
var msg v01Msg
|
||||||
if err := json.Unmarshal(line, &msg); err != nil {
|
if err := json.Unmarshal(line, &msg); err != nil {
|
||||||
|
|
@ -134,7 +142,7 @@ func (t v01Transformation) pipe(s string) string {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v01 V01) telemetry(msg v01Msg) {
|
func (v01 *V01) telemetry(msg v01Msg) {
|
||||||
if FlagDebug {
|
if FlagDebug {
|
||||||
log.Printf("%s|%dms", msg.U, time.Now().UnixNano()/int64(time.Millisecond)-msg.T)
|
log.Printf("%s|%dms", msg.U, time.Now().UnixNano()/int64(time.Millisecond)-msg.T)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -134,4 +135,24 @@ func TestV01Feedback(t *testing.T) {
|
||||||
t.Error(b)
|
t.Error(b)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("change broadcast", func(t *testing.T) {
|
||||||
|
want := `my new broadcast`
|
||||||
|
r, _ := http.NewRequest(http.MethodPut, "http://localhost:27071/broadcast", strings.NewReader(want))
|
||||||
|
resp, err := http.DefaultClient.Do(r)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
|
||||||
|
resp, err = http.Get("http://localhost:27071")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
b, _ := io.ReadAll(resp.Body)
|
||||||
|
if string(b) != want {
|
||||||
|
t.Error(string(b))
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue