2025-11-04 23:27:26 -06:00
..
2025-10-14 18:01:51 -05:00
2025-10-14 10:42:40 -05:00

red head API Server

A Python Flask server that serves data for the red head application.

Endpoints

  • GET /posts - Get first 10 posts for initial load
  • GET /posts/more?count=N - Get next 10 posts, starting from index N
  • GET /posts/total - Get total number of posts available
  • GET / - Server status endpoint

Setup Instructions

  1. Install Python 3 if you don't have it already

  2. Navigate to the server directory:

    cd /Users/jariancottingham/Projects/redhead/src/server
    
  3. Create a virtual environment (recommended):

    python -m venv venv
    source venv/bin/activate  # On Windows use: venv\Scripts\activate
    
  4. Install dependencies:

    pip install -r requirements.txt
    
  5. Run the server:

    python app.py
    
  6. The server will be available at http://localhost:5000

Data Format

The server returns JSON data in the same format as used by the frontend:

[
  {
    "id": "1",
    "title": "This is a sample Reddit post title",
    "url": "https://example.com/sample-post",
    "image": "https://picsum.photos/400/300?random=1"
  }
]

Usage with Frontend

Update the frontend's fetchRedditPosts() and fetchMorePosts() functions to call your server endpoints instead of using mock data:

// Replace URLs with your server endpoints
function fetchRedditPosts() {
  return fetch('/posts')
    .then(response => response.json())
}

function fetchMorePosts(currentCount) {
  return fetch(`/posts/more?count=${currentCount}`)
    .then(response => response.json())
}

Example Data

The sample data is stored in sample_posts.json. You can replace this with your own data, or connect to a real Reddit API.

To add your own posts:

  1. Replace the content of sample_posts.json with your data
  2. Each post should follow this structure:
    {
      "id": "post_id",
      "title": "Post title",
      "url": "https://example.com/post-url",
      "image": "https://example.com/image-url"
    }
    

Running the Server

After installation, run the server with:

python app.py

The server will start on port 5000 and listen for connections from localhost.

Testing Endpoints

You can test the endpoints directly using curl or a browser:

  • http://localhost:5000/posts - Get initial posts
  • http://localhost:5000/posts/more?count=10 - Get more posts starting from index 10
  • http://localhost:5000/posts/total - Get total post count