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
For testing, we have a smtp server (MailHog) running as a container. His goal is to received outgoing email (smtp) add display them through a web interface (http://localhost:8025/).
The frontend ask a confirm code with a GET on /api/confirm
.
The mail is then confirmed after a GET on /api/confirm/correct jwt token
When the user lost his password, he can ask an email with a link to recreate a new password on the web app.
We can ask to reset password with a POST and a username on /api/reset-password
. That will send an email to the user.
payload example:
{
"username": "daphnee"
}
The user will receive a link on the frontend like this: https://frontend/reset-password/<jwt>;
.
The user complete a form where a new password is asked.
The frontend send the new password to the backend with a POST on https://backend/reset-password/<jwt>;
payload example:
{
"password": "1234"
}
/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"
}