wrightkhlebisol / Result-Note-API

Endpoint for Result Note
MIT License
1 stars 1 forks source link

Flask REST API Template

A basic template to help kickstart development of a pure Flask API. This template is completely front-end independent and leaves all decisions up to the developer. The template includes basic login functionality based on JWT checks. How this token is stored and sent to the API is entirely up to the developer.

Features

Application Structure

The API is divided in six blueprints auth, comments, errors, posts, tasks and users.

The auth blueprint is responsible for all the routes associated with user registration and authentication.

The comments blueprint is responsible for handeling all requests related to comments, suchs as GET, POST and DELETE requests.

The errors blueprint is used to catch all errors and return the correct information.

The posts blueprint, just like the comments one, is responsible for handeling all requests related to posts, suchs as GET, POST and DELETE requests.

The tasks blueprint is responsible for requests related to asynchronous background tasks. Such as launching, retrieving the status of a specific task and retrieving all completed tasks.

The users blueprint handles the user related requests. Currently there are two routes which return either information ahout the current user or a different user based on username.

Installation

Template and Dependencies

Virtual Environment Setup

It is preferred to create a virtual environment per project, rather then installing all dependencies of each of your projects system wide. Once you install virtual env, and move to your projects directory through your terminal, you can set up a virtual env with:

python3 -m venv .venv

Dependency installations

To install the necessary packages:

source venv/bin/activate
pip3 install -r requirements.txt

This will install the required packages within your venv.


Setting up a SQLite3 Database

Database migrations are handled through Flask's Migrate Package, which provides a wrapper around Alembic. Migrations are done for updating and creating necessary tables/entries in your database. Flask provides a neat way of handling these. The files generate by the migrations should be added to source control.

To setup a SQLite3 database for development (SQLite3 is not recommended for production, use something like PostgreSQL or MySQL) you navigate to the folder where flask_api_template.py is located and run:

export FLASK_APP=flask_api_template.py

then you need to initiate your database and the migration folder with the following commands:

flask db init
flask db migrate "Your message here"
flask db upgrade

Migrations

To make changes to the database structure you can also use the flask db commands:

export FLASK_APP=flask_api_template.py
flask db migrate -m "Your message here"
flask db upgrade

Running the Application

Once you have setup your database, you are ready to run the application. Assuming that you have exported your app's path by:

export FLASK_APP=flask_api_template.py

You can go ahead and run the application with a simple command:

flask run

PostgreSQL

TODO

Gunicorn

TODO

Conclusion

Hopefully this template will inspire you to use Flask for your future API projects. If you have any feedback please do let me know or feel free to fork and raise a PR. I'm actively trying to maintain this project so pull request are more than welcome.

Todo's and Improvements

Acknowledgements

Flask Mega Guide - Miguel Grinberg

Path to Frontend Github

ResultNote Frontend