This article will guide you to complete the building and deploying of a Flask back-end application from scratch using Zeabur CLI to Zeabur within 10 minutes.
Zeabur CLI offers command-line operations for Zeabur Dashboard, allowing developers to provide one-click deployment services for applications quickly without leaving the terminal. With just a few commands, the application can run in the cloud and be configured with a domain for access.
For technical details of Zeabur CLI, you can refer to this article.
You can refer to Install Zeabur CLI for installation according to the operating system.
For example, on the MacOS platform, execute the following command in the command line to complete the installation:
`brew install zeabur/tap/cli`
Here we use Flask and MySQL to build the backend application.
mkdir zeabur-cli-flask-demo
cd zeabur-cli-flask-demo
python -m venv myenv
source myenv/bin/activate
pip install flask Flask-SQLAlchemy
from flask import Flask, request, current_app
from flask_sqlalchemy import SQLAlchemy
import os
import logging
app = Flask(__name__)
db_url = os.environ.get('DATABASE_URL')
app.config['SQLALCHEMY_DATABASE_URI'] = db_url or 'mysql://username:password@localhost/db_name'
db = SQLAlchemy(app)
class Task(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(80), nullable=False)
@app.route('/tasks', methods=['POST'])
def add_task():
content = request.json['content']
task = Task(content=content)
db.session.add(task)
db.session.commit()
return {'id': task.id}
@app.route('/tasks', methods=['GET'])
def get_tasks():
tasks = Task.query.all()
return {'tasks': [{'id': task.id, 'content': task.content} for task in tasks]}
@app.route('/tasks/<int:id>', methods=['DELETE'])
def delete_task(id):
task = Task.query.get(id)
if task:
db.session.delete(task)
db.session.commit()
return {'result': 'success'}
return {'result': 'error', 'message': 'Task not found'}, 404
with app.app_context():
db.create_all()
if __name__ == '__main__':
app.run(debug=True, port=os.getenv("PORT", default=5000), host='0.0.0.0')
In general, backend application development requires testing of the database server. Traditional methods such as local running/ Docker deployment are relatively cumbersome. Zeabur provides a new way to quickly deploy database services:
DATABASE_URL=mysql://root:<MYSQL_PASSWORD>@sfo1.clusters.zeabur.com:<MYSQL_PORT>/zeabur python ./app.py
After successful operation, you can use debugging tools like Postman or Paw to test the API:
DATABASE_URL=mysql://${MYSQL_USERNAME}:${MYSQL_PASSWORD}@mysql.zeabur.internal:${MYSQL_PORT}/${MYSQL_DATABASE}
PORT=8080
The above environment variables MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_PORT, MYSQL_DATABASE are all system-generated injections, eliminating the trouble of manual filling. The PORT environment variable allows Zeabur to automatically recognize the web port and automatically bind public network access.
For the service to apply the environment variables, we should restart the service. Enter zeabur service restart in the terminal:
To access the web service on the public network, we still need to bind a domain name. Here we use *.zeabur.app to automatically generate a domain name as an example. Enter zeabur domain create in the terminal:
At this point, we have completed all the steps. Now we can use debugging tools like Postman or Paw to verify the deployment:
Using Zeabur CLI to deploy backend applications is a quick and comfortable process, which can greatly improve your efficiency in agile development, making testing and production deployment very convenient.