Skip to content

Commit e10b948

Browse files
committed
Add API server
Signed-off-by: Knut Ahlers <[email protected]>
1 parent ecdf496 commit e10b948

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

cmd/streamdeck/api.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/pkg/errors"
8+
log "github.com/sirupsen/logrus"
9+
)
10+
11+
func init() {
12+
api := router.PathPrefix("/api/v1/").Subrouter()
13+
api.HandleFunc("/get-config", handleSerializeUserConfig)
14+
15+
router.PathPrefix("/")
16+
17+
router.Use(func(h http.Handler) http.Handler {
18+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
19+
w.Header().Set("x-streamdeck-version", version)
20+
w.Header().Set("cache-control", "no-cache")
21+
22+
h.ServeHTTP(w, r)
23+
})
24+
})
25+
}
26+
27+
func handleSerializeUserConfig(w http.ResponseWriter, r *http.Request) {
28+
w.Header().Set("content-type", "application/json")
29+
30+
if err := json.NewEncoder(w).Encode(userConfig); err != nil {
31+
log.WithError(err).Error("Unable to marshal user config")
32+
http.Error(w, errors.Wrap(err, "marshal user config").Error(), http.StatusInternalServerError)
33+
return
34+
}
35+
}

cmd/streamdeck/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func (a attributeCollection) RGBAToColor() color.RGBA {
6868
}
6969

7070
type config struct {
71+
APIListen string `json:"api_listen" yaml:"api_listen"`
7172
AutoReload bool `json:"auto_reload" yaml:"auto_reload"`
7273
CaptionBorder int `json:"caption_border" yaml:"caption_border"`
7374
CaptionColor [4]int `json:"caption_color" yaml:"caption_color"`

cmd/streamdeck/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/Luzifer/streamdeck v0.0.0-20191122003228-a2f524a6b22c
1111
github.com/fsnotify/fsnotify v1.4.7
1212
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
13+
github.com/gorilla/mux v1.8.0
1314
github.com/jfreymuth/pulse v0.0.0-20200804114219-7d61c4938214
1415
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
1516
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646

cmd/streamdeck/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV
1010
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
1111
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
1212
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
13+
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
14+
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
1315
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h1:hyb9oH7vZsitZCiBt0ZvifOrB+qc8PS5IiilCIb87rg=
1416
github.com/jfreymuth/pulse v0.0.0-20200804114219-7d61c4938214 h1:2xVJKIumEUWeV3vczQwn61SHjNZ94Bwk+4CTjmcePxk=
1517
github.com/jfreymuth/pulse v0.0.0-20200804114219-7d61c4938214/go.mod h1:cpYspI6YljhkUf1WLXLLDmeaaPFc3CnGLjDZf9dZ4no=

cmd/streamdeck/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"net/http"
67
"os"
78
"os/signal"
89
"path"
@@ -12,6 +13,7 @@ import (
1213
"github.com/Luzifer/rconfig/v2"
1314
"github.com/Luzifer/streamdeck"
1415
"github.com/fsnotify/fsnotify"
16+
"github.com/gorilla/mux"
1517
"github.com/pkg/errors"
1618
"github.com/sashko/go-uinput"
1719
log "github.com/sirupsen/logrus"
@@ -36,6 +38,8 @@ var (
3638
activeLoops []refreshingDisplayElement
3739
pageStack []string
3840

41+
router = mux.NewRouter()
42+
3943
sd *streamdeck.Client
4044

4145
kbd uinput.Keyboard
@@ -141,6 +145,14 @@ func main() {
141145
}
142146
}
143147

148+
if userConfig.APIListen != "" {
149+
go func() {
150+
log.WithError(
151+
http.ListenAndServe(userConfig.APIListen, router),
152+
).Fatal("API-server exited unexpectedly")
153+
}()
154+
}
155+
144156
var (
145157
actor *int
146158
actStart time.Time

0 commit comments

Comments
 (0)