Pablote
Pablote

Reputation: 5083

Firebase Hosting: is it possible to rewrite a URL and pass part of the path to Cloud Run?

I want to front an API on Cloud Run with Firebase Hosting on the /api prefix. So that an incoming request for /api/something is handled by the API /something handler. Seems simple but I can't figure it out, so I'm wondering if it's even possible with Firebase hosting.

First thing I've tried is:

"rewrites": [
      {
        "source": "/api",
        "run": {
          "serviceId": "my-api",
          "region": "us-central1"
        }
      }
    ]

This correctly routes /api requests to the service root /, but calling /api/something just 404s.

Second attempt was using "source": "/api/**" or "regex": "/api(/.*)?",. This correctly receives requests like /api/something but routes it to the API root / instead of /something.

Ideally, I would like it to strip the /api prefix, or allow me to use the extracted regex group on the routed url. Thanks.

Upvotes: 5

Views: 1451

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317497

Ideally, I would like it to strip the /api prefix, or allow me to use the extracted regex group on the routed url.

That's not going to be possible. The full URI of the request will always be preserved and forwarded to Cloud Run. Your code on the Cloud Run side should be able to deal with the full path as it was sent to Firebase Hosting. If you want to strip any path prefixes, it will have to be in the code deployed to Cloud Run.

Upvotes: 6

Related Questions