Fix base_path for integration with Hass

This commit is contained in:
Alexey Khit
2022-09-16 14:19:23 +03:00
parent 63b9639e86
commit 71e1c840a7
7 changed files with 22 additions and 15 deletions

View File

@@ -36,8 +36,8 @@ func Init() {
initStatic(cfg.Mod.StaticDir) initStatic(cfg.Mod.StaticDir)
initWS() initWS()
HandleFunc("/api/streams", streamsHandler) HandleFunc("api/streams", streamsHandler)
HandleFunc("/api/ws", apiWS) HandleFunc("api/ws", apiWS)
// ensure we can listen without errors // ensure we can listen without errors
listener, err := net.Listen("tcp", cfg.Mod.Listen) listener, err := net.Listen("tcp", cfg.Mod.Listen)
@@ -64,8 +64,15 @@ func Init() {
}() }()
} }
// HandleFunc handle pattern with relative path:
// - "api/streams" => "{basepath}/api/streams"
// - "/streams" => "/streams"
func HandleFunc(pattern string, handler http.HandlerFunc) { func HandleFunc(pattern string, handler http.HandlerFunc) {
http.HandleFunc(basePath+pattern, handler) if len(pattern) == 0 || pattern[0] != '/' {
pattern = basePath + "/" + pattern
}
log.Trace().Str("path", pattern).Msg("[api] register path")
http.HandleFunc(pattern, handler)
} }
func HandleWS(msgType string, handler WSHandler) { func HandleWS(msgType string, handler WSHandler) {

View File

@@ -14,13 +14,13 @@ func initStatic(staticDir string) {
root = http.FS(www.Static) root = http.FS(www.Static)
} }
base := len(basePath)
fileServer := http.FileServer(root) fileServer := http.FileServer(root)
HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { HandleFunc("", func(w http.ResponseWriter, r *http.Request) {
if basePath != "" { if base > 0 {
r.URL.Path = r.URL.Path[len(basePath):] r.URL.Path = r.URL.Path[base:]
} }
fileServer.ServeHTTP(w, r) fileServer.ServeHTTP(w, r)
}) })
} }

View File

@@ -10,8 +10,8 @@ import (
) )
func Init() { func Init() {
api.HandleFunc("/api/stack", stackHandler) api.HandleFunc("api/stack", stackHandler)
api.HandleFunc("/api/exit", exitHandler) api.HandleFunc("api/exit", exitHandler)
streams.HandleFunc("null", nullHandler) streams.HandleFunc("null", nullHandler)
} }

View File

@@ -15,7 +15,7 @@ import (
func Init() { func Init() {
log = app.GetLogger("exec") log = app.GetLogger("exec")
api.HandleFunc("/api/devices", handle) api.HandleFunc("api/devices", handle)
} }
func GetInput(src string) (string, error) { func GetInput(src string) (string, error) {

View File

@@ -14,7 +14,7 @@ func Init() {
streams.HandleFunc("homekit", streamHandler) streams.HandleFunc("homekit", streamHandler)
api.HandleFunc("/api/homekit", apiHandler) api.HandleFunc("api/homekit", apiHandler)
} }
var log zerolog.Logger var log zerolog.Logger

View File

@@ -16,8 +16,8 @@ func Init() {
api.HandleWS(MsgTypeMSE, handlerWS) api.HandleWS(MsgTypeMSE, handlerWS)
api.HandleFunc("/api/frame.mp4", handlerKeyframe) api.HandleFunc("api/frame.mp4", handlerKeyframe)
api.HandleFunc("/api/stream.mp4", handlerMP4) api.HandleFunc("api/stream.mp4", handlerMP4)
} }
var log zerolog.Logger var log zerolog.Logger

View File

@@ -26,6 +26,8 @@ func main() {
app.Init() // init config and logs app.Init() // init config and logs
streams.Init() // load streams list streams.Init() // load streams list
api.Init() // init HTTP API server
echo.Init() echo.Init()
rtsp.Init() // add support RTSP client and RTSP server rtsp.Init() // add support RTSP client and RTSP server
@@ -34,8 +36,6 @@ func main() {
ffmpeg.Init() // add support ffmpeg scheme (depends on exec scheme) ffmpeg.Init() // add support ffmpeg scheme (depends on exec scheme)
hass.Init() // add support hass scheme hass.Init() // add support hass scheme
api.Init() // init HTTP API server
webrtc.Init() webrtc.Init()
mp4.Init() mp4.Init()