Introduction
-
Flask is very flexible, but also very difficult to control. How to combine flask with the functions of daily use is a problem that needs to be considered. Here are some solutions
- Complete docker+docker-compose+ci/cd process One-click deployment of hot update code to cloud hosts
- flask cli manages all commands and runs scripts through FlaskGroup
- Flask factory mode combined with celery to create asynchronous task queue
- flask+redis configuration cache
- Automatically load configuration from .env files
- Flask automatically refreshes js and css files every time it loads, improving development efficiency
- Configure sentry automatic monitoring and ignore log settings
- flask-env loads configuration from .env files
- flask+pytest for unit testing, configuration database
-
Deployment is often a problem encountered by new handwritten programs. With docker, it is much more convenient. But every time you update your own code, it will take some time to republish it to the server. Here is a solution:
- The docker-compose container arranges its own code, such as nginx container + mysql + redis and so on. How to solve the interdependence of multiple services.
- The gitlab ci/cd process performs static checks and integration tests on the entire code. and continuous release. After each git push, the code can be deployed to the server with one click.
github address link
If it is helpful to you, I hope to star
ci/cd continuous integration & continuous deployment
Complete the continuous release of personal projects through gitlab ci+docker-compose
docker-compose: nginx+mysql+redis run the program in the container through Dockerfile
to ensure that every time the code is pushed, the online code can be updated with one click.
ssh is asymmetric encryption, you need to generate a public key and a private key on this machine, and then give the public key to the machine that needs to log in. Then store the private key
in gitlab ci as variable information.
test
Tested with docker images and dependencies in gitlab runner. Run flak8 and pytest unit tests.
deploy
Update the code under the server through the ssh command, and then docker-compose rebuild
automatically triggers the test task and manually triggers the deploy task every time the code is pushed to gitlab.
celery
When create_app, load update_celery method. Update the config and place the task under the context
to create a new run_celery file:
from jasmine_app import create_app
from jasmine_app import celery
app = create_app()
app.app_context().push()
run
celery worker -B -A run_celery.celery --loglevel=info
Publish the image to docker.io
docker build --cache-from jasmine:latest -t jasmine:latest .
docker tag jasmine:latest fjl2401/jasmine
docker push fjl2401/jasmine
flask-env
Installation: pip install python-dotenv
Add the .env file to the root directory Add configuration to the .env file
- FLASK_DEBUG=True or False
- FLASK_APP=jasmin_app
- FLASK_ENV=development
Configure Sentry
You need to go to sentry to register and generate a secret key, and the official documentation is also quite good. The method of shielding error information is mainly given.
There are two ways
- raven
- sentry_sdk
pip install --upgrade sentry-sdk[flask]==0.5.5
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
sentry_sdk.init(
dsn="https://[email protected]/1327554",
integrations=[FlaskIntegration()]
)
flask+commands
When you need to run the script, you can put the script file under the commands for processing.
Customize the cli through AppGroup, and then import_string() to load the commands into it.
# extension文件中
usr_cli = AppGroup("user")
import_string("play_flask.single_app.command")
# 写在commands文件夹中
@usr_cli.command("create_user")
def create_user():
"""
create user
"""
print("create user {}".format("cli"))
print(current_app.config)
# 在init_app时
app.cli.add_command(usr_cli)
gunicorn run
create_app in app and run
gunicorn --bind 0.0.0.0