初始化
This commit is contained in:
5
app/api/__init__.py
Normal file
5
app/api/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from .account import router as account_router
|
||||
from .database import router as database_router
|
||||
from .task import router as task_router
|
||||
|
||||
__all__ = ["account_router", "database_router", "task_router"]
|
||||
BIN
app/api/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
app/api/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
BIN
app/api/__pycache__/account.cpython-312.pyc
Normal file
BIN
app/api/__pycache__/account.cpython-312.pyc
Normal file
Binary file not shown.
BIN
app/api/__pycache__/database.cpython-312.pyc
Normal file
BIN
app/api/__pycache__/database.cpython-312.pyc
Normal file
Binary file not shown.
BIN
app/api/__pycache__/task.cpython-312.pyc
Normal file
BIN
app/api/__pycache__/task.cpython-312.pyc
Normal file
Binary file not shown.
60
app/api/account.py
Normal file
60
app/api/account.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
from ..core.database import get_db
|
||||
from ..schemas.account import (
|
||||
AccountCreate, AccountUpdate, AccountResponse,
|
||||
AccountListRequest, AccountGetRequest, AccountUpdateRequest, AccountDeleteRequest
|
||||
)
|
||||
from ..services.account import AccountService
|
||||
|
||||
router = APIRouter(prefix="/accounts", tags=["账号管理"])
|
||||
|
||||
@router.post("/create", response_model=AccountResponse, status_code=status.HTTP_201_CREATED)
|
||||
def create_account(account: AccountCreate, db: Session = Depends(get_db)):
|
||||
"""创建账号"""
|
||||
# 检查账号是否已存在
|
||||
existing_account = AccountService.get_account_by_account(db, account.account)
|
||||
if existing_account:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="账号已存在"
|
||||
)
|
||||
|
||||
return AccountService.create_account(db, account)
|
||||
|
||||
@router.post("/list", response_model=List[AccountResponse])
|
||||
def get_accounts(request: AccountListRequest, db: Session = Depends(get_db)):
|
||||
"""获取账号列表"""
|
||||
return AccountService.get_accounts(db, skip=request.skip, limit=request.limit)
|
||||
|
||||
@router.post("/get", response_model=AccountResponse)
|
||||
def get_account(request: AccountGetRequest, db: Session = Depends(get_db)):
|
||||
"""根据ID获取账号"""
|
||||
account = AccountService.get_account(db, request.account_id)
|
||||
if not account:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="账号不存在"
|
||||
)
|
||||
return account
|
||||
|
||||
@router.post("/update", response_model=AccountResponse)
|
||||
def update_account(request: AccountUpdateRequest, db: Session = Depends(get_db)):
|
||||
"""更新账号"""
|
||||
account = AccountService.update_account(db, request.account_id, request.account_data)
|
||||
if not account:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="账号不存在"
|
||||
)
|
||||
return account
|
||||
|
||||
@router.post("/delete", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_account(request: AccountDeleteRequest, db: Session = Depends(get_db)):
|
||||
"""删除账号"""
|
||||
if not AccountService.delete_account(db, request.account_id):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="账号不存在"
|
||||
)
|
||||
56
app/api/database.py
Normal file
56
app/api/database.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
from ..core.database import get_db
|
||||
from ..schemas.database import (
|
||||
SQLExecuteRequest, SQLExecuteResponse, TableDataRequest,
|
||||
TableDataResponse, CreateTableRequest, ImportDataRequest
|
||||
)
|
||||
from ..services.database import DatabaseService
|
||||
|
||||
router = APIRouter(prefix="/database", tags=["数据库管理"])
|
||||
|
||||
@router.post("/execute", response_model=SQLExecuteResponse)
|
||||
def execute_sql(request: SQLExecuteRequest, db: Session = Depends(get_db)):
|
||||
"""执行SQL语句"""
|
||||
result = DatabaseService.execute_sql(db, request.sql)
|
||||
return SQLExecuteResponse(**result)
|
||||
|
||||
@router.post("/table-data", response_model=TableDataResponse)
|
||||
def get_table_data(request: TableDataRequest, db: Session = Depends(get_db)):
|
||||
"""获取表数据"""
|
||||
result = DatabaseService.get_table_data(
|
||||
db,
|
||||
request.table_name,
|
||||
request.limit or 100,
|
||||
request.offset or 0
|
||||
)
|
||||
return TableDataResponse(**result)
|
||||
|
||||
@router.post("/create-table", response_model=SQLExecuteResponse)
|
||||
def create_table(request: CreateTableRequest, db: Session = Depends(get_db)):
|
||||
"""创建表"""
|
||||
result = DatabaseService.create_table(
|
||||
db,
|
||||
request.table_name,
|
||||
request.columns,
|
||||
request.primary_key
|
||||
)
|
||||
return SQLExecuteResponse(**result)
|
||||
|
||||
@router.delete("/drop-table/{table_name}", response_model=SQLExecuteResponse)
|
||||
def drop_table(table_name: str, db: Session = Depends(get_db)):
|
||||
"""删除表"""
|
||||
result = DatabaseService.drop_table(db, table_name)
|
||||
return SQLExecuteResponse(**result)
|
||||
|
||||
@router.post("/import-data", response_model=SQLExecuteResponse)
|
||||
def import_data(request: ImportDataRequest, db: Session = Depends(get_db)):
|
||||
"""导入数据"""
|
||||
result = DatabaseService.import_data(db, request.table_name, request.data)
|
||||
return SQLExecuteResponse(**result)
|
||||
|
||||
@router.get("/tables", response_model=List[str])
|
||||
def get_table_list():
|
||||
"""获取所有表名"""
|
||||
return DatabaseService.get_table_list()
|
||||
187
app/api/task.py
Normal file
187
app/api/task.py
Normal file
@@ -0,0 +1,187 @@
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from typing import List
|
||||
from ..schemas.task import (
|
||||
JobResponse, AddCronJobRequest, AddIntervalJobRequest,
|
||||
AddDateJobRequest, TaskResponse
|
||||
)
|
||||
from ..utils.scheduler import task_scheduler, example_task, database_cleanup_task
|
||||
|
||||
router = APIRouter(prefix="/tasks", tags=["定时任务管理"])
|
||||
|
||||
# 可用的任务函数映射
|
||||
AVAILABLE_FUNCTIONS = {
|
||||
"example_task": example_task,
|
||||
"database_cleanup_task": database_cleanup_task,
|
||||
}
|
||||
|
||||
@router.post("/cron", response_model=TaskResponse)
|
||||
def add_cron_job(request: AddCronJobRequest):
|
||||
"""添加cron定时任务"""
|
||||
try:
|
||||
if request.func_name not in AVAILABLE_FUNCTIONS:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"函数 {request.func_name} 不可用"
|
||||
)
|
||||
|
||||
func = AVAILABLE_FUNCTIONS[request.func_name]
|
||||
|
||||
# 构建cron参数
|
||||
cron_kwargs = {}
|
||||
if request.year is not None:
|
||||
cron_kwargs['year'] = request.year
|
||||
if request.month is not None:
|
||||
cron_kwargs['month'] = request.month
|
||||
if request.day is not None:
|
||||
cron_kwargs['day'] = request.day
|
||||
if request.week is not None:
|
||||
cron_kwargs['week'] = request.week
|
||||
if request.day_of_week is not None:
|
||||
cron_kwargs['day_of_week'] = request.day_of_week
|
||||
if request.hour is not None:
|
||||
cron_kwargs['hour'] = request.hour
|
||||
if request.minute is not None:
|
||||
cron_kwargs['minute'] = request.minute
|
||||
if request.second is not None:
|
||||
cron_kwargs['second'] = request.second
|
||||
|
||||
job = task_scheduler.add_cron_job(func, request.job_id, **cron_kwargs)
|
||||
|
||||
return TaskResponse(
|
||||
success=True,
|
||||
message=f"Cron任务 {request.job_id} 添加成功",
|
||||
data={"job_id": job.id, "next_run": str(job.next_run_time)}
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
return TaskResponse(
|
||||
success=False,
|
||||
message=f"添加Cron任务失败: {str(e)}"
|
||||
)
|
||||
|
||||
@router.post("/interval", response_model=TaskResponse)
|
||||
def add_interval_job(request: AddIntervalJobRequest):
|
||||
"""添加间隔执行任务"""
|
||||
try:
|
||||
if request.func_name not in AVAILABLE_FUNCTIONS:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"函数 {request.func_name} 不可用"
|
||||
)
|
||||
|
||||
func = AVAILABLE_FUNCTIONS[request.func_name]
|
||||
|
||||
# 构建interval参数
|
||||
interval_kwargs = {}
|
||||
if request.seconds is not None:
|
||||
interval_kwargs['seconds'] = request.seconds
|
||||
if request.minutes is not None:
|
||||
interval_kwargs['minutes'] = request.minutes
|
||||
if request.hours is not None:
|
||||
interval_kwargs['hours'] = request.hours
|
||||
if request.days is not None:
|
||||
interval_kwargs['days'] = request.days
|
||||
|
||||
job = task_scheduler.add_interval_job(func, request.job_id, **interval_kwargs)
|
||||
|
||||
return TaskResponse(
|
||||
success=True,
|
||||
message=f"间隔任务 {request.job_id} 添加成功",
|
||||
data={"job_id": job.id, "next_run": str(job.next_run_time)}
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
return TaskResponse(
|
||||
success=False,
|
||||
message=f"添加间隔任务失败: {str(e)}"
|
||||
)
|
||||
|
||||
@router.post("/date", response_model=TaskResponse)
|
||||
def add_date_job(request: AddDateJobRequest):
|
||||
"""添加指定时间执行任务"""
|
||||
try:
|
||||
if request.func_name not in AVAILABLE_FUNCTIONS:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"函数 {request.func_name} 不可用"
|
||||
)
|
||||
|
||||
func = AVAILABLE_FUNCTIONS[request.func_name]
|
||||
job = task_scheduler.add_date_job(func, request.job_id, run_date=request.run_date)
|
||||
|
||||
return TaskResponse(
|
||||
success=True,
|
||||
message=f"定时任务 {request.job_id} 添加成功",
|
||||
data={"job_id": job.id, "run_date": str(job.next_run_time)}
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
return TaskResponse(
|
||||
success=False,
|
||||
message=f"添加定时任务失败: {str(e)}"
|
||||
)
|
||||
|
||||
@router.get("/", response_model=List[JobResponse])
|
||||
def get_jobs():
|
||||
"""获取所有任务"""
|
||||
jobs = task_scheduler.get_jobs()
|
||||
result = []
|
||||
for job in jobs:
|
||||
result.append(JobResponse(
|
||||
id=job.id,
|
||||
name=job.name,
|
||||
func=str(job.func),
|
||||
trigger=str(job.trigger),
|
||||
next_run_time=job.next_run_time
|
||||
))
|
||||
return result
|
||||
|
||||
@router.delete("/{job_id}", response_model=TaskResponse)
|
||||
def remove_job(job_id: str):
|
||||
"""删除任务"""
|
||||
success = task_scheduler.remove_job(job_id)
|
||||
if success:
|
||||
return TaskResponse(
|
||||
success=True,
|
||||
message=f"任务 {job_id} 删除成功"
|
||||
)
|
||||
else:
|
||||
return TaskResponse(
|
||||
success=False,
|
||||
message=f"删除任务 {job_id} 失败"
|
||||
)
|
||||
|
||||
@router.put("/{job_id}/pause", response_model=TaskResponse)
|
||||
def pause_job(job_id: str):
|
||||
"""暂停任务"""
|
||||
success = task_scheduler.pause_job(job_id)
|
||||
if success:
|
||||
return TaskResponse(
|
||||
success=True,
|
||||
message=f"任务 {job_id} 已暂停"
|
||||
)
|
||||
else:
|
||||
return TaskResponse(
|
||||
success=False,
|
||||
message=f"暂停任务 {job_id} 失败"
|
||||
)
|
||||
|
||||
@router.put("/{job_id}/resume", response_model=TaskResponse)
|
||||
def resume_job(job_id: str):
|
||||
"""恢复任务"""
|
||||
success = task_scheduler.resume_job(job_id)
|
||||
if success:
|
||||
return TaskResponse(
|
||||
success=True,
|
||||
message=f"任务 {job_id} 已恢复"
|
||||
)
|
||||
else:
|
||||
return TaskResponse(
|
||||
success=False,
|
||||
message=f"恢复任务 {job_id} 失败"
|
||||
)
|
||||
|
||||
@router.get("/functions", response_model=List[str])
|
||||
def get_available_functions():
|
||||
"""获取可用的任务函数列表"""
|
||||
return list(AVAILABLE_FUNCTIONS.keys())
|
||||
Reference in New Issue
Block a user