Ilanak / mentors-backend

Backend server code for Baot mentorship application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mentors-backend

Backend server code for Baot mentorship application.

Deploy a Local Setup

Local Database

  1. Install Docker and verify installation.
  2. Install pgAdmin (or your favorite PostgreSQL GUI client).
  3. In the command line, tun docker run -p 5432:5432 --name baot-mentors -e POSTGRES_PASSWORD=somePassword -d postgres. baot-mentors is the docker container's name and can be changed freely.
  4. Connect to this DB using your PostgresSQL client with the following details:
    • host: localhost
    • port: 5432
    • username: postgres
    • password: somePassword

Local Server

  1. Clone this repository and install all dependencies using pip install -r requirements.txt. Consider using Python's virtual environment (venv) to prevent versioning conflicts.

    📣 If you're experiencing installation problems with psycopg2, try installing it separately using the command pip install psycopg2-binary.

  2. Set the following environment variables (you can do so in PyCharm's run configurations):

    • DATABASE_URL=postgresql://postgres:somePassword@localhost/postgres
    • SECRET_KEY=someSecretStringUsedByFlask
    • APP_SETTINGS=config.DevelopmentConfig
    • GOOGLE_APPLICATION_CREDENTIALS=secretKey - for prod use FIREBASE_CONFIG instead
  3. Run python reset_db.py to initialize the DB tables.

  4. Run python main_app.py and verify (using the console output) that the server is running.

  5. Browse to http://localhost:5000 in your browser and verify you get a Hello World page.

Project Structure

  • main_app.py: This file runs the application instance. Heroku knows to run this file as it is pointed to by Procfile (using gunicorn).
  • app.py - defines the application, its configuration and the DB.
  • config.py: declares different configuration schemes for the application. The configuration is set to an environment variable APP_SETTINGS.
  • models.py: defines the DB model. Each class is a DB table. Refer to Peewee ORM for more details and documentation.
  • views.py: This file contains all application routing. Namely, it is responsible for all exposed APIs.

Heroku Files

  • Procfile tells Heroku which process to run on initialization.
  • runtime.txt tells Heroku which Python version it should use.

DB Migrations

To perform changes on the DB, follow these guidelines:

  1. Backup the database's current state (you can do this manually from the database's resource page on Heroku).
  2. Edit migrate.py to perform the desired operations on the DB. Use Peewee's Schema Migrations documentation and examples.
  3. Make sure you edit models.py to reflect your new schema.
  4. Once the script is final (and tested locally! 😉), push it to the remote repository.
  5. Run it on the staging app with heroku run python migrate.py --app baot-mentors-stage (you'll need Heroku CLI).
  6. Do the same with production: heroku run python migrate.py --app baot-mentors-prod

To apply migrations to the local DB (and if you don't mind losing all local data) - simply run reset_db.py which will re-create the DB according to the schema in models.py.

Tips & Tricks 😄

  • heroku logs --app <app_name> to show recent logs (use after crash...).
  • Both staging and production applications deploy automatically from master.

Project Architecture

Project Architecture

About

Backend server code for Baot mentorship application


Languages

Language:Python 100.0%