Spaces:
Sleeping
Commit-1
ReactFast Project Overview
A minimal full-stack setup with a FastAPI backend serving a Vite + React frontend. The frontend builds into frontend/dist, and FastAPI mounts it under the /app route.
What this project includes
- Backend: FastAPI app (
backend/app.py) mounting static files from the React build. - Frontend: Vite + React (TypeScript) app configured with base
/app/so assets resolve when hosted under that path. - Local dev: Build frontend once, then run the FastAPI server. Visit
http://127.0.0.1:<port>/app/.
Dependencies
- Python (backend)
- fastapi: Web framework serving API and static files
- uvicorn: ASGI server to run the FastAPI app
- Node (frontend)
- react, react-dom: UI library and DOM renderer
- vite: Build tool and dev server
- @vitejs/plugin-react: React plugin for Vite (Fast Refresh, JSX, etc.)
- typescript, @types/react, @types/react-dom: TypeScript and React typings
Folder tree and file descriptions
backend/
backend/
ββ app.py # FastAPI app mounting the React build at /app
ββ requirements.txt # Python dependencies for backend (fastapi, uvicorn)
ββ __pycache__/ # Python bytecode cache (auto-generated)
ββ .venv/ # Local Python virtual environment (developer local)
frontend/
frontend/
ββ index.html # Vite HTML entry; loads /src/main.tsx
ββ package.json # Frontend scripts and dependencies
ββ package-lock.json # Exact dependency versions (npm lockfile)
ββ tsconfig.json # TypeScript compiler options for the app
ββ vite.config.ts # Vite config; base set to /app and outDir=dist
ββ src/ # Application source code
β ββ App.tsx # Main UI component rendered by the app
β ββ main.tsx # React entry; creates root and renders <App />
β ββ style.css # Minimal global styles
ββ dist/ # Production build output (generated by `npm run build`)
β ββ index.html # Built HTML referencing hashed asset files under /app
β ββ assets/ # Hashed JS/CSS bundles and sourcemaps
β ββ index-*.js # Production JS bundle (hashed filename)
β ββ index-*.js.map # Sourcemap for debugging (if enabled)
β ββ index-*.css # Production CSS bundle (hashed filename)
ββ node_modules/ # Installed frontend dependencies (generated by npm)
How it works
- Build the frontend (Vite) which outputs to
frontend/distwith asset URLs prefixed by/app/. - Start the FastAPI server; it mounts
frontend/distas static files at the/approute. - Navigate to
http://127.0.0.1:<port>/app/to view the app (index.html + assets).
Common commands (optional)
- Build frontend:
npm run buildinfrontend/ - Run backend:
uvicorn app:app --host 127.0.0.1 --port 8000inbackend/(after installing requirements)
Notes
- If you change the frontend base path or output folder, update either Viteβs
base/build.outDiror the backend static mount path accordingly. dist/is generatedβdo not edit files there manually; edit files undersrc/instead and rebuild.
Commit-2
High-level summary of enabling frontend β backend communication.
Backend
- Added a simple POST API at
/api/transformthat accepts{ text: string }and returns{ result: string }with a minimal transformation. - Kept the React static site mounted at
/appso built assets resolve correctly (aligned with Vitebase: '/app/').
- Added a simple POST API at
Frontend
- Updated the main UI (
src/App.tsx) to include:- A label, a textbox for user input, and a submit button.
- On submit, a
fetch('/api/transform', { method: 'POST', body: JSON.stringify({ text }) })call. - Displays the returned
resultstring below the form.
- Light, elegant styling in
src/style.cssto keep the layout centered and readable without overengineering.
- Updated the main UI (
Result
- Users can type a message, submit, and see a transformed response from the FastAPI backendβserved together under the same origin, avoiding CORS configuration.
Commit-3
High-level summary of adding containerization (Docker) support.
Purpose
- Provide a reproducible build artifact bundling backend (FastAPI) and pre-built frontend (Vite) into one image.
- Simplify deployment: single
docker runserves both API and static UI.
Dockerfile Structure (multi-stage)
- Stage 1 (node:20-alpine): installs frontend deps and runs
npm run buildto producedist/. - Stage 2 (python:3.12-slim): installs backend Python deps, copies backend code and built
frontend/dist. - Starts with:
uvicorn backend.app:app --host 0.0.0.0 --port 8000.
- Stage 1 (node:20-alpine): installs frontend deps and runs
Key Paths Inside Image
/app/backendβ FastAPI code/app/frontend/distβ Built static assets served by FastAPI at/approute
Added Files
Dockerfileβ Multi-stage build definition.dockerignoreβ Excludes node_modules, virtual envs, caches, VCS metadata, logs, etc., reducing context size and image bloat
Build & Run (local)
- Build image:
docker build -t reactfast .
- Run container:
docker run --rm -p 8000:8000 reactfast
- Access UI:
http://localhost:8000/app/
- Build image:
Customization Notes
- To enable auto-reload in development, run locally without Docker or create a dev Dockerfile variant mounting source.
- For production scaling, consider adding a process manager (e.g.,
gunicornwithuvicorn.workers.UvicornWorker) and HEALTHCHECK. - Pin dependency versions more strictly if reproducibility across time is critical.
Outcome
- Project can be built and deployed as a single immutable image; frontend and backend remain in sync at build time.
Pushing the app to Azure Container Registry. Use below commands
docker loginto login to Azure Container Registrydocker tag <app_name>:latest <registry-name>.azurecr.io/<app_name>:latestdocker push <registry-name>.azurecr.io/<app_name>:latest
Commit-4
High-level summary of adding CI automation (GitHub Actions) to build and push the Docker image to Azure Container Registry (ACR).
Purpose
- Automate image builds on each push to
main(and manual dispatch) ensuring the registry always has an upβtoβdate image. - Provide traceable image tags (
<commit-sha>andlatest) for rollback and promotion.
- Automate image builds on each push to
Secrets / Inputs
AZURE_CREDENTIALS: JSON fromaz ad sp create-for-rbac --role AcrPush --scopes <ACR_ID> --sdk-auth.ACR_LOGIN_SERVER: e.g.minimum.azurecr.io.- (Optional)
ACR_NAMEif deriving login server dynamically.
Workflow Steps (simplified)
- Checkout repository source.
- Azure login using service principal (
azure/login). - Authenticate to ACR (either via
az acr loginordocker/login-action). - Build Docker image with existing multi-stage
Dockerfile. - Tag image twice:
:<git-sha>and:latest. - Push both tags to ACR.
- Summarize pushed tags for visibility.
Tagging Strategy
- Immutable:
registry/app:{{ github.sha }}for precise traceability. - Mutable convenience:
registry/app:latestfor default deployments / quick tests.
- Immutable:
Minimal Example (conceptual)
- Trigger:
on: push: branches: [ main ]+workflow_dispatch. - Uses official actions:
actions/checkout,azure/login,docker/build-push-action.
- Trigger:
Benefits
- Eliminates manual local build/push steps.
- Reduces risk of βworks on my machineβ discrepancies.
- Provides consistent, auditable artifact generation tied to commit history.
Follow-on Opportunities
- Add deploy job (e.g., to Azure Web App / Container Apps / AKS) after successful push.
- Introduce image security scanning (Trivy / Microsoft Defender).
- Add build cache (GitHub Actions cache or ACR build tasks) for faster builds.
- Add semantic version tagging (e.g.,
v1.2.3) if release process formalizes.
Outcome
- CI pipeline ensures every code change can rapidly produce a runnable, versioned container image in ACR, ready for deployment workflows.