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
|
||||
|
||||
# 安装 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 mkdir -p /app/data
|
||||
|
||||
# 复制应用代码
|
||||
COPY . .
|
||||
|
||||
# 确保数据目录有正确的权限
|
||||
RUN chmod -R 755 /app/data
|
||||
|
||||
@ -19,4 +21,4 @@ RUN chmod -R 755 /app/data
|
||||
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):
|
||||
# 基本配置
|
||||
database_url: str = os.getenv("DATABASE_URL", "sqlite:///sms_database.db")
|
||||
api_key: str = os.getenv("API_KEY", "sk-nmhjklnm")
|
||||
database_url: str = os.getenv("DATABASE_URL", "sqlite:///data/sms_database.db")
|
||||
api_key: Optional[str] = os.getenv("API_KEY", None)
|
||||
host: str = os.getenv("HOST", "0.0.0.0")
|
||||
port: int = int(os.getenv("PORT", "8322"))
|
||||
log_level: str = os.getenv("LOG_LEVEL", "info").lower()
|
||||
@ -17,6 +17,11 @@ class Settings(BaseSettings):
|
||||
app_title: str = "SMS 验证码接收服务"
|
||||
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:
|
||||
env_file = ".env"
|
||||
case_sensitive = False
|
||||
|
@ -18,8 +18,12 @@ router = APIRouter(prefix="/v1/sms", tags=["sms"])
|
||||
|
||||
# API KEY 校验
|
||||
def check_api_key(x_api_key: Optional[str] = Header(None)):
|
||||
if (x_api_key != settings.api_key):
|
||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||
# 如果设置了API_KEY,则进行验证
|
||||
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")
|
||||
|
9
sms_service.sh
Normal file → Executable file
9
sms_service.sh
Normal file → Executable file
@ -1,9 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 确保脚本以root权限运行
|
||||
if [ "$(id -u)" -ne 0; then
|
||||
echo "请使用sudo运行此脚本"
|
||||
exit 1
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
echo "请使用sudo运行此脚本"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 创建systemd服务文件
|
||||
@ -16,6 +16,7 @@ After=network.target
|
||||
User=root
|
||||
WorkingDirectory=/root/project/sms_server
|
||||
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"
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
@ -36,4 +37,4 @@ systemctl start sms-webhook.service
|
||||
echo "SMS Webhook服务已安装并启动"
|
||||
echo "查看状态: systemctl status 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