diff --git a/examples/bayern.html b/examples/bayern.html index 30507ff..014c9e5 100644 --- a/examples/bayern.html +++ b/examples/bayern.html @@ -35,25 +35,11 @@
-
- + \ No newline at end of file diff --git a/main.go b/main.go index fb8e5f3..e8c2e4e 100644 --- a/main.go +++ b/main.go @@ -1,17 +1,39 @@ package main import ( + "bytes" + "io" + "log" + "net/http" + "os" + "strings" + + "github.com/labstack/echo/v5" "github.com/pocketbase/pocketbase" "github.com/pocketbase/pocketbase/apis" "github.com/pocketbase/pocketbase/core" - "log" - "os" - "strings" ) func main() { app := pocketbase.New() + app.OnBeforeServe().Add(func(e *core.ServeEvent) error { + _, err := e.Router.AddRoute(echo.Route{ + Method: http.MethodGet, + Path: "/cors", + Handler: func(c echo.Context) error { + return corsProxyHandler(c) + }, + Middlewares: []echo.MiddlewareFunc{ + apis.ActivityLogger(app), + }, + }) + if err != nil { + return err + } + return nil + }) + app.OnBeforeServe().Add(func(e *core.ServeEvent) error { // add new "GET /hello" route to the app router (echo) e.Router.GET("/*", apis.StaticDirectoryHandler(os.DirFS("./pb_public"), false)) @@ -33,3 +55,65 @@ func main() { log.Fatal(err) } } + +func corsProxyHandler(c echo.Context) error { + // Get the target URL from the request query string + url := c.QueryParam("url") + if url == "" { + return c.JSON(http.StatusBadRequest, "url param is required") + } + + // Fetch the resource from the target URL + resp, err := http.Get(url) + if err != nil { + return c.JSON(http.StatusInternalServerError, "Failed to fetch the URL") + } + defer resp.Body.Close() + + // If the content is a .m3u8 file, rewrite the URLs + if strings.HasSuffix(url, ".m3u8") { + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + return c.JSON(http.StatusInternalServerError, "Failed to read response body") + } + + proxyBaseURL := c.Scheme() + "://" + c.Request().Host + c.Path() + "?url=" + url[:strings.LastIndex(url, "/")+1] + + // Rewrite the URLs in the .m3u8 file + lines := strings.Split(string(bodyBytes), "\n") + for i, line := range lines { + if strings.HasSuffix(line, ".ts") { + lines[i] = proxyBaseURL + line + } + } + modifiedContent := strings.Join(lines, "\n") + + // Write the modified content to the response + c.Response().Header().Set("Content-Type", resp.Header.Get("Content-Type")) + c.Response().WriteHeader(resp.StatusCode) + _, err = io.Copy(c.Response().Writer, bytes.NewReader([]byte(modifiedContent))) + if err != nil { + return c.JSON(http.StatusInternalServerError, "Failed to copy modified response body") + } + + return nil + } + + // Copy the headers from the fetched response to the proxy response + for key, values := range resp.Header { + for _, value := range values { + c.Response().Header().Add(key, value) + } + } + + // Copy the status code from the fetched response to the proxy response + c.Response().WriteHeader(resp.StatusCode) + + // Copy the body from the fetched response to the proxy response + _, err = io.Copy(c.Response().Writer, resp.Body) + if err != nil { + return c.JSON(http.StatusInternalServerError, "Failed to copy response body") + } + + return nil +} diff --git a/pb_public/admin/manage.html b/pb_public/admin/manage.html index 9b35f75..b6583f7 100644 --- a/pb_public/admin/manage.html +++ b/pb_public/admin/manage.html @@ -86,7 +86,7 @@ // fetch mediaData from http://127.0.0.1:8090/api/collections/media/records/ with GET method function fetchMediaData() { - fetch("/api/collections/media/records/", { + fetch("/api/collections/media/records/?perPage=3000", { method: "GET", headers: { "Content-Type": "application/json", diff --git a/pb_public/index.html b/pb_public/index.html old mode 100644 new mode 100755 index 6e7f81c..1d23561 --- a/pb_public/index.html +++ b/pb_public/index.html @@ -70,4 +70,4 @@ - \ No newline at end of file +