Skip to content

Latest commit

 

History

History
160 lines (119 loc) · 2.83 KB

File metadata and controls

160 lines (119 loc) · 2.83 KB

FastAPI Backend for Berry React Admin Template

This is a FastAPI backend that provides API endpoints for the Berry React Admin Template frontend.

Features

  • GET /api/hello - Returns a greeting message
  • POST /api/hello - Accepts a message and echoes it back
  • GET /api/users - Returns a list of sample users
  • POST /api/users - Creates a new user and adds it to the in-memory list
  • CORS support for http://localhost:3000 (React frontend)

Prerequisites

  • Python 3.8 or higher
  • pip (Python package installer)

Installation

  1. Navigate to the backend directory:
cd backend
  1. Create a virtual environment (recommended):
python3 -m venv venv
  1. Activate the virtual environment:

    • On Linux/Mac:
      source venv/bin/activate
    • On Windows:
      venv\Scripts\activate
  2. Install the dependencies:

pip install -r requirements.txt

Running the Backend

Start the FastAPI server using uvicorn:

uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

The server will start at http://localhost:8000

API Endpoints

GET /api/hello

Returns a greeting message.

Response:

{
  "message": "Hello from FastAPI!"
}

Example:

curl http://localhost:8000/api/hello

POST /api/hello

Accepts a message and echoes it back.

Request Body:

{
  "message": "Your custom message"
}

Response:

{
  "message": "Your custom message"
}

Example:

curl -X POST http://localhost:8000/api/hello \
  -H "Content-Type: application/json" \
  -d '{"message": "Hello from the client!"}'

GET /api/users

Returns a list of sample users.

Response:

[
  {"id": 1, "name": "Alice"},
  {"id": 2, "name": "Bob"}
]

Example:

curl http://localhost:8000/api/users

POST /api/users

Creates a new user and adds it to the in-memory list.

Request Body:

{
  "name": "John Doe"
}

Response:

{
  "id": 3,
  "name": "John Doe"
}

Example:

curl -X POST http://localhost:8000/api/users \
  -H "Content-Type: application/json" \
  -d '{"name": "John Doe"}'

Interactive API Documentation

FastAPI automatically generates interactive API documentation:

Development

The --reload flag enables auto-reload on code changes, which is useful during development.

Integration with React Frontend

The backend is configured to accept requests from the React frontend running on http://localhost:3000. Make sure both the backend and frontend are running simultaneously for proper integration.

To start the React frontend, navigate to the vite directory and run:

npm install  # or yarn install
npm start    # or yarn start