diff --git a/cmd/api/api.go b/cmd/api/api.go index 1ea01799..c196a986 100644 --- a/cmd/api/api.go +++ b/cmd/api/api.go @@ -36,8 +36,8 @@ func Init() { initStatic(cfg.Mod.StaticDir) initWS() - HandleFunc("/api/streams", streamsHandler) - HandleFunc("/api/ws", apiWS) + HandleFunc("api/streams", streamsHandler) + HandleFunc("api/ws", apiWS) // ensure we can listen without errors 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) { - 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) { diff --git a/cmd/api/static.go b/cmd/api/static.go index 3d061112..9d9335ea 100644 --- a/cmd/api/static.go +++ b/cmd/api/static.go @@ -14,13 +14,13 @@ func initStatic(staticDir string) { root = http.FS(www.Static) } + base := len(basePath) fileServer := http.FileServer(root) - HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - if basePath != "" { - r.URL.Path = r.URL.Path[len(basePath):] + HandleFunc("", func(w http.ResponseWriter, r *http.Request) { + if base > 0 { + r.URL.Path = r.URL.Path[base:] } - fileServer.ServeHTTP(w, r) }) } diff --git a/cmd/debug/debug.go b/cmd/debug/debug.go index 22746cf9..a50c3ed8 100644 --- a/cmd/debug/debug.go +++ b/cmd/debug/debug.go @@ -10,8 +10,8 @@ import ( ) func Init() { - api.HandleFunc("/api/stack", stackHandler) - api.HandleFunc("/api/exit", exitHandler) + api.HandleFunc("api/stack", stackHandler) + api.HandleFunc("api/exit", exitHandler) streams.HandleFunc("null", nullHandler) } diff --git a/cmd/ffmpeg/device/devices.go b/cmd/ffmpeg/device/devices.go index aefa3dc6..31f603e9 100644 --- a/cmd/ffmpeg/device/devices.go +++ b/cmd/ffmpeg/device/devices.go @@ -15,7 +15,7 @@ import ( func Init() { log = app.GetLogger("exec") - api.HandleFunc("/api/devices", handle) + api.HandleFunc("api/devices", handle) } func GetInput(src string) (string, error) { diff --git a/cmd/homekit/homekit.go b/cmd/homekit/homekit.go index c39a2c13..3084122d 100644 --- a/cmd/homekit/homekit.go +++ b/cmd/homekit/homekit.go @@ -14,7 +14,7 @@ func Init() { streams.HandleFunc("homekit", streamHandler) - api.HandleFunc("/api/homekit", apiHandler) + api.HandleFunc("api/homekit", apiHandler) } var log zerolog.Logger diff --git a/cmd/mp4/mp4.go b/cmd/mp4/mp4.go index a43907f8..6e9d1ef4 100644 --- a/cmd/mp4/mp4.go +++ b/cmd/mp4/mp4.go @@ -16,8 +16,8 @@ func Init() { api.HandleWS(MsgTypeMSE, handlerWS) - api.HandleFunc("/api/frame.mp4", handlerKeyframe) - api.HandleFunc("/api/stream.mp4", handlerMP4) + api.HandleFunc("api/frame.mp4", handlerKeyframe) + api.HandleFunc("api/stream.mp4", handlerMP4) } var log zerolog.Logger diff --git a/main.go b/main.go index 4b664c48..8123c026 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,8 @@ func main() { app.Init() // init config and logs streams.Init() // load streams list + api.Init() // init HTTP API server + echo.Init() 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) hass.Init() // add support hass scheme - api.Init() // init HTTP API server - webrtc.Init() mp4.Init()