This repository demonstrates Firebase hosting both a front-end and back-end from the same repository. This test uses a SvelteKit front-end, with experimental Firebase support for SSR as well as a FastAPI Python back-end/API.
Static site files are served through Firebase Hosting, SSR is handled by Cloud Functions (automatically, with Firebase Framework Tools), and the back-end is containerized and deployed using Cloud Run.
Initialize SvelteKit project
npm create svelte@latest frontend
cd frontend
npm install
Initialize Firebase Hosting
cd frontend
firebase experiments:enable webframeworks
firebase init hosting
Run SvelteKit project in Firebase emulator
firebase emulators:start
localhost:5000
in a browser to see the web-app running.Deploy app
firebase deploy
Set up FastAPI back-end
app
directory, Dockerfile
, and requirements.txt
inside ./backend
app/main.py
Build/Run Docker image of back-end server locally
cd backend
docker build -t backend .
docker run -d -p 9090:8080 -e PORT=8080 backend
backend
. Note we can tag it as backend:1.0.0
, for instance, if we would like to version our containers.-p
makes one of the container's ports accessible to the outside computer: in this case, we expose port 8080 of the container to port 9090 on the host. Finally, we set the environment variable PORT to be 8080, allowing the server process in the container to know which port to listen on.localhost:9090/docs
in a browser..env.development
and .env.prduction
files in frontend
. Insert VITE_BASE_URL
as an environment variable in both. In our case, the .development
version should say http://localhost:9090"
and the .production
version should say the URL of the hosted wbsite.npm install axios
api_repo
.deploy.sh
. Note that we set our Firebase project ID, service ID, and name of the image here.frontend/firebase.json
, note the additional rewrites section, redirecting requests to /api
to our containerized service, identified by the service ID../deploy.sh
and confirm the service id when prompted.