2021-09-22 13:36:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/pkg/errors"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2021-09-22 13:36:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const websocketPingInterval = 30 * time.Second
|
|
|
|
|
|
|
|
var (
|
|
|
|
availableActorDocs = []plugins.ActionDocumentation{}
|
|
|
|
availableActorDocsLock sync.RWMutex
|
|
|
|
|
|
|
|
upgrader = websocket.Upgrader{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func registerActorDocumentation(doc plugins.ActionDocumentation) {
|
|
|
|
availableActorDocsLock.Lock()
|
|
|
|
defer availableActorDocsLock.Unlock()
|
|
|
|
|
|
|
|
availableActorDocs = append(availableActorDocs, doc)
|
|
|
|
sort.Slice(availableActorDocs, func(i, j int) bool {
|
|
|
|
return availableActorDocs[i].Name < availableActorDocs[j].Name
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
registerEditorAutoMessageRoutes()
|
|
|
|
registerEditorFrontend()
|
|
|
|
registerEditorGeneralConfigRoutes()
|
|
|
|
registerEditorGlobalMethods()
|
|
|
|
registerEditorRulesRoutes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerEditorFrontend() {
|
|
|
|
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
f, err := configEditorFrontend.Open("editor/index.html")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "opening index.html").Error(), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
io.Copy(w, f)
|
|
|
|
})
|
|
|
|
|
|
|
|
router.HandleFunc("/editor/vars.json", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := json.NewEncoder(w).Encode(struct {
|
2021-12-06 16:25:19 +00:00
|
|
|
IRCBadges []string
|
|
|
|
KnownEvents []*string
|
|
|
|
TemplateFunctions []string
|
|
|
|
TwitchClientID string
|
2022-01-20 00:16:05 +00:00
|
|
|
Version string
|
2021-09-22 13:36:45 +00:00
|
|
|
}{
|
2021-12-06 16:25:19 +00:00
|
|
|
IRCBadges: twitch.KnownBadges,
|
|
|
|
KnownEvents: knownEvents,
|
|
|
|
TemplateFunctions: tplFuncs.GetFuncNames(),
|
|
|
|
TwitchClientID: cfg.TwitchClient,
|
2022-01-20 00:16:05 +00:00
|
|
|
Version: version,
|
2021-09-22 13:36:45 +00:00
|
|
|
}); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-12-06 16:25:19 +00:00
|
|
|
router.PathPrefix("/editor").Handler(http.FileServer(configEditorFrontend))
|
2021-09-22 13:36:45 +00:00
|
|
|
}
|