GithubHelp home page GithubHelp logo

flask_react_crud_jwt_backend's Introduction

Flask backend with PostgreSQL

The following instructions are for the MacOS environment, they might be slightly different on Windows/Linux.

1. Database Setup

1.1 pull postgres docker image and run

$ docker pull postgres
$ docker run -d \
        --name my-postgres \
        -p 5432:5432 \
        -e POSTGRES_PASSWORD=postgres \
        -v [host_src_path]:/var/lib/postgresql/data \
        postgres

1.2 run container bash, then run psql command as user postgres

$ docker exec -it my-postgres bash
# psql -U postgres

you will be presented with a psql terminal like this

psql (16.0 (Debian 16.0-1.pgdg120+1))
Type "help" for help.

postgres=# 

1.3 create database

postgres=# CREATE DATABASE testdb;

1.4 create role

postgres=# CREATE ROLE test WITH LOGIN PASSWORD ‘test’;

1.5 grant privileges to the role above

postgres=# \c testdb;
You are now connected to database "testdb" as user "postgres".
testdb=# grant all on schema public to test;
GRANT
testdb=# grant all privileges on database testdb to test;
GRANT
testdb=# \q

2. Flask Setup

2.1 Install python3 (don't install Python 3.12.x, psycopg2 isnt working in python 3.12.0 recently, not sure if it has been fixed or not)

$ python3 --version
Python 3.11.6

2.2 Create Python venv and activate it

$ cd [the project root folder]
$ python3 -m venv .venv
$ source ./.venv/bin/activate

2.3 Install python packages

### under the project folder ###
(.venv) $ pip install -r ./requirements.txt

2.4 create .env file and set environment variables

(.venv) $ nano flaskreact/.env

set the corresponding settings

DATABASE_URI='postgresql+psycopg2://test:test@localhost:5432/testdb'
DEVELOPMENT_SECRET_KEY='flask app secret key'              <- modify this
DEVELOPMENT_JWT_SECRET='flask-jwt-extended jwt secret key' <- modify this

2.5 run Flask-Migrate commands to create tables (a folder called migrations will be created)

### under the project root folder ###
(.venv) $ flask db init
(.venv) $ flask db migrate
(.venv) $ flask db upgrade

3. Start the local Flask server

### unser the project root folder ###
(.venv) $ flask run
or
(.venv) $ flask --app "flaskreact:create_app(mode='development')" run

if using gunicorn
(.venv) $ gunicorn -w 4 "flaskreact:create_app(mode='development')"
or create a wsgi.py
inside wsgi.py
from my_project import create_app

app = create_app()

(.venv) $ gunicorn -w 4 my_project.wsgi:app

you should see this after flask run

* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit

flask_react_crud_jwt_backend's People

Contributors

yurenke avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.