1
0
Fork 0
mirror of https://github.com/Luzifer/streamdeck.git synced 2024-12-29 22:21:24 +00:00

Add API server

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2020-12-10 18:42:44 +01:00
parent ecdf4967ae
commit e10b9480b9
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
5 changed files with 51 additions and 0 deletions

35
cmd/streamdeck/api.go Normal file
View file

@ -0,0 +1,35 @@
package main
import (
"encoding/json"
"net/http"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
func init() {
api := router.PathPrefix("/api/v1/").Subrouter()
api.HandleFunc("/get-config", handleSerializeUserConfig)
router.PathPrefix("/")
router.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("x-streamdeck-version", version)
w.Header().Set("cache-control", "no-cache")
h.ServeHTTP(w, r)
})
})
}
func handleSerializeUserConfig(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
if err := json.NewEncoder(w).Encode(userConfig); err != nil {
log.WithError(err).Error("Unable to marshal user config")
http.Error(w, errors.Wrap(err, "marshal user config").Error(), http.StatusInternalServerError)
return
}
}

View file

@ -68,6 +68,7 @@ func (a attributeCollection) RGBAToColor() color.RGBA {
} }
type config struct { type config struct {
APIListen string `json:"api_listen" yaml:"api_listen"`
AutoReload bool `json:"auto_reload" yaml:"auto_reload"` AutoReload bool `json:"auto_reload" yaml:"auto_reload"`
CaptionBorder int `json:"caption_border" yaml:"caption_border"` CaptionBorder int `json:"caption_border" yaml:"caption_border"`
CaptionColor [4]int `json:"caption_color" yaml:"caption_color"` CaptionColor [4]int `json:"caption_color" yaml:"caption_color"`

View file

@ -10,6 +10,7 @@ require (
github.com/Luzifer/streamdeck v0.0.0-20191122003228-a2f524a6b22c github.com/Luzifer/streamdeck v0.0.0-20191122003228-a2f524a6b22c
github.com/fsnotify/fsnotify v1.4.7 github.com/fsnotify/fsnotify v1.4.7
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/gorilla/mux v1.8.0
github.com/jfreymuth/pulse v0.0.0-20200804114219-7d61c4938214 github.com/jfreymuth/pulse v0.0.0-20200804114219-7d61c4938214
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646

View file

@ -10,6 +10,8 @@ github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h1:hyb9oH7vZsitZCiBt0ZvifOrB+qc8PS5IiilCIb87rg= github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h1:hyb9oH7vZsitZCiBt0ZvifOrB+qc8PS5IiilCIb87rg=
github.com/jfreymuth/pulse v0.0.0-20200804114219-7d61c4938214 h1:2xVJKIumEUWeV3vczQwn61SHjNZ94Bwk+4CTjmcePxk= github.com/jfreymuth/pulse v0.0.0-20200804114219-7d61c4938214 h1:2xVJKIumEUWeV3vczQwn61SHjNZ94Bwk+4CTjmcePxk=
github.com/jfreymuth/pulse v0.0.0-20200804114219-7d61c4938214/go.mod h1:cpYspI6YljhkUf1WLXLLDmeaaPFc3CnGLjDZf9dZ4no= github.com/jfreymuth/pulse v0.0.0-20200804114219-7d61c4938214/go.mod h1:cpYspI6YljhkUf1WLXLLDmeaaPFc3CnGLjDZf9dZ4no=

View file

@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"net/http"
"os" "os"
"os/signal" "os/signal"
"path" "path"
@ -12,6 +13,7 @@ import (
"github.com/Luzifer/rconfig/v2" "github.com/Luzifer/rconfig/v2"
"github.com/Luzifer/streamdeck" "github.com/Luzifer/streamdeck"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/gorilla/mux"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sashko/go-uinput" "github.com/sashko/go-uinput"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -36,6 +38,8 @@ var (
activeLoops []refreshingDisplayElement activeLoops []refreshingDisplayElement
pageStack []string pageStack []string
router = mux.NewRouter()
sd *streamdeck.Client sd *streamdeck.Client
kbd uinput.Keyboard kbd uinput.Keyboard
@ -141,6 +145,14 @@ func main() {
} }
} }
if userConfig.APIListen != "" {
go func() {
log.WithError(
http.ListenAndServe(userConfig.APIListen, router),
).Fatal("API-server exited unexpectedly")
}()
}
var ( var (
actor *int actor *int
actStart time.Time actStart time.Time