mirror of
https://github.com/fastapi/full-stack-fastapi-template.git
synced 2026-11-03 12:29:42 +00:00
♻️ Serve frontend from FastAPI (#2393)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
588dfd46e5
commit
e80d92452e
+8
-7
@@ -15,7 +15,7 @@ bun run dev
|
||||
|
||||
* Then open your browser at http://localhost:5173/.
|
||||
|
||||
Notice that this live server is not running inside Docker, it's for local development, and that is the recommended workflow. Once you are happy with your frontend, you can build the frontend Docker image and start it, to test it in a production-like environment. But building the image at every change will not be as productive as running the local development server with live reload.
|
||||
Notice that this live server is not running inside Docker, it's for local development, and that is the recommended workflow. Once you are happy with your frontend, you can build the backend Docker image and start it, to test the production-like setup where FastAPI serves the built frontend at `http://localhost:8000`.
|
||||
|
||||
Check the file `package.json` to see other available options.
|
||||
|
||||
@@ -25,18 +25,19 @@ If you are developing an API-only app and want to remove the frontend, you can d
|
||||
|
||||
* Remove the `./frontend` directory.
|
||||
|
||||
* In the `compose.yml` file, remove the whole service / section `frontend`.
|
||||
* In the `backend/app/main.py` file, remove the `app.frontend()` call.
|
||||
|
||||
* In the `compose.override.yml` file, remove the whole service / section `frontend` and `playwright`.
|
||||
* In the `backend/Dockerfile` file, remove the frontend build stage and the `COPY --from=frontend-build` instruction.
|
||||
|
||||
* In the `compose.override.yml` file, remove the `playwright` service.
|
||||
|
||||
Done, you have a frontend-less (api-only) app. 🤓
|
||||
|
||||
---
|
||||
|
||||
If you want, you can also remove the `FRONTEND` environment variables from:
|
||||
If you want, you can also remove the `FRONTEND_HOST` environment variable from:
|
||||
|
||||
* `.env`
|
||||
* `./scripts/*.sh`
|
||||
|
||||
But it would be only to clean them up, leaving them won't really have any effect either way.
|
||||
|
||||
@@ -71,10 +72,10 @@ Notice that everytime the backend changes (changing the OpenAPI schema), you sho
|
||||
|
||||
## Using a Remote API
|
||||
|
||||
If you want to use a remote API, you can set the environment variable `VITE_API_URL` to the URL of the remote API. For example, you can set it in the `frontend/.env` file:
|
||||
By default, the built frontend uses the same origin as the FastAPI app. If you want to use a remote API while running the Vite development server, you can set the environment variable `VITE_API_URL` to the URL of the remote API. For example, you can set it in the `frontend/.env` file:
|
||||
|
||||
```env
|
||||
VITE_API_URL=https://api.my-domain.example.com
|
||||
VITE_API_URL=https://my-domain.example.com
|
||||
```
|
||||
|
||||
Then, when you run the frontend, it will use that URL as the base URL for the API.
|
||||
|
||||
Reference in New Issue
Block a user