A lightweight RESTful API template based on Flask, which supports Python 2.7 and 3.6+
This framework is designed with the Model-View-Controller pattern, hence api.lib
is the controller of this application.
Per name, ORM database models should be located under this directory. This project uses sqlalchemy
to implement models. For instance, api.models.your_app.hello
has a model Hello
which binds to database table hello
.
This directory hosts all API entries of the application. Upon starting, all files under api/views/*
are scanned for subclass of APIView
and to have those subclasses registered as API entries. E.g. api.views.your_app.hello.py
implements HelloWorldView
class which inherents the class APIView
and it shall be registered onto the entry point /api/v0.1/hello
Celery is used for implementing tasks in this project, and all tasks related code shall be located in api.tasks
.
Besides the running RESTful API, this project implements command line tools using Click and has them integrated in the flask app. Any functions in api.commands.*
decorated with @click.command()
is registered under its name in cabab-case as click command and they should be run like flask your-command-name
It is recommended to use pipenv
for environment configuration and management for this project. A virtualenv will be automatically created upon the initial activation of pipenv for the project directory.
pipenv shell
pipenv install
This will install requirements in Pipfile
A .env
file is provided for environmental variable settings which contains some settings for the project. Please make sure the SECRET_KEY
is set up properly before continue.
Before run the project, make sure you have a settings.py
under current directory and an example settings file is provided.
cp settings.example.py settings.py
To run the RESTful API, use the following command. The RESTful API will by default serve at http://127.0.0.1:5000/api/v0.1/
flask run
To run the celery tasks, use the following command.
celery -A celery_worker.celery worker --concurrency=2 -E -Q {queue name} --logfile={queue name}.log --pidfile={queue name}.pid -D
flask what-ever-command
First clone the project and to the following.
pipenv shell
pipenv install
cp settings.example.py settings.py
flask run