Conversation
Fixed code so the server can run and run on port 8000
This reverts commit e99f4e4.
There was a problem hiding this comment.
Pull request overview
This PR updates the local/server startup workflow and refreshes internal imports/docs to better reflect the project structure.
Changes:
- Updates
run.shto support a--serverflag and standardizes on port8000. - Switches several imports to package-relative style and improves inline comments.
- Adds a comprehensive
PROJECT_FLOW.mdwalkthrough and removes theProcfile.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 13 comments.
Show a summary per file
| File | Description |
|---|---|
| run.sh | Adds --server mode and changes how the app is launched. |
| PROJECT_FLOW.md | New documentation describing startup/runtime flow and request paths. |
| Procfile | Removes Heroku/Procfile-based startup entry. |
| forum/routes.py | Converts imports to relative and does minor refactors/comments in handlers. |
| forum/models.py | Comment cleanup and removes a debug print from time formatting. |
| forum/app.py | Converts model import to relative and adjusts how create_app is imported; adds comments. |
| forum/init.py | Uses relative imports for blueprint/models; comment cleanup. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -1,6 +1,11 @@ | |||
| # export PORT=5006 | |||
| #!/bin/bash | |||
| export SECRET_KEY="kristofer" | |||
There was a problem hiding this comment.
Hard-coding a SECRET_KEY in this script is a security risk and also diverges from the app’s actual config (config.py already sets SECRET_KEY). Prefer reading SECRET_KEY from the environment in config.py (with a safe default for local dev) and remove it from run.sh so secrets aren’t committed or duplicated.
| export SECRET_KEY="kristofer" |
| cd ./forum && flask run --host=0.0.0.0 --port=8000 | ||
| else | ||
| cd ./forum && flask run --port=8000 |
There was a problem hiding this comment.
This script cds into ./forum and runs flask run without --app/FLASK_APP. Combined with forum/app.py doing from forum import create_app, running from inside forum/ will fail to import the forum package. Consider running from repo root and using flask --app forum.app run ... (or exporting FLASK_APP) instead of changing directories.
| cd ./forum && flask run --host=0.0.0.0 --port=8000 | |
| else | |
| cd ./forum && flask run --port=8000 | |
| flask --app forum.app run --host=0.0.0.0 --port=8000 | |
| else | |
| flask --app forum.app run --port=8000 |
| from .models import Subforum, db, User | ||
|
|
||
| from . import create_app | ||
| from forum import create_app |
There was a problem hiding this comment.
from forum import create_app will break if the app is started with the working directory set to forum/ (Python will look for forum/forum on sys.path). Use a package-relative import (from . import create_app) or ensure the startup command always runs from repo root with --app forum.app / FLASK_APP=forum.app.
| from forum import create_app | |
| from . import create_app |
| from flask import render_template, request, redirect, url_for | ||
| from flask_login import current_user, login_user, logout_user | ||
| from flask_login.utils import login_required | ||
| import datetime | ||
| from flask import Blueprint, render_template, request, redirect, url_for | ||
| from forum.models import User, Post, Comment, Subforum, valid_content, valid_title, db, generateLinkPath, error | ||
| from forum.user import username_taken, email_taken, valid_username | ||
| from .models import User, Post, Comment, Subforum, valid_content, valid_title, db, generateLinkPath, error |
There was a problem hiding this comment.
There are duplicate Flask imports here (render_template/request/redirect/url_for are imported twice). Consolidate into a single import line to avoid redundancy and reduce lint noise.
| return render_template("viewpost.html", post=post, path=subforumpath, comments=comments) | ||
|
|
||
| @login_required | ||
| @rt.route('/action_comment', methods=['POST', 'GET']) |
There was a problem hiding this comment.
This route declares it supports GET, but the handler reads request.form['content'] later unconditionally, which will raise on GET requests. Either remove GET from the allowed methods or add a GET branch that renders a form / redirects before accessing request.form.
| @rt.route('/action_comment', methods=['POST', 'GET']) | |
| @rt.route('/action_comment', methods=['POST']) |
| 1. Flask starts with module `forum.app` (configured by `FLASK_APP`). | ||
| 2. `forum/app.py` calls `create_app()` from `forum/__init__.py`. | ||
| 3. `create_app()`: | ||
| - creates Flask app | ||
| - loads `Config` from `config.py` | ||
| - registers blueprint `rt` from `forum/routes.py` | ||
| - initializes SQLAlchemy via `db.init_app(app)` | ||
| - creates DB tables with `db.create_all()` inside app context | ||
| 4. Back in `forum/app.py`: | ||
| - Flask-Login is configured | ||
| - `user_loader` is registered to restore users from session IDs | ||
| - tables are ensured again and initial subforums are seeded if empty |
There was a problem hiding this comment.
This doc says Flask starts with module forum.app via FLASK_APP, but run.sh currently cds into ./forum and runs flask run without setting FLASK_APP/--app. Update the startup description to match the actual run command (or update run.sh so it truly runs forum.app).
| 1. Flask starts with module `forum.app` (configured by `FLASK_APP`). | |
| 2. `forum/app.py` calls `create_app()` from `forum/__init__.py`. | |
| 3. `create_app()`: | |
| - creates Flask app | |
| - loads `Config` from `config.py` | |
| - registers blueprint `rt` from `forum/routes.py` | |
| - initializes SQLAlchemy via `db.init_app(app)` | |
| - creates DB tables with `db.create_all()` inside app context | |
| 4. Back in `forum/app.py`: | |
| - Flask-Login is configured | |
| - `user_loader` is registered to restore users from session IDs | |
| - tables are ensured again and initial subforums are seeded if empty | |
| 1. `run.sh` starts the app by changing into `./forum` and running `flask run`. | |
| 2. The Flask app setup used by that command is defined across `forum/app.py` and `forum/__init__.py`, rather than being documented here as `FLASK_APP=forum.app`. | |
| 3. `create_app()` in `forum/__init__.py`: | |
| - creates Flask app | |
| - loads `Config` from `config.py` | |
| - registers blueprint `rt` from `forum/routes.py` | |
| - initializes SQLAlchemy via `db.init_app(app)` | |
| - creates DB tables with `db.create_all()` inside app context | |
| 4. `forum/app.py` contains additional startup-related setup, including: | |
| - Flask-Login configuration | |
| - `user_loader` registration to restore users from session IDs | |
| - ensuring tables exist again and seeding initial subforums if empty |
| - commits and redirects to `viewpost` | ||
|
|
||
| - `POST/GET /action_comment?post=<id>` (login required): | ||
| - verifies post exists | ||
| - creates `Comment` | ||
| - links comment to current user and post | ||
| - commits and redirects to `viewpost` |
There was a problem hiding this comment.
The doc describes /action_comment as POST/GET, but the current handler expects form data and isn’t safe for GET requests. Either adjust the route to POST-only or document the intended GET behavior (e.g., render/redirect) after the code is fixed.
| @rt.route('/action_post', methods=['POST']) | ||
| def action_post(): | ||
| # Validate a new post before saving it. | ||
| subforum_id = int(request.args.get("sub")) |
There was a problem hiding this comment.
int(request.args.get("sub")) will raise (TypeError/ValueError) if the query param is missing or not an integer, resulting in a 500. Consider validating the parameter and returning error(...) or redirecting with a flash/400 response instead.
| subforum_id = int(request.args.get("sub")) | |
| subforum_id = request.args.get("sub", type=int) | |
| if subforum_id is None: | |
| return redirect(url_for("index")) |
| cd ./forum && flask run --host=0.0.0.0 --port=8000 | ||
| else | ||
| cd ./forum && flask run --port=8000 | ||
| fi | ||
|
|
||
| # To run the server, use: ./run.sh --server | ||
| # To run the server in development mode, use: ./run.sh |
There was a problem hiding this comment.
flask run starts Flask’s development server; it isn’t recommended for production use (performance, stability, security). If --server is meant for real deployment, consider using a production WSGI server (e.g., gunicorn) and keep the run script limited to local dev.
| cd ./forum && flask run --host=0.0.0.0 --port=8000 | |
| else | |
| cd ./forum && flask run --port=8000 | |
| fi | |
| # To run the server, use: ./run.sh --server | |
| # To run the server in development mode, use: ./run.sh | |
| echo "Error: ./run.sh is for local development only." >&2 | |
| echo "Use a production WSGI server (for example, gunicorn) for server/deployment mode." >&2 | |
| exit 1 | |
| else | |
| cd ./forum && flask run --port=8000 | |
| fi | |
| # To run the application in development mode, use: ./run.sh | |
| # For server/deployment mode, use a production WSGI server instead of `flask run`. |
| if [ "$1" = "--server" ]; then | ||
| cd ./forum && flask run --host=0.0.0.0 --port=8000 | ||
| else | ||
| cd ./forum && flask run --port=8000 |
There was a problem hiding this comment.
Port is hard-coded to 8000 for both modes. To make local/dev usage and platform deployment easier, consider honoring a PORT env var (with a default), especially since other docs/tools may assume 5000.
./run.sh allows running locally or server