Skip to content

Latest commit

 

History

History
286 lines (207 loc) · 5.08 KB

README.adoc

File metadata and controls

286 lines (207 loc) · 5.08 KB

Matcha

Frameworks

Frontend

Database

Backend

  • Flask as WSGI

  • psycopg as adapter between Python and PostgreSQL

  • black as code formatter

Backend

Structure

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:

Directory 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

app.py

from flask import Flask
from routes import init_routes

app = Flask(__name__)

# Initialize routes
init_routes(app)

if __name__ == "__main__":
    app.run(debug=True)

routes/init.py

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)

routes/home_routes.py

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()

routes/user_routes.py

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))

services/home_service.py

def get_welcome_message():
    return {"message": "Welcome to the API"}

services/user_service.py

def get_user(user_id):
    # Simulated user retrieval
    return {"user_id": user_id, "name": "User Name"}
SID

Session ID

We use Flask-SocketIO.

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.

Like

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",
}

Browsing

  • 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": []
}

Pictures

/api/modify-profile-picture

PUT to update the picture selected as profile picture.

GET or PUT return the URL of the profile picture

/api/pictures

POST for upload pictures

DELETE array of URL in the body

example of payload

{
  "url": ["https://.../2", "https://.../2"],
}

GET, DELETE or POST return the array of picture in URL format

Position

POST

On /api/postition example of payload

{
  "latitude": 46.532327,
  "longitude": 6.591987
}

Chat

GET

/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"
  }
]

POST

/api/chat

{
 "to": "other"
 "message": "Hi!"
}

the api return the current post message

{
  "date": 4,
  "sender": "edythe",
  "message": "fourth message"
}

DB

CREATE TABLE IF NOT EXISTS chat (
    id SERIAL PRIMARY KEY,
    id_sender INTEGER REFERENCES users (id) ON DELETE CASCADE,
    id_receiver INTEGER REFERENCES users (id) ON DELETE CASCADE,
    message TEXT NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT now()
);

Debug

JWT Error: Subject must be a string!

Fix with this comment

So quickfix is to add PyJWT==2.9.0 at the top of your requirements.txt file