This is an minimal but opinionated boilerplate meant for building out simple REST APIs. It is primarily used at Hack4Impact UIUC. This app is written in Python 3.6 with Postgres 10 as the chosen data persistence. The default way to deploy it is with Heroku or Zeit now but you can deploy it with another service, like AWS, Google Cloud, or DigitalOcean with Gunicorn and Nginx, but instructions for that are not provided. Included are simple examples and instructions developing with or without Docker are provided. I've also written a blog post about using Docker based on this repository.
Documentation is located here. We use black for code formatting, and mypy for optional static typing.
The goal of this boilerplate is to allow developers to quickly write their API with code structured to best practices while giving them flexibility to easily add/change features. Here are the problems this is trying to solve:
Flask is too flexible. With Flask, you can write your application in any structure you like, even in one file. There are also a lot of different tutorials and guides providing different instructions & application structures to set up a Flask app with a database, confusing many newcomers about best practices.
Django and other Flask boilerplates are too heavy. Sometimes, I don't need a fully featured admin portal with Redis and an Email manager nor do I need templates. Many APIs and applications require the use of a database though. Thus, I've chosen Postgres because it is a battle-tested and reliable database used in many companies and we know that 99% of applications can easily be designed to use relational databases (especially the ones used at Hack4Impact).
Please Please PLEASE read the documentation if you don't understand something relevant to this boilerplate. Documentation is provided in the wiki page of this repository. I've also added comments with links to specific Flask Documentation to explain certain design choices and/or a specific Flask API (ex: test clients).
Here are some quickstart instructions, although I would look at the documentation for more details and other options of setting up your environment (e.g. full Docker setup, installed postgres instance, pipenv, etc).
First start a postgres docker container and persist the data with a volume flask-app-db
:
make start_dev_db
Another option is to create a postgres instance on a cloud service like elephantsql and connect it to this app. Remember to change the postgres url and don't hard code it in!
Then, start your virtual environment
$ pip3 install virtualenv
$ virtualenv venv
$ source venv/bin/activate
Now, install the python dependencies and run the server:
(venv) $ pip install -r requirements.txt
(venv) $ pip install -r requirements-dev.txt
(venv) $ python manage.py recreate_db
(venv) $ python manage.py runserver
To exit the virtual environment:
(venv) $ deactivate
$
For ease of setup, I have hard-coded postgres URLs for development and docker configurations. If you are using a separate postgres instance as mentioned above, do not hardcode the postgres url including the credentials to your code. Instead, create a file called creds.ini
in the same directory level as manage.py
and write something like this:
[pg_creds]
pg_url = postgresql://testusr:password@127.0.0.1:5432/testdb
Note: you will need to call api.core.get_pg_url
in the Config file.
For production, you should do something similar with the flask SECRET_KEY
.
I've created a makefile to make this entire process easier but purposely provided verbose instructions there to show you what is necessary to start this application. To do so:
$ make setup
If you like to destroy your docker postgres database and start over, run:
$ make recreate_db
This is under the assumption that you have only set up one postgres container that's linked to the flask-app-db
volume.
I would highly suggest reading the documentation for more details on setup.
You may use Heroku or Zeit Now and the instructions are defined in the wiki page. I would recommend Heroku. The easiest way to do so is to click the Heroku Deploy button. Remember, once you fork/copy this repo, you will need to change app.json
, especially the repository
key. Everything else should be fine.
api/views/
- Holds files that define your endpointsapi/models/
- Holds files that defines your database schemaapi/__init__.py
- What is initially ran when you start your applicationapi/utils.py
- utility functions and classes - explained hereapi/core.py
- includes core functionality including error handlers and loggerapi/wsgi.py
- app reference for gunicorntests/
- Folder holding testsconfig.py
- Provides Configuration for the application. There are two configurations: one for development and one for production using Heroku.manage.py
- Command line interface that allows you to perform common functions with a commandrequirements.txt
- A list of python package dependencies the application requiresruntime.txt
& Procfile
- configuration for HerokuDockerfile
- instructions for Docker to build the Flask appdocker-compose.yml
- config to setup this Flask app and a Databasemigrations/
- Holds migration files – doesn't exist until you python manage.py db init
if you decide to not use dockerIf you're annoyed by the pycache files
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
Feel free to contact me for questions and contributions are welcome :)
tk2@illinois.edu
flask url构建 by Greg Obinna 由格雷格·奥比纳(Greg Obinna) 如何为生产构建构建Flask-RESTPlus Web服务 (How to structure a Flask-RESTPlus web service for production builds) In this guide I’ll show you a step by step approach
by Timothy Ko 蒂莫西·高(Timothy Ko) Docker开发工作流程-Flask和Postgres指南 (Docker Development WorkFlow — a guide with Flask and Postgres) Docker, one of the latest crazes, is an amazing and powerful tool for pack
flask身份验证 This tutorial takes a test-first approach to implementing token-based authentication in a Flask app using JSON Web Tokens (JWTs). 本教程采用测试优先的方法,使用JSON Web令牌(JWT)在Flask应用中实现基于令牌的身份验证。 Updates:
问题内容: 在官方的快速入门中,建议在使用单个 模块 时使用: 2. …如果您使用的是单个模块(如本例所示),则应使用,因为取决于它是作为应用程序启动还是作为模块导入,其名称将有所不同(与实际导入名称不同)。… 但是,在他们的API文档中,当我的应用程序为 软件包 时,建议进行硬编码: 因此,您在此处提供的内容很重要。如果使用单个模块,则始终为正确的值。但是,如果您使用的是包,通常建议在其中硬编码
在前面,我们介绍了 REST Web 服务,并使用 Flask 提供服务。这里,我们使用第三方库 Flask-RESTful,它使得在 Flask 中提供 REST 服务变得更加简单。 安装 使用 pip 安装: $ pip install flask-restful 使用 下面我们主要使用官方文档的例子进行说明。 Hello World 我们先来看一个简单的例子。 # -*- coding: u
Bootstrap 是 Twitter 开源的一个 CSS/HTML 框架,它让 Web 开发变得更加迅速,简单。要想在我们的 Flask 应用中使用 Boostrap,有两种方案可供选择: 第 1 种,在我们的 Jinja 模板中直接引入 Bootstrap 层叠样式表 (CSS) 和 JavaScript 文件,比如 bootstrap.min.css,bootstrap.min.js; 第
在 Web 应用中,我们经常需要保护我们的 api,以避免非法访问。比如,只允许登录成功的用户发表评论等。Flask-HTTPAuth 扩展可以很好地对 HTTP 的请求进行认证,不依赖于 Cookie 和 Session。本文主要介绍两种认证的方式:基于密码和基于令牌 (token)。 安装 使用 pip 安装: $ pip install Flask-HTTPAuth 基于密码的认证 为了简化
假设你的 Web 服务对于某些请求比较耗时,而该请求的返回结果在较短的时间内(比如 5 分钟内)都是足够有效的,这时你能想到什么方法去改善这种状况呢?缓存?对,至少这是一种提高性能的最简单的方法。 Flask 本身不提供缓存功能,但是作为 Flask 核心的 Werkzeug 框架则提供了一个简单的缓存对象 SimpleCache,它将缓存项存放在 Python 解释器的内存中。使用 Simple
MongoDB 是一个文档型数据库,是 NoSQL (not only SQL) 的一种,具有灵活、易扩展等诸多优点,受到许多开发者的青睐。MongoEngine 是一个用来操作 MongoDB 的 ORM 框架,如果你不知道什么是 ORM,可以参考 Flask-SQLAlchemy 一节。在 Flask 中,我们可以直接使用 MongoEngine,也可使用 Flask-MongoEngine