Move WS API to separate module

This commit is contained in:
Alexey Khit
2023-05-23 14:21:39 +03:00
parent 82a8e07b66
commit 59555cfe1d
9 changed files with 97 additions and 62 deletions
-2
View File
@@ -38,12 +38,10 @@ func Init() {
log = app.GetLogger("api")
initStatic(cfg.Mod.StaticDir)
initWS(cfg.Mod.Origin)
HandleFunc("api", apiHandler)
HandleFunc("api/config", configHandler)
HandleFunc("api/exit", exitHandler)
HandleFunc("api/ws", apiWS)
// ensure we can listen without errors
listener, err := net.Listen("tcp", cfg.Mod.Listen)
+16 -2
View File
@@ -1,7 +1,9 @@
package api
package ws
import (
"github.com/AlexxIT/go2rtc/internal/api"
"github.com/gorilla/websocket"
"github.com/rs/zerolog/log"
"net/http"
"net/url"
"strings"
@@ -9,6 +11,18 @@ import (
"time"
)
func Init() {
var cfg struct {
Mod struct {
Origin string `yaml:"origin"`
} `yaml:"api"`
}
initWS(cfg.Mod.Origin)
api.HandleFunc("api/ws", apiWS)
}
// Message - struct for data exchange in Web API
type Message struct {
Type string `json:"type"`
@@ -33,7 +47,7 @@ func (m *Message) GetString(key string) string {
type WSHandler func(tr *Transport, msg *Message) error
func HandleWS(msgType string, handler WSHandler) {
func HandleFunc(msgType string, handler WSHandler) {
wsHandlers[msgType] = handler
}