mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
Compare commits
2 Commits
v0.19.1
...
feat/js-wa
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d4cb251e9 | ||
|
|
050ada6a70 |
5
app/demo/document.go
Normal file
5
app/demo/document.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
import "syscall/js"
|
||||
|
||||
var document = js.Global().Get("document")
|
||||
71
app/demo/index.html
Normal file
71
app/demo/index.html
Normal file
@@ -0,0 +1,71 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<script src="wasm_exec.js"></script>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
#output-container {
|
||||
flex-grow: 1;
|
||||
}
|
||||
#log-container {
|
||||
background-color: #1e1e1e;
|
||||
width: 100%;
|
||||
height: 200px;
|
||||
overflow-y: auto;
|
||||
overflow-x: scroll;
|
||||
box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
#logs {
|
||||
margin: 0;
|
||||
padding-left: 2px;
|
||||
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
|
||||
font-size: 13px;
|
||||
line-height: 1.4;
|
||||
color: #a8cc8c;
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
const container = document.getElementById("log-container");
|
||||
|
||||
function isScrolledToBottom() {
|
||||
const scrollThreshold = 1;
|
||||
return (
|
||||
container.scrollHeight - container.clientHeight <=
|
||||
container.scrollTop + scrollThreshold
|
||||
);
|
||||
}
|
||||
function scrollToBottomManual() {
|
||||
const container = document.getElementById("log-container");
|
||||
// Set scrollTop to the maximum value possible (scrollHeight - clientHeight)
|
||||
container.scrollTop = container.scrollHeight - container.clientHeight;
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="output-container">
|
||||
<div id="output"></div>
|
||||
</div>
|
||||
|
||||
<div id="log-container">
|
||||
<pre id="logs"></pre>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const go = new Go();
|
||||
WebAssembly.instantiateStreaming(
|
||||
fetch("main.wasm"),
|
||||
go.importObject,
|
||||
).then((result) => {
|
||||
go.run(result.instance);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
42
app/demo/log.go
Normal file
42
app/demo/log.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import "syscall/js"
|
||||
|
||||
type LogWriter struct {
|
||||
Element js.Value
|
||||
Container js.Value
|
||||
}
|
||||
|
||||
func (pw *LogWriter) ScrollToBottom() {
|
||||
scrollHeight := pw.Container.Get("scrollHeight").Int()
|
||||
clientHeight := pw.Container.Get("clientHeight").Int()
|
||||
pw.Container.Set("scrollTop", scrollHeight-clientHeight)
|
||||
}
|
||||
|
||||
func (pw *LogWriter) IsScrolledToBottom() bool {
|
||||
scrollHeight := pw.Container.Get("scrollHeight").Int()
|
||||
clientHeight := pw.Container.Get("clientHeight").Int()
|
||||
scrollTop := pw.Container.Get("scrollTop").Int()
|
||||
return scrollHeight-clientHeight <= scrollTop+25
|
||||
}
|
||||
|
||||
func (pw *LogWriter) Write(p []byte) (n int, err error) {
|
||||
if !pw.Element.IsUndefined() {
|
||||
currentText := pw.Element.Get("textContent").String()
|
||||
newText := currentText + string(p)
|
||||
pw.Element.Set("textContent", newText)
|
||||
if pw.IsScrolledToBottom() {
|
||||
pw.ScrollToBottom()
|
||||
}
|
||||
}
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func NewLogWriter(id string) *LogWriter {
|
||||
element := document.Call("getElementById", id)
|
||||
container := element.Get("parentElement")
|
||||
return &LogWriter{
|
||||
Element: element,
|
||||
Container: container,
|
||||
}
|
||||
}
|
||||
71
app/demo/main.go
Normal file
71
app/demo/main.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/jwetzell/showbridge-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
slog.SetLogLoggerLevel(slog.LevelDebug)
|
||||
slog.SetDefault(slog.New(slog.NewTextHandler(NewLogWriter("logs"), &slog.HandlerOptions{
|
||||
Level: slog.LevelDebug,
|
||||
})))
|
||||
|
||||
router, moduleConfigErrors, routeConfigErrors := showbridge.NewRouter(config.Config{
|
||||
Api: config.ApiConfig{
|
||||
Enabled: false,
|
||||
Port: 0,
|
||||
},
|
||||
Modules: []config.ModuleConfig{
|
||||
{
|
||||
Id: "midi",
|
||||
Type: "midi.input",
|
||||
Params: map[string]any{
|
||||
"port": "Launchpad S",
|
||||
},
|
||||
},
|
||||
},
|
||||
Routes: []config.RouteConfig{
|
||||
{
|
||||
Input: "midi",
|
||||
Processors: []config.ProcessorConfig{
|
||||
{
|
||||
Type: "debug.log",
|
||||
},
|
||||
{
|
||||
Type: "web.set",
|
||||
Params: map[string]any{
|
||||
"id": "output",
|
||||
"property": "textContent",
|
||||
"value": "{{.Payload}}",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if len(moduleConfigErrors) > 0 {
|
||||
for _, err := range moduleConfigErrors {
|
||||
println("Module config error:", err.Error)
|
||||
}
|
||||
}
|
||||
|
||||
if len(routeConfigErrors) > 0 {
|
||||
for _, err := range routeConfigErrors {
|
||||
println("Route config error:", err.Error)
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go func() {
|
||||
router.Start(ctx)
|
||||
fmt.Println("router stopped")
|
||||
}()
|
||||
<-ctx.Done()
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
|
||||
7
internal/module/desktop.go
Normal file
7
internal/module/desktop.go
Normal file
@@ -0,0 +1,7 @@
|
||||
//go:build cgo
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
_ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv"
|
||||
)
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//go:build cgo
|
||||
//go:build cgo || js
|
||||
|
||||
package module
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
_ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv"
|
||||
)
|
||||
|
||||
type MIDIInput struct {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//go:build cgo
|
||||
//go:build cgo || js
|
||||
|
||||
package module
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
_ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv"
|
||||
)
|
||||
|
||||
type MIDIOutput struct {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
|
||||
@@ -64,7 +64,7 @@ func (t *TimeTimer) Start(ctx context.Context) error {
|
||||
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("net.tcp.client unable to get router from context")
|
||||
return errors.New("time.timer unable to get router from context")
|
||||
}
|
||||
t.router = router
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
|
||||
91
internal/module/web-onclick.go
Normal file
91
internal/module/web-onclick.go
Normal file
@@ -0,0 +1,91 @@
|
||||
//go:build js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"syscall/js"
|
||||
"time"
|
||||
|
||||
"github.com/google/jsonschema-go/jsonschema"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type WebOnClick struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
logger *slog.Logger
|
||||
ElementId string
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "web.onclick",
|
||||
Title: "On Click",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"id": {
|
||||
Title: "Element ID",
|
||||
Type: "string",
|
||||
Description: "ID of the HTML element to attach the click listener to",
|
||||
},
|
||||
},
|
||||
Required: []string{"duration"},
|
||||
AdditionalProperties: nil,
|
||||
},
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
params := config.Params
|
||||
|
||||
idString, err := params.GetString("id")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("web.onclick id error: %w", err)
|
||||
}
|
||||
|
||||
return &WebOnClick{ElementId: idString, config: config, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (woc *WebOnClick) Id() string {
|
||||
return woc.config.Id
|
||||
}
|
||||
|
||||
func (woc *WebOnClick) Type() string {
|
||||
return woc.config.Type
|
||||
}
|
||||
|
||||
func (woc *WebOnClick) Start(ctx context.Context) error {
|
||||
woc.logger.Debug("running")
|
||||
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("net.tcp.client unable to get router from context")
|
||||
}
|
||||
woc.router = router
|
||||
woc.ctx = ctx
|
||||
|
||||
element := js.Global().Get("document").Call("getElementById", woc.ElementId)
|
||||
|
||||
if element.IsNull() || element.IsUndefined() {
|
||||
return fmt.Errorf("web.onclick unable to find element with id: %s", woc.ElementId)
|
||||
}
|
||||
|
||||
element.Set("onclick", js.FuncOf(func(js.Value, []js.Value) interface{} {
|
||||
if woc.router != nil {
|
||||
woc.router.HandleInput(woc.ctx, woc.Id(), time.Now())
|
||||
}
|
||||
return nil
|
||||
}))
|
||||
|
||||
<-ctx.Done()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (woc *WebOnClick) Stop() {
|
||||
}
|
||||
7
internal/module/web.go
Normal file
7
internal/module/web.go
Normal file
@@ -0,0 +1,7 @@
|
||||
//go:build js
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
_ "gitlab.com/gomidi/midi/v2/drivers/webmididrv"
|
||||
)
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package processor
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package processor
|
||||
|
||||
import (
|
||||
|
||||
@@ -21,6 +21,11 @@ type HTTPRequestDo struct {
|
||||
URL *template.Template
|
||||
}
|
||||
|
||||
type HTTPResponse struct {
|
||||
Status int
|
||||
Body []byte
|
||||
}
|
||||
|
||||
func (hrd *HTTPRequestDo) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
|
||||
templateData := wrappedPayload
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package processor
|
||||
|
||||
import (
|
||||
@@ -17,11 +19,6 @@ type HTTPResponseCreate struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
type HTTPResponse struct {
|
||||
Status int
|
||||
Body []byte
|
||||
}
|
||||
|
||||
func (hrc *HTTPResponseCreate) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
templateData := wrappedPayload
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//go:build cgo
|
||||
//go:build cgo || js
|
||||
|
||||
package processor
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//go:build cgo
|
||||
//go:build cgo || js
|
||||
|
||||
package processor
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//go:build cgo
|
||||
//go:build cgo || js
|
||||
|
||||
package processor
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//go:build cgo
|
||||
//go:build cgo || js
|
||||
|
||||
package processor
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package processor
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package processor
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package processor
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package processor
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package processor
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !js
|
||||
|
||||
package processor
|
||||
|
||||
import (
|
||||
|
||||
102
internal/processor/web-set.go
Normal file
102
internal/processor/web-set.go
Normal file
@@ -0,0 +1,102 @@
|
||||
//go:build js
|
||||
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log/slog"
|
||||
"syscall/js"
|
||||
|
||||
"github.com/google/jsonschema-go/jsonschema"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type WebSet struct {
|
||||
config config.ProcessorConfig
|
||||
ModuleId string
|
||||
ElementId string
|
||||
Property string
|
||||
Value *template.Template
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (kvs *WebSet) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
|
||||
element := js.Global().Get("document").Call("getElementById", kvs.ElementId)
|
||||
|
||||
if element.IsNull() || element.IsUndefined() {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("web.set unable to find element with id: %s", kvs.ElementId)
|
||||
}
|
||||
|
||||
var valueBuffer bytes.Buffer
|
||||
err := kvs.Value.Execute(&valueBuffer, wrappedPayload)
|
||||
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, err
|
||||
}
|
||||
|
||||
element.Set(kvs.Property, valueBuffer.String())
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (kvs *WebSet) Type() string {
|
||||
return kvs.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "web.set",
|
||||
Title: "Set Web Element Property",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"id": {
|
||||
Title: "Element ID",
|
||||
Type: "string",
|
||||
},
|
||||
"property": {
|
||||
Title: "Property",
|
||||
Type: "string",
|
||||
},
|
||||
"value": {
|
||||
Title: "Value",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"id", "property", "value"},
|
||||
AdditionalProperties: nil,
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
|
||||
params := config.Params
|
||||
|
||||
idString, err := params.GetString("id")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("web.set id error: %w", err)
|
||||
}
|
||||
|
||||
propertyString, err := params.GetString("property")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("web.set property error: %w", err)
|
||||
}
|
||||
|
||||
valueString, err := params.GetString("value")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("web.set value error: %w", err)
|
||||
}
|
||||
valueTemplate, err := template.New("template").Parse(valueString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &WebSet{config: config, ElementId: idString, Property: propertyString, Value: valueTemplate, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user