pocketbase icon indicating copy to clipboard operation
pocketbase copied to clipboard

Feature Request: Ability to proxy requests to another endpoint

Open rodydavis opened this issue 2 years ago • 4 comments

Problem

Right now if you want to extend pocketbase as a framework without Go via JS then there are some limitations when it comes to routing. Curious if it would be possible to include an API similar to static files that can use a wild card to proxy requests too.

routerAdd("GET", "/*", $apis.staticDirectoryHandler("/path/to/public", false))

Possible Solution

This would need to support more than just GET. For example the API could be:

routerAdd("*", "/*", $apis.singleHostReverseProxy(
     scheme: "http",
     host: "localhost:4321",
))

Here is an example that works if you compile with GO:

package main

import (
	"log"
	"net/http/httputil"
	"net/url"

	"github.com/labstack/echo/v5"
	"github.com/pocketbase/pocketbase"
	"github.com/pocketbase/pocketbase/core"
)

func main() {
    app := pocketbase.New()

    app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
        proxy := httputil.NewSingleHostReverseProxy(&url.URL{
			Scheme: "http",
			Host:   "localhost:4321",
		})
		e.Router.Any("/*", echo.WrapHandler(proxy))
		e.Router.Any("/", echo.WrapHandler(proxy))
        return nil
    })

    if err := app.Start(); err != nil {
        log.Fatal(err)
    }
}

I also wrote a blog post on how I am doing this with Astro: https://rodydavis.com/posts/astro-ssr-pocketbase-single-server

The downside of this is I believe it does not support auto updates ./pocketbase update and requires GO knowledge to implement.

rodydavis avatar Jan 31 '24 19:01 rodydavis

If you go to http://127.0.0.1:8090/_/#/login it can be easily exposed admin login

awd3321 avatar Mar 01 '24 19:03 awd3321

Hmm, not sure but why not we reverse the request. So user request to astro server and in some path, it will proxy to pocketbase?

https://stackoverflow.com/questions/73212935/astro-how-to-proxy-service-calls

tegarimansyah avatar Mar 29 '24 01:03 tegarimansyah

If you already has a working Go code then why don't just use it? You could continue writing your hooks in JS like before (but you need to pull in JSVM yourself) .

thangngoc89 avatar May 20 '24 12:05 thangngoc89