Skip to content

ncthuc/flask-api-boilerplate

Repository files navigation

FLASK RESTFUL API BOILERPLATE

Flask command line

List all commands

flask

Start api application

flask run

Config environment variables in .env file

  • Generate a good secret key:
python -c 'import os; print(os.urandom(24))'

More detail at: https://flask.palletsprojects.com/en/1.1.x/quickstart/#sessions

  • Create .env at project root folder with following content:
SECRET_KEY={generated secret}
DATABASE_URL=mysql+pymysql://user:password@host:port/database
TEST_DATABASE_URL=mysql+pymysql://user:password@host:port/database
ORIGIN_ALLOW_DOMAIN=*
DEPLOYMENT_ENV=dev
  • Available environments for DEPLOYMENT_ENV: dev | test | prod

Database migration

  1. Initiate a migration folder (once only)
flask db init
  1. Create migration script from detected changes in the model
flask db migrate --message 'initial database migration'
  1. Apply the migration script to the database
flask db upgrade

Viewing the app

Open the following url on your browser to view swagger documentation http://127.0.0.1:5000/

Contributing

If you want to contribute to this boilerplate, clone the repository and just start making pull requests.

Testing

PyCharm

  • Create a .env file in ./tests folder and config DATABASE_URL to your test database
  • Add a run/debug configuration: Python Tests > pytest
  • In Configuration tab, choose Scrip Path as Target and point to ./tests folder
  • Set working directory to ./tests
  • In EnvFile tab, Add ./tests/.env, or in Environment variables, add all necessary variables
  • Click run

Console

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published