#!/bin/bash

# 1. 更新系统
sudo yum update -y

# 2. 安装 Python 3 和 pip（如果尚未安装）
sudo yum install -y epel-release
sudo yum install -y python3 python3-pip

# 3. 安装 Flask 依赖
sudo pip3 install flask requests

# 4. 下载 Python 脚本
curl -o /opt/http_service.py https://ping.aa1.cn/download/http_service/http_service.py

# 5. 创建一个服务启动脚本并在后台运行 Flask 应用
cat <<EOL > /etc/systemd/system/http_service.service
[Unit]
Description=Flask HTTP Service
After=network.target

[Service]
User=nobody
ExecStart=/usr/bin/python3 /opt/http_service.py
WorkingDirectory=/opt
Restart=always

[Install]
WantedBy=multi-user.target
EOL

# 6. 启动 Flask 服务
sudo systemctl daemon-reload
sudo systemctl enable http_service.service
sudo systemctl start http_service.service

# 7. 输出服务状态
sudo systemctl status http_service.service
