This is the backend API for Scholarly Insight, a platform for exploring and interacting with scholarly articles from arXiv.
- Python 3.10+
- PostgreSQL 14+
- Firebase account (free tier works fine)
macOS (using Homebrew):
brew install postgresql
brew services start postgresqlUbuntu/Debian:
sudo apt update
sudo apt install postgresql postgresql-contrib
sudo systemctl start postgresql
sudo systemctl enable postgresqlWindows: Download and install from PostgreSQL official website
- Access PostgreSQL command line:
# macOS/Linux
sudo -u postgres psql
# Windows
psql -U postgres- Create a database and user:
CREATE DATABASE scholarly;
CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypassword';
GRANT ALL PRIVILEGES ON DATABASE scholarly TO myuser;- Make note of your database name, username, and password for the
.envfile
- Go to Firebase Console
- Create a new project
- Enable Email/Password Authentication:
- In the Firebase console, go to "Authentication" > "Sign-in method"
- Enable "Email/Password" provider
- Generate Firebase Admin SDK credentials:
- Go to "Project settings" > "Service accounts"
- Click "Generate new private key"
- Save the JSON file securely
- Get Firebase Web API Key:
- Go to "Project settings" > "General"
- Copy the "Web API Key"
- Create a
.envfile in thebackenddirectory:
cp .env.example .env- Open the
.envfile and fill in your details:
DATABASE_URL=postgresql://myuser:mypassword@localhost:5432/scholarly
SECRET_KEY=your-generated-secret-key
FIREBASE_CREDENTIALS={"type": "service_account", "project_id": "...", "private_key_id": "...", ...}
FIREBASE_WEB_API_KEY=your-firebase-web-api-key
Notes:
- For
SECRET_KEY, you can generate one with:openssl rand -hex 32 - For
FIREBASE_CREDENTIALS, copy the entire content of the downloaded JSON file FIREBASE_WEB_API_KEYis from your Firebase project settings
Initialize and run migrations using Alembic:
# Install requirements first
pip install -r requirements.txt
# Run migrations
alembic upgrade head- Activate your virtual environment (if you're using one):
# Create virtual environment (if not already created)
python -m venv venv
# Activate virtual environment
# macOS/Linux
source venv/bin/activate
# Windows
venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Start the FastAPI server:
uvicorn app.main:app --reloadThe API should now be running at http://localhost:8000
Once the server is running, API documentation is available at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
backend/
├── alembic/ # Database migration files
├── app/
│ ├── api/ # API endpoints
│ │ └── v1/
│ │ └── endpoints/ # API route handlers
│ ├── core/ # Core functionality
│ │ ├── config.py # Application configuration
│ │ ├── database.py # Database connection
│ │ └── firebase.py # Firebase integration
│ ├── models/ # SQLAlchemy models
│ ├── schemas/ # Pydantic models/schemas
│ ├── services/ # Business logic
│ └── main.py # FastAPI application
├── .env # Environment variables (create from .env.example)
├── .env.example # Example environment variables
├── alembic.ini # Alembic configuration
└── requirements.txt # Python dependencies
- Add new models: Create new files in
app/models/directory - Add new API endpoints: Add new files in
app/api/v1/endpoints/and include them inapp/main.py - Change database schema: Create new migrations with
alembic revision --autogenerate -m "description" - Add business logic: Add new service functions in
app/services/directory
- Create a new file in
app/api/v1/endpoints/ - Define your router and endpoints
- Include the router in
app/main.py
- Create or update files in
app/models/ - Create a migration:
alembic revision --autogenerate -m "Add new model" - Apply the migration:
alembic upgrade head
Use the get_current_user dependency:
from app.api.deps import get_current_user
@router.get("/protected-route")
async def protected_route(current_user = Depends(get_current_user)):
return {"message": "This is protected", "user_id": current_user.id}