option apikey
This commit is contained in:
parent
9c1512889c
commit
9211bcdc50
12
Dockerfile
12
Dockerfile
@ -2,16 +2,18 @@ FROM python:3.9-slim
|
|||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
# 安装 git 用于拉取最新代码
|
||||||
|
RUN apt-get update && apt-get install -y git && apt-get clean
|
||||||
|
|
||||||
|
# 拉取最新代码(请确保仓库为public,否则需要处理身份验证)
|
||||||
|
RUN git clone https://github.com/nmhjklnm/sms_server.git .
|
||||||
|
|
||||||
# 安装依赖
|
# 安装依赖
|
||||||
COPY requirements.txt .
|
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
# 创建数据目录
|
# 创建数据目录
|
||||||
RUN mkdir -p /app/data
|
RUN mkdir -p /app/data
|
||||||
|
|
||||||
# 复制应用代码
|
|
||||||
COPY . .
|
|
||||||
|
|
||||||
# 确保数据目录有正确的权限
|
# 确保数据目录有正确的权限
|
||||||
RUN chmod -R 755 /app/data
|
RUN chmod -R 755 /app/data
|
||||||
|
|
||||||
@ -19,4 +21,4 @@ RUN chmod -R 755 /app/data
|
|||||||
EXPOSE 8322
|
EXPOSE 8322
|
||||||
|
|
||||||
# 运行应用
|
# 运行应用
|
||||||
CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8322"]
|
CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8322"]
|
@ -4,8 +4,8 @@ from pydantic_settings import BaseSettings
|
|||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
# 基本配置
|
# 基本配置
|
||||||
database_url: str = os.getenv("DATABASE_URL", "sqlite:///sms_database.db")
|
database_url: str = os.getenv("DATABASE_URL", "sqlite:///data/sms_database.db")
|
||||||
api_key: str = os.getenv("API_KEY", "sk-nmhjklnm")
|
api_key: Optional[str] = os.getenv("API_KEY", None)
|
||||||
host: str = os.getenv("HOST", "0.0.0.0")
|
host: str = os.getenv("HOST", "0.0.0.0")
|
||||||
port: int = int(os.getenv("PORT", "8322"))
|
port: int = int(os.getenv("PORT", "8322"))
|
||||||
log_level: str = os.getenv("LOG_LEVEL", "info").lower()
|
log_level: str = os.getenv("LOG_LEVEL", "info").lower()
|
||||||
@ -17,6 +17,11 @@ class Settings(BaseSettings):
|
|||||||
app_title: str = "SMS 验证码接收服务"
|
app_title: str = "SMS 验证码接收服务"
|
||||||
app_version: str = "1.0"
|
app_version: str = "1.0"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def requires_api_key(self) -> bool:
|
||||||
|
"""检查是否需要API密钥验证"""
|
||||||
|
return self.api_key is not None and self.api_key.strip() != ""
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
env_file = ".env"
|
env_file = ".env"
|
||||||
case_sensitive = False
|
case_sensitive = False
|
||||||
|
@ -18,8 +18,12 @@ router = APIRouter(prefix="/v1/sms", tags=["sms"])
|
|||||||
|
|
||||||
# API KEY 校验
|
# API KEY 校验
|
||||||
def check_api_key(x_api_key: Optional[str] = Header(None)):
|
def check_api_key(x_api_key: Optional[str] = Header(None)):
|
||||||
if (x_api_key != settings.api_key):
|
# 如果设置了API_KEY,则进行验证
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
if settings.requires_api_key:
|
||||||
|
if (x_api_key != settings.api_key):
|
||||||
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
# 如果没有设置API_KEY,则跳过验证
|
||||||
|
return True
|
||||||
|
|
||||||
# 接收短信接口
|
# 接收短信接口
|
||||||
@router.post("/receive")
|
@router.post("/receive")
|
||||||
|
9
sms_service.sh
Normal file → Executable file
9
sms_service.sh
Normal file → Executable file
@ -1,9 +1,9 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# 确保脚本以root权限运行
|
# 确保脚本以root权限运行
|
||||||
if [ "$(id -u)" -ne 0; then
|
if [ "$(id -u)" -ne 0 ]; then
|
||||||
echo "请使用sudo运行此脚本"
|
echo "请使用sudo运行此脚本"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 创建systemd服务文件
|
# 创建systemd服务文件
|
||||||
@ -16,6 +16,7 @@ After=network.target
|
|||||||
User=root
|
User=root
|
||||||
WorkingDirectory=/root/project/sms_server
|
WorkingDirectory=/root/project/sms_server
|
||||||
EnvironmentFile=-/root/project/sms_server/.env
|
EnvironmentFile=-/root/project/sms_server/.env
|
||||||
|
Environment="PYTHONPATH=."
|
||||||
ExecStart=/bin/bash -c "cd /root/project/sms_server && /usr/bin/poetry run python backend/main.py"
|
ExecStart=/bin/bash -c "cd /root/project/sms_server && /usr/bin/poetry run python backend/main.py"
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=5
|
RestartSec=5
|
||||||
@ -36,4 +37,4 @@ systemctl start sms-webhook.service
|
|||||||
echo "SMS Webhook服务已安装并启动"
|
echo "SMS Webhook服务已安装并启动"
|
||||||
echo "查看状态: systemctl status sms-webhook.service"
|
echo "查看状态: systemctl status sms-webhook.service"
|
||||||
echo "查看日志: journalctl -u sms-webhook.service"
|
echo "查看日志: journalctl -u sms-webhook.service"
|
||||||
echo "访问服务: http://your-server-ip:$(grep -oP 'PORT=\K[0-9]+' /root/project/sms_server/.env 2>/dev/null || echo 8322)"
|
echo "访问服务: http://47.97.31.90:$(grep -oP 'PORT=\K[0-9]+' /root/project/sms_server/.env 2>/dev/null || echo 8322)"
|
Loading…
x
Reference in New Issue
Block a user