Heroku is a wonderful platform to host your web application which have every few traffic.
You only need to set up an git
repository and push it to Heroku
, then your web should be running!
Write your flask app and set every thing up!
# set up the heroku buildpack
cat > app.json << EOF
{
"name": "Lambda API",
"description": "API like Amazon Lambda calculation",
"image": "heroku/python",
"keywords": ["python", "flask" ]
}
EOF
# may need to specify the python runtime version
cat > runtime.txt << EOF
python-3.6.6
EOF
# point out the needed python packages
cat > requirements.txt << EOF
Flask==1.0.2
gunicorn==19.9.0
EOF
# configure the gunicorn as web server
cat > Procfile << EOF
web: gunicorn app:app
EOF
# the flask web app
mkdir app
cat > app/__init__.py << EOF
from flask import Flask, Response, jsonify
class JSONResponse(Response):
@classmethod
def force_type(cls, response, environ=None):
if isinstance(response, (list, dict, bool)):
response = jsonify(response)
return super(Response, cls).force_type(response, environ)
app = Flask(__name__)
app.response_class = JSONResponse
app.config['JSON_AS_ASCII'] = False
@app.route('/')
def hello_world():
return ['hello', 'world']
EOF
# flask app for local test
cat > run.py << EOF
#!/usr/bin/env python3
from app import app
if __name__ == '__main__':
app.run()
EOF
The Heroku
seems only support gunicorn
as web server, that is what Procfile
is for.
Set up the git repository
Assuming the app your created on Heroku
is named $APP
.
# set up the git repository
git init
git remote add heroku https://git.heroku.com/$APP .git
echo __* cache__ > .gitignore
git add .
git commit -m "initial"
# push to heroku
git push heroku master
Every thing is done!
Try curl https://$APP.herokuapp.com/
.