From 59d39de81e35c0023acf9477d34921d8890b63f8 Mon Sep 17 00:00:00 2001 From: MR-ZC <1475796375@qq.com> Date: Tue, 25 Mar 2025 15:09:18 +0800 Subject: [PATCH] init --- .env | 3 +++ .github/workflows/docker-build.yml | 29 +++++++++++++++++++++++ Dockerfile | 7 ++++++ main.py | 37 ++++++++++++++++++++++++++++++ requirements.txt | 3 +++ 5 files changed, 79 insertions(+) create mode 100644 .env create mode 100644 .github/workflows/docker-build.yml create mode 100644 Dockerfile create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.env b/.env new file mode 100644 index 0000000..e66c2db --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +FLASK_PORT=5000 +FLASK_HOST=0.0.0.0 +FLASK_ENV=development \ No newline at end of file diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml new file mode 100644 index 0000000..7091294 --- /dev/null +++ b/.github/workflows/docker-build.yml @@ -0,0 +1,29 @@ +name: 打包发布docker镜像 +on: + push: + branches: [ main ] + +jobs: + build-push: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v4 + + - name: Login to Docker Registry + uses: docker/login-action@v3 + with: + registry: your-registry.com # 替换为自建 Registry 的地址 + username: ${{ secrets.DOCKER_USER }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + file: ./Dockerfile # 指定 Dockerfile 路径 + tags: your-registry.com/your-image:latest # 镜像标签(包含 Registry 地址) + push: true \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..28da01c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3.10-alpine + +RUN pip install -r requirements.txt + +COPY . . + +CMD ["python", "main.py"] \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..56fe5df --- /dev/null +++ b/main.py @@ -0,0 +1,37 @@ +from flask import Flask, jsonify +import os +from dotenv import load_dotenv + +# 加载环境变量 +load_dotenv() + +app = Flask(__name__) + +@app.route('/') +def home(): + return jsonify({ + "message": "欢迎使用 Flask API", + "status": "running", + "version": "1.0.0" + }) + +@app.route('/health') +def health_check(): + return jsonify({ + "status": "healthy", + "timestamp": "2024-03-21" + }) + +@app.route('/info') +def info(): + return jsonify({ + "hostname": os.uname().nodename, + "python_version": os.sys.version, + "environment": os.getenv('FLASK_ENV', 'production'), + "port": os.getenv('FLASK_PORT', '5000') + }) + +if __name__ == '__main__': + port = int(os.getenv('FLASK_PORT', 5000)) + host = os.getenv('FLASK_HOST', '0.0.0.0') + app.run(host=host, port=port) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c043a1b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +flask==3.0.2 +gunicorn==21.2.0 +python-dotenv==1.0.1