2.4 KiB
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 loadGET /posts/more?count=N- Get next 10 posts, starting from index NGET /posts/total- Get total number of posts availableGET /- Server status endpoint
Setup Instructions
-
Install Python 3 if you don't have it already
-
Navigate to the server directory:
cd /Users/jariancottingham/Projects/redhead/src/server -
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows use: venv\Scripts\activate -
Install dependencies:
pip install -r requirements.txt -
Run the server:
python app.py -
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:
- Replace the content of
sample_posts.jsonwith your data - 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 postshttp://localhost:5000/posts/more?count=10- Get more posts starting from index 10http://localhost:5000/posts/total- Get total post count