streamdeck-remote/server.go

104 lines
1.8 KiB
Go

package main
import (
"github.com/valyala/fastjson"
"io"
"io/ioutil"
"log"
"meow.tf/streamdeck-remote/client"
"meow.tf/streamdeck/sdk"
"net"
"net/http"
)
type serverActionFunc func(context, host, token string, settings *fastjson.Value)
func serverActionHandler(f serverActionFunc) sdk.ActionHandler{
return func(action, context string, payload *fastjson.Value, deviceId string) {
settings := payload.Get("settings")
host := sdk.JsonStringValue(settings, "remote_host")
if host == "" {
return
}
h, p, err := net.SplitHostPort(host)
// Ensure there's a host + port
if err != nil {
h = host
p = "4443"
}
host = net.JoinHostPort(h, p)
log.Println("Using host", host)
token := sdk.JsonStringValue(settings, "remote_token")
if token == "" {
return
}
f(context, host, token, settings)
}
}
func websiteActionHandler(context, host, token string, settings *fastjson.Value) {
c := client.New(host, token)
url := sdk.JsonStringValue(settings, "url")
if url == "" {
sdk.ShowAlert(context)
return
}
background := settings.GetBool("background")
var err error
if background {
// Use Go to visit URL
res, err := http.Get(url)
if err == nil {
defer res.Body.Close()
_, err = io.Copy(ioutil.Discard, res.Body)
}
} else {
err = c.OpenURL(url)
}
if err != nil {
log.Println("error opening url:", err)
sdk.ShowAlert(context)
} else {
sdk.ShowOk(context)
}
}
func openActionHandler(context, host, token string, settings *fastjson.Value) {
c := client.New(host, token)
path := sdk.JsonStringValue(settings, "path")
if path == "" {
sdk.ShowAlert(context)
return
}
err := c.Open(path)
if err != nil {
log.Println("error opening path:", err)
sdk.ShowAlert(context)
} else {
sdk.ShowOk(context)
}
}