Package Description
$ dotnet add package masterzdran.react.fastapi.project.acceleratorThis project demonstrates a secure architecture for handling configuration and secrets in a full-stack application. It consists of a FastAPI backend and a React/TypeScript frontend, both designed to follow security best practices for managing sensitive configuration.
![Architecture Diagram]
.
├── backend/ # FastAPI Python backend
│ ├── app/ # Application code
│ │ ├── __init__.py
│ │ ├── main.py # Main FastAPI application
│ │ ├── middleware.py # CORS and other middleware
│ │ ├── routes/ # API routes
│ │ │ ├── __init__.py
│ │ │ └── config.py # Config endpoints
│ │ └── utils/ # Utility functions
│ │ ├── __init__.py
│ │ └── security.py # API key validation
│ ├── .env.example # Example environment variables
│ └── requirements.txt # Python dependencies
│
├── frontend/ # React/TypeScript frontend
│ ├── index.html # Entry HTML file
│ ├── package.json # npm dependencies and scripts
│ ├── tsconfig.json # TypeScript configuration
│ ├── vite.config.ts # Vite configuration
│ └── src/ # Source code
│ ├── App.tsx # Main application component
│ ├── main.tsx # Entry point
│ ├── api/ # API client code
│ │ └── index.ts # API client implementation
│ └── components/ # React components
│ └── ConfigDisplay.tsx # Display fetched config
│
├── .vscode/ # VSCode configuration
│ └── launch.json # Debug configurations
│
└── README.md # This file
Navigate to the backend directory:
cd backend
Create a virtual environment and activate it:
python -m venv venv
# On Windows
venv\Scripts\activate
# On Linux/Mac
source venv/bin/activate
Install dependencies:
pip install -r requirements.txt
Create a .env file based on .env.example:
cp .env.example .env
Modify the .env file with your actual values:
API_KEY=your_secure_api_key_here
ENTRA_CLIENT_ID=your_entra_client_id
ENTRA_SCOPE=api://your-app-id/access_as_user
FRONTEND_ORIGIN=http://localhost:5173
Start the FastAPI server:
uvicorn app.main:app --reload
The backend will be available at http://localhost:8000.
Navigate to the frontend directory:
cd frontend
Install dependencies:
npm install
Start the development server:
npm run dev
The frontend will be available at http://localhost:5173.
This project implements a secure pattern for handling configuration:
/config.json to obtain the API keyx-api-key headerThis approach ensures:
Frontend code is always accessible to users through browser dev tools. Any secret baked into a frontend build (whether in JS bundles, environment variables loaded at build time, or hardcoded values) can be extracted by users. This project demonstrates the correct pattern: retrieving sensitive information at runtime from a protected API.
Instead of hardcoding the API key in the frontend, this application:
/config.json) that provides the API keyThis approach allows the API key to be rotated without rebuilding the frontend.
Cross-Origin Resource Sharing (CORS) protection is implemented to prevent browsers from making requests to the API from unauthorized origins. The backend only accepts requests from the specified frontend origin (e.g., http://localhost:5173 for development), blocking requests from malicious sites.
This project includes VSCode launch configurations for debugging both the backend and frontend simultaneously:
You can set breakpoints in both Python and TypeScript code, and VSCode will stop at both.
To debug individually:
/config.jsonIf you encounter a 404 error when the frontend tries to fetch /config.json:
FRONTEND_ORIGIN in your backend .env file matches your frontend URL (default: http://localhost:5173)./config.json endpoint should be proxied to the backend in development.http://localhost:8000/config.json in your browser to see if the backend serves it correctly..env file.If you successfully get /config.json but subsequent API calls fail:
x-api-key header is being correctly added to requests.If the frontend can't connect to the backend: