调整路由以符合 RESTful 规范,新增 API Token 撤销功能
This commit is contained in:
@@ -97,7 +97,7 @@ func (h *BatchHandler) ListBatches(c *gin.Context) {
|
||||
// @Produce json
|
||||
// @Success 200 {object} model.Response{data=model.FileBatch}
|
||||
// @Failure 404 {object} model.Response
|
||||
// @Router /admin/batch/{batch_id} [get]
|
||||
// @Router /admin/batches/{batch_id} [get]
|
||||
func (h *BatchHandler) GetBatch(c *gin.Context) {
|
||||
id := c.Param("batch_id")
|
||||
var batch model.FileBatch
|
||||
@@ -119,7 +119,7 @@ func (h *BatchHandler) GetBatch(c *gin.Context) {
|
||||
// @Param request body UpdateBatchRequest true "修改内容"
|
||||
// @Success 200 {object} model.Response{data=model.FileBatch}
|
||||
// @Failure 400 {object} model.Response
|
||||
// @Router /admin/batch/{batch_id} [put]
|
||||
// @Router /admin/batches/{batch_id} [put]
|
||||
func (h *BatchHandler) UpdateBatch(c *gin.Context) {
|
||||
id := c.Param("batch_id")
|
||||
var batch model.FileBatch
|
||||
@@ -159,7 +159,7 @@ func (h *BatchHandler) UpdateBatch(c *gin.Context) {
|
||||
// @Produce json
|
||||
// @Success 200 {object} model.Response
|
||||
// @Failure 500 {object} model.Response
|
||||
// @Router /admin/batch/{batch_id} [delete]
|
||||
// @Router /admin/batches/{batch_id} [delete]
|
||||
func (h *BatchHandler) DeleteBatch(c *gin.Context) {
|
||||
idStr := c.Param("batch_id")
|
||||
id, _ := strconv.ParseUint(idStr, 10, 32)
|
||||
|
||||
@@ -80,6 +80,16 @@ func (h *TokenHandler) CreateToken(c *gin.Context) {
|
||||
}))
|
||||
}
|
||||
|
||||
// RevokeToken 撤销 API Token
|
||||
// @Summary 撤销 API Token
|
||||
// @Description 将 API Token 标记为已撤销,使其失效但保留记录
|
||||
// @Tags Admin
|
||||
// @Security AdminAuth
|
||||
// @Param id path int true "Token ID"
|
||||
// @Produce json
|
||||
// @Success 200 {object} model.Response
|
||||
// @Failure 500 {object} model.Response
|
||||
// @Router /admin/api-tokens/{id}/revoke [post]
|
||||
func (h *TokenHandler) RevokeToken(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if err := bootstrap.DB.Model(&model.APIToken{}).Where("id = ?", id).Update("revoked", true).Error; err != nil {
|
||||
|
||||
@@ -26,7 +26,7 @@ func PickupRateLimit() gin.HandlerFunc {
|
||||
failureMutex.Unlock()
|
||||
|
||||
if exists && count >= config.GlobalConfig.Security.PickupFailLimit {
|
||||
c.JSON(http.StatusTooManyRequests, model.ErrorResponse(http.StatusTooManyRequests, "Too many failed attempts. Please try again later."))
|
||||
c.JSON(http.StatusTooManyRequests, model.ErrorResponse(model.CodeTooManyRequests, "Too many failed attempts. Please try again later."))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ type PickupResponse struct {
|
||||
// @Produce application/zip
|
||||
// @Success 200 {file} file
|
||||
// @Failure 404 {object} model.Response
|
||||
// @Router /api/download/batch/{pickup_code} [get]
|
||||
// @Router /api/batches/{pickup_code}/download [get]
|
||||
func (h *PickupHandler) DownloadBatch(c *gin.Context) {
|
||||
code := c.Param("pickup_code")
|
||||
batch, err := h.batchService.GetBatchByPickupCode(code)
|
||||
@@ -88,7 +88,7 @@ func NewPickupHandler() *PickupHandler {
|
||||
// @Param pickup_code path string true "取件码"
|
||||
// @Success 200 {object} model.Response{data=PickupResponse}
|
||||
// @Failure 404 {object} model.Response
|
||||
// @Router /api/pickup/{pickup_code} [get]
|
||||
// @Router /api/batches/{pickup_code} [get]
|
||||
func (h *PickupHandler) Pickup(c *gin.Context) {
|
||||
code := c.Param("pickup_code")
|
||||
if code == "" {
|
||||
@@ -124,7 +124,7 @@ func (h *PickupHandler) Pickup(c *gin.Context) {
|
||||
// @Success 200 {file} file
|
||||
// @Failure 404 {object} model.Response
|
||||
// @Failure 410 {object} model.Response
|
||||
// @Router /api/download/file/{file_id} [get]
|
||||
// @Router /api/files/{file_id}/download [get]
|
||||
func (h *PickupHandler) DownloadFile(c *gin.Context) {
|
||||
fileIDStr := c.Param("file_id")
|
||||
fileID, _ := strconv.ParseUint(fileIDStr, 10, 32)
|
||||
|
||||
@@ -41,7 +41,7 @@ type UploadResponse struct {
|
||||
// @Success 200 {object} model.Response{data=UploadResponse}
|
||||
// @Failure 400 {object} model.Response
|
||||
// @Failure 500 {object} model.Response
|
||||
// @Router /api/upload [post]
|
||||
// @Router /api/batches [post]
|
||||
func (h *UploadHandler) Upload(c *gin.Context) {
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
@@ -113,7 +113,7 @@ type UploadTextRequest struct {
|
||||
// @Success 200 {object} model.Response{data=UploadResponse}
|
||||
// @Failure 400 {object} model.Response
|
||||
// @Failure 500 {object} model.Response
|
||||
// @Router /api/upload/text [post]
|
||||
// @Router /api/batches/text [post]
|
||||
func (h *UploadHandler) UploadText(c *gin.Context) {
|
||||
var req UploadTextRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
|
||||
@@ -9,13 +9,14 @@ type Response struct {
|
||||
|
||||
// 错误码定义
|
||||
const (
|
||||
CodeSuccess = 200
|
||||
CodeBadRequest = 400
|
||||
CodeUnauthorized = 401
|
||||
CodeForbidden = 403
|
||||
CodeNotFound = 404
|
||||
CodeGone = 410
|
||||
CodeInternalError = 500
|
||||
CodeSuccess = 200
|
||||
CodeBadRequest = 400
|
||||
CodeUnauthorized = 401
|
||||
CodeForbidden = 403
|
||||
CodeNotFound = 404
|
||||
CodeGone = 410
|
||||
CodeInternalError = 500
|
||||
CodeTooManyRequests = 429
|
||||
)
|
||||
|
||||
// NewResponse 创建响应
|
||||
|
||||
Reference in New Issue
Block a user