To structure your Flask REST API with app.py
as the main entry point and separate files for endpoints and services, you can follow a modular design. Here’s an example structure:
project/ │ ├── app.py # Main entry point ├── routes/ │ ├── __init__.py # Initializes Blueprint │ ├── home_routes.py # Example routes file │ └── user_routes.py # Example routes file ├── services/ │ ├── __init__.py # Optional, for package initialization │ ├── home_service.py # Example service file │ └── user_service.py # Example service file └── requirements.txt # Dependencies
from flask import Flask from routes import init_routes app = Flask(__name__) # Initialize routes init_routes(app) if __name__ == "__main__": app.run(debug=True)
from flask import Blueprint from .home_routes import home_bp from .user_routes import user_bp def init_routes(app): app.register_blueprint(home_bp) app.register_blueprint(user_bp)
from flask import Blueprint from services.home_service import get_welcome_message home_bp = Blueprint('home', __name__) @home_bp.route("/") def home(): return get_welcome_message()
from flask import Blueprint, jsonify from services.user_service import get_user user_bp = Blueprint('user', __name__, url_prefix='/users') @user_bp.route("/<int:user_id>") def user(user_id): return jsonify(get_user(user_id))
- SID
-
Session ID
We use Flask-SocketIO.
Note
|
API Reference |
The goal of webSocket it to push notification to connected users, for example:
-
a user has liked his profile
-
he received a message
When a user log in, the frontend will connect to the backend through a webSocket. The backend will add the new connection into a array of connected user and create a session ID.
When Alice like Bob, Bob receive a toast notifiation through the web socket
Payload example for a like
{
"like": "user",
}
and for a dislike
{
"dislike": "user",
}
-
Max age gap 0 - 30 years
-
Max distance 0 - 100 km
-
Max fame gap 0 - 10 points
-
Interests: array of strings
example of payload
{
"ageGap": 27,
"fameGap": 0,
"distance": 0,
"interests": []
}
PUT to update the picture selected as profile picture.
GET or PUT return the URL of the profile picture
/api/chat/username
return an array
[
{
"date": 1,
"sender": "daphnee",
"message": "first message"
},
{
"date": 2,
"sender": "edythe",
"message": "second message"
},
{
"date": 3,
"sender": "edythe",
"message": "third message"
}
]
/api/chat
{
"to": "other"
"message": "Hi!"
}
the api return the current post message
{
"date": 4,
"sender": "edythe",
"message": "fourth message"
}