Coder Social home page Coder Social logo

dydanz / flask-boilerplate Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 25 KB

Just Another Flask B̶o̶i̶l̶e̶r̶p̶l̶a̶t̶e̶ Project Skeleton - A (not) Complete Example

Python 100.00%
python3 flask python-flask-application flask-restplus flask-swagger flask-sqlalchemy sqlalchemy-database-migrations flask-security python alembic

flask-boilerplate's Introduction

This project might not a boilerplate, but you can find an example how to build small-to-medium (or maybe even large) scale flask-based application with few cases that might be needed further on you development process.

I ran this template for one of startup company in Indonesia and able to serve millions request per day.

You can implement Monorepo for multiple python-based projects with this schema, why? Facebook/Google uses a giant monorepo for their billions line of code and you can find another benefit using Monorepo at: https://gomonorepo.org/

Getting Started


1. Virtual Environment

First, I'd recommend you to use Virtual Env and install the necessary package specifically only used by ts project:

$ virtualenv venv; 
$ source venv/bin/activate 
(env) $ pip install -r requirements.txt
2. Setup The Database

Then create a new database, we're using Postgresql here as an example.

postgres=# CREATE USER admin WITH PASSWORD 'password';
postgres=# CREATE DATABASE marketplace;
postgres=# GRANT ALL PRIVILEGES ON DATABASE marketplace TO admin;
3. Migrate The Database Model

As you can find database model on the application, firstly you should init the alembic folder, generate a migration script and upgrade (commit database model changes) into your DBMS.

This kind of operation should be done if you have changes on your database model/schema. Otherwise, SQLAlchemy unable correctly data model mapping between you model-app-code and actual table on DBMS.

Create Alembic Versioning table on your db and folders on you project

(env) $ python db migrate init

Generate Migration Script

(env) $ python db migrate migrate

Apply the DB Model Changes

(env) $ python db migrate upgrade
4. Run The Unit Test

Writing proper Unit Test is one of important keys delivering clean working product, so here they are in Flask, you should NOT ignore this one before committing the code.

(env) $ python manage.py test
5. Flake8: Your Tool For Code Style Guide Enforcement

Flake8 is a code style checker - to beautify your code (and more readable!) - that can be integrated into your CI.

(env) $ flake8 path/to/code/to/check.py

or just type on your root project folder, it will read setup.cfg file

(env) $ flake8
6. Run The Server

You can setup and use IDE or use terminal console to run the server locally, with your virtual-environment activated:

(env) $ export FLASK_APP=manage.py;
(env) $ export FLASK_CONFIG=development;
(env) $ flask run

.. or as an alternative:

(env) $ python manage.py runserver

.. or use to enable gunicorn or similar lightweight web server gateway interface (WSGI).

(env) $ gunicorn --bind 0.0.0.0:5000 manage:app -w 4 --timeout 180

By default Local Server will run on http://localhost:5000 make sure the port not already in use.

6. Flask-Swagger

Swagger pages will automatically loaded and shown as your registered blueprint's version e.g. v1, v2 etc.

Check and Test APIs through Swagger HERE.

Before Testing the APIs, please run this command to create a new User as John Doe

(env) $ python manage.py initdb

Test new user John Doe by using this bash shell command

(env) $ curl -X POST "http://localhost:5000/api/v1/user/auth/login" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"username\": \"john_doe_1946\", \"password\": \"this15secret\"}"

Expected will return a response as

{
  "token": "eyJhbGciOiJIUzI1NiIsImlhdCI6MTU1MjEyMTc1NiwiZXhwIjoxNTU0NTQwOTU2fQ.eyJzZXNzaW9uX2lkIjoiNmM1MzY2MDkiLCJ1c2VybmFtZSI6ImpvaG5fZG9lXzE5NDYifQ.TvQn76Ek7sPCLHS4hxMuk3XuQzvOt_pWL5w3_I84mvc",
  "username": "john_doe_1946"
}

Further Info? Contact me at Linkedin

flask-boilerplate's People

Contributors

dydanz avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

flask-boilerplate's Issues

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.