Flask-RESTful provides the building blocks for creating a great REST API.
apns Gunicorn Flask + Gunicorn + Nginx 部署
You'll find the user guide and all documentation here
- Requirements
dp is an example Flask application illustrating some of my common practices
At the bare minimum you'll need the following for your development environment:
It is strongly recommended to also install and use the following tools:
The following assumes you have all of the recommended tools listed above installed.
$ git clone https://github.com/arvin-chou/mc.git
$ cd mc
$ apt-get install python3
$ apt-get install python3-pip
$ pip3 install virtualenv uwsgi
$ apt-get install python-software-properties
$ add-apt-repository ppa:gias-kay-lee/npm
$ apt-get update
$ apt-get install npm
$ apt-get install zsh
$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
$ wget https://gist.githubusercontent.com/phawk/b2b3f1e28f8ffc33b396/raw/41772b8df50395dff3f0281ccde4ab723f1c466e/.screenrc -O ~/.screenrc
$ LC_ALL=C virtualenv mc
$ source mc/bin/activate
$ pip uninstall werkzeug
$ pip install werkzeug==0.10
$ apt-get install build-essential curl git m4 ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev
$ git clone https://github.com/Homebrew/linuxbrew.git ~/.linuxbrew
$ export PATH="$HOME/.linuxbrew/bin:$PATH"
$ export MANPATH="$HOME/.linuxbrew/share/man:$MANPATH"
$ export INFOPATH="$HOME/.linuxbrew/share/info:$INFOPATH"
$ brew install libjpeg zlib
$ python3 ./setup.py install
$ pip install -r requirements.txt # if setup fail
# [How to install nginx 1.9.5 with HTTP2 support on Ubuntu 14.04 LTS](https://by-example.org/install-nginx-with-http2-support-on-ubuntu-14-04-lts/)
$ vim /etc/apt/sources.list
# add ```
deb http://nginx.org/packages/mainline/ubuntu trusty nginx
deb-src http://nginx.org/packages/mainline/ubuntu trusty nginx
```
$ wget -q -O- http://nginx.org/keys/nginx_signing.key | sudo apt-key add -
$ apt-get update
$ apt-get install nginx
# [How To Install MySQL on Ubuntu 14.04](https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-14-04)
$ wget http://dev.mysql.com/get/mysql-apt-config_0.6.0-1_all.deb
$ dpkg -i mysql-apt-config_0.6.0-1_all.deb
$ apt-get update
$ apt-get install mysql-server
```
// in mysql shell
mysql> create database Merchant;
mysql> grant all on Merchant.* to 'Merchant_user'@'localhost' identified by "password";
mysql> flush privileges;
// hack uft-8 default in my.cnf
[client]
default-character-set = utf8
[mysqld]
character-set-client-handshake=FALSE
init_connect = SET collation_connection = utf8_general_ci
init_connect = SET NAMES utf8
character-set-server = utf8
collation-server = utf8_general_ci
```
$ berks install
$ vagrant plugin install vagrant-berkshelf
$ vagrant up
$ alembic upgrade head
(mc) $ run.sh
$ celery -A mini-restful.tasks worker
9. Open http://localhost:5000
$ npm config set strict-ssl false
$ npm install apidoc
$ apt-get install nodejs-legacy
$ npm cache clean -f
$ npm install -g n
$ n stable
$ IP="localhost" ./scripts/genapidoc.sh # change to your target ip / dns
$ # open apidoc/index.html
- create dictionary under ${ROOT}/module and module name as
module name
, for example, we create policy under ${ROOT}/module/policy. - the module folder at least contains with three files: sql.py, model.py, init.py.
- sql.py was called by ${ROOT}/config/config.py when server runs and it could initialize database.
- model.py was called by run.py which could route to the model by path.
- init.py contains common variables such as table name, request/response json head used in model.py/sql.py/unittest
- add routing rules in run.py and request path to ${ROOT}/config/config.py
This application uses Alembic for database
migrations and schema management. Changes or additions to the application data
models will require the database be updated with the new tables and fields.
Additionally, ensure that any new models are imported into the consolidated
models file at overholt.models
. To generate a migration file based on the
current set of models run the following command:
$ alembic revision --autogenerate -m "<a description of what was modified>"
Review the resulting version file located in the alembic/versions
folder. If
the file is to your liking upgrade the database with the following command:
$ alembic upgrade head
For anything beyond this workflow please read the Alembic documentation.
Management commands can be listed with the following command:
$ python manage.py
These can sometimes be useful to manipulate data while debugging in the browser.
To run the tests use the following command:
$ EVN="settings/test.py" python3 ./setup.py test