From e7ec7c057623fd42143b92af30bdba8cabc7c0ca Mon Sep 17 00:00:00 2001 From: Kerwin Date: Wed, 16 Jul 2025 18:05:29 +0800 Subject: [PATCH] update: password --- db/converter/converter.go | 18 ++ db/converter/system_config_converter.go | 6 + db/dto/ready_resource.go | 18 ++ db/dto/system_config.go | 6 + db/dto/user.go | 5 + db/entity/system_config.go | 3 + db/repo/resource_repository.go | 44 +++ docs/docs.go | 379 ++++++++++++++++++++++++ docs/swagger.json | 355 ++++++++++++++++++++++ docs/swagger.yaml | 246 +++++++++++++++ go.mod | 57 ++-- go.sum | 107 +++++++ handlers/public_api_handler.go | 334 +++++++++++++++++++++ handlers/user_handler.go | 38 +++ main.go | 48 +++ middleware/public_api.go | 93 ++++++ migration_add_api_token.sql | 17 ++ web/components/BatchAddResource.vue | 77 ++++- web/components/SingleAddResource.vue | 10 +- web/composables/useApi.ts | 11 + web/pages/api-docs.vue | 371 +++++++++++++++++++++++ web/pages/index.vue | 45 ++- web/pages/system-config.vue | 67 ++++- web/pages/users.vue | 110 +++++++ 24 files changed, 2409 insertions(+), 56 deletions(-) create mode 100644 db/dto/ready_resource.go create mode 100644 docs/docs.go create mode 100644 docs/swagger.json create mode 100644 docs/swagger.yaml create mode 100644 handlers/public_api_handler.go create mode 100644 middleware/public_api.go create mode 100644 migration_add_api_token.sql create mode 100644 web/pages/api-docs.vue diff --git a/db/converter/converter.go b/db/converter/converter.go index 1a4283a..9ca439f 100644 --- a/db/converter/converter.go +++ b/db/converter/converter.go @@ -171,3 +171,21 @@ func ToReadyResourceResponseList(resources []entity.ReadyResource) []dto.ReadyRe } return responses } + +// RequestToReadyResource 将ReadyResourceRequest转换为ReadyResource实体 +func RequestToReadyResource(req *dto.ReadyResourceRequest) *entity.ReadyResource { + if req == nil { + return nil + } + + return &entity.ReadyResource{ + Title: &req.Title, + Description: req.Description, + URL: req.Url, + Category: req.Category, + Tags: req.Tags, + Img: req.Img, + Source: req.Source, + Extra: req.Extra, + } +} diff --git a/db/converter/system_config_converter.go b/db/converter/system_config_converter.go index 3d5ffd4..3bdc66b 100644 --- a/db/converter/system_config_converter.go +++ b/db/converter/system_config_converter.go @@ -31,6 +31,9 @@ func SystemConfigToResponse(config *entity.SystemConfig) *dto.SystemConfigRespon AutoTransferEnabled: config.AutoTransferEnabled, AutoFetchHotDramaEnabled: config.AutoFetchHotDramaEnabled, + // API配置 + ApiToken: config.ApiToken, + // 其他配置 PageSize: config.PageSize, MaintenanceMode: config.MaintenanceMode, @@ -57,6 +60,9 @@ func RequestToSystemConfig(req *dto.SystemConfigRequest) *entity.SystemConfig { AutoTransferEnabled: req.AutoTransferEnabled, AutoFetchHotDramaEnabled: req.AutoFetchHotDramaEnabled, + // API配置 + ApiToken: req.ApiToken, + // 其他配置 PageSize: req.PageSize, MaintenanceMode: req.MaintenanceMode, diff --git a/db/dto/ready_resource.go b/db/dto/ready_resource.go new file mode 100644 index 0000000..d9972bc --- /dev/null +++ b/db/dto/ready_resource.go @@ -0,0 +1,18 @@ +package dto + +// ReadyResourceRequest 待处理资源请求 +type ReadyResourceRequest struct { + Title string `json:"title" validate:"required" example:"示例资源标题"` + Description string `json:"description" example:"这是一个示例资源描述"` + Url string `json:"url" validate:"required" example:"https://example.com/resource"` + Category string `json:"category" example:"示例分类"` + Tags string `json:"tags" example:"标签1,标签2"` + Img string `json:"img" example:"https://example.com/image.jpg"` + Source string `json:"source" example:"数据来源"` + Extra string `json:"extra" example:"额外信息"` +} + +// BatchReadyResourceRequest 批量待处理资源请求 +type BatchReadyResourceRequest struct { + Resources []ReadyResourceRequest `json:"resources" validate:"required"` +} diff --git a/db/dto/system_config.go b/db/dto/system_config.go index 6540d05..5f200f4 100644 --- a/db/dto/system_config.go +++ b/db/dto/system_config.go @@ -15,6 +15,9 @@ type SystemConfigRequest struct { AutoTransferEnabled bool `json:"auto_transfer_enabled"` // 开启自动转存 AutoFetchHotDramaEnabled bool `json:"auto_fetch_hot_drama_enabled"` // 自动拉取热播剧名字 + // API配置 + ApiToken string `json:"api_token"` // 公开API访问令牌 + // 其他配置 PageSize int `json:"page_size" validate:"min=10,max=500"` MaintenanceMode bool `json:"maintenance_mode"` @@ -39,6 +42,9 @@ type SystemConfigResponse struct { AutoTransferEnabled bool `json:"auto_transfer_enabled"` // 开启自动转存 AutoFetchHotDramaEnabled bool `json:"auto_fetch_hot_drama_enabled"` // 自动拉取热播剧名字 + // API配置 + ApiToken string `json:"api_token"` // 公开API访问令牌 + // 其他配置 PageSize int `json:"page_size"` MaintenanceMode bool `json:"maintenance_mode"` diff --git a/db/dto/user.go b/db/dto/user.go index 1baa7b6..fe2218b 100644 --- a/db/dto/user.go +++ b/db/dto/user.go @@ -32,6 +32,11 @@ type UpdateUserRequest struct { IsActive bool `json:"is_active"` } +// ChangePasswordRequest 修改密码请求 +type ChangePasswordRequest struct { + NewPassword string `json:"new_password" binding:"required,min=6"` +} + // UserResponse 用户响应 type UserResponse struct { ID uint `json:"id"` diff --git a/db/entity/system_config.go b/db/entity/system_config.go index fc90387..696d65f 100644 --- a/db/entity/system_config.go +++ b/db/entity/system_config.go @@ -23,6 +23,9 @@ type SystemConfig struct { AutoTransferEnabled bool `json:"auto_transfer_enabled" gorm:"default:false"` // 开启自动转存 AutoFetchHotDramaEnabled bool `json:"auto_fetch_hot_drama_enabled" gorm:"default:false"` // 自动拉取热播剧名字 + // API配置 + ApiToken string `json:"api_token" gorm:"size:100;uniqueIndex"` // 公开API访问令牌 + // 其他配置 PageSize int `json:"page_size" gorm:"default:100"` MaintenanceMode bool `json:"maintenance_mode" gorm:"default:false"` diff --git a/db/repo/resource_repository.go b/db/repo/resource_repository.go index 24c6ad1..ebba5d8 100644 --- a/db/repo/resource_repository.go +++ b/db/repo/resource_repository.go @@ -21,6 +21,7 @@ type ResourceRepository interface { FindByIsPublic(isPublic bool) ([]entity.Resource, error) Search(query string, categoryID *uint, page, limit int) ([]entity.Resource, int64, error) SearchByPanID(query string, panID uint, page, limit int) ([]entity.Resource, int64, error) + SearchWithFilters(params map[string]interface{}) ([]entity.Resource, int64, error) IncrementViewCount(id uint) error FindWithTags() ([]entity.Resource, error) UpdateWithTags(resource *entity.Resource, tagIDs []uint) error @@ -201,6 +202,49 @@ func (r *ResourceRepositoryImpl) SearchByPanID(query string, panID uint, page, l return resources, total, err } +// SearchWithFilters 根据参数进行搜索 +func (r *ResourceRepositoryImpl) SearchWithFilters(params map[string]interface{}) ([]entity.Resource, int64, error) { + var resources []entity.Resource + var total int64 + + db := r.db.Model(&entity.Resource{}) + + // 处理参数 + for key, value := range params { + switch key { + case "query": + if query, ok := value.(string); ok && query != "" { + db = db.Where("title ILIKE ? OR description ILIKE ?", "%"+query+"%", "%"+query+"%") + } + case "category_id": + if categoryID, ok := value.(uint); ok { + db = db.Where("category_id = ?", categoryID) + } + case "is_valid": + if isValid, ok := value.(bool); ok { + db = db.Where("is_valid = ?", isValid) + } + case "is_public": + if isPublic, ok := value.(bool); ok { + db = db.Where("is_public = ?", isPublic) + } + case "pan_id": + if panID, ok := value.(uint); ok { + db = db.Where("pan_id = ?", panID) + } + } + } + + // 获取总数 + if err := db.Count(&total).Error; err != nil { + return nil, 0, err + } + + // 获取分页数据,按更新时间倒序 + err := db.Order("updated_at DESC").Find(&resources).Error + return resources, total, err +} + // IncrementViewCount 增加浏览次数 func (r *ResourceRepositoryImpl) IncrementViewCount(id uint) error { return r.db.Model(&entity.Resource{}).Where("id = ?", id). diff --git a/docs/docs.go b/docs/docs.go new file mode 100644 index 0000000..7a7c220 --- /dev/null +++ b/docs/docs.go @@ -0,0 +1,379 @@ +// Package docs Code generated by swaggo/swag. DO NOT EDIT +package docs + +import "github.com/swaggo/swag" + +const docTemplate = `{ + "schemes": {{ marshal .Schemes }}, + "swagger": "2.0", + "info": { + "description": "{{escape .Description}}", + "title": "{{.Title}}", + "termsOfService": "http://swagger.io/terms/", + "contact": { + "name": "API Support", + "url": "http://www.swagger.io/support", + "email": "support@swagger.io" + }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + }, + "version": "{{.Version}}" + }, + "host": "{{.Host}}", + "basePath": "{{.BasePath}}", + "paths": { + "/api/public/hot-dramas": { + "get": { + "description": "获取热门剧列表,支持分页", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "PublicAPI" + ], + "summary": "获取热门剧列表", + "parameters": [ + { + "type": "string", + "description": "API访问令牌", + "name": "X-API-Token", + "in": "header", + "required": true + }, + { + "type": "integer", + "default": 1, + "description": "页码", + "name": "page", + "in": "query" + }, + { + "maximum": 100, + "type": "integer", + "default": 20, + "description": "每页数量", + "name": "page_size", + "in": "query" + } + ], + "responses": { + "200": { + "description": "获取成功", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "401": { + "description": "认证失败", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "500": { + "description": "服务器内部错误", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + }, + "/api/public/resources/add": { + "post": { + "description": "通过公开API添加单个资源到待处理列表", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "PublicAPI" + ], + "summary": "单个添加资源", + "parameters": [ + { + "type": "string", + "description": "API访问令牌", + "name": "X-API-Token", + "in": "header", + "required": true + }, + { + "description": "资源信息", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.ReadyResourceRequest" + } + } + ], + "responses": { + "200": { + "description": "添加成功", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "400": { + "description": "请求参数错误", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "401": { + "description": "认证失败", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "500": { + "description": "服务器内部错误", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + }, + "/api/public/resources/batch-add": { + "post": { + "description": "通过公开API批量添加多个资源到待处理列表", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "PublicAPI" + ], + "summary": "批量添加资源", + "parameters": [ + { + "type": "string", + "description": "API访问令牌", + "name": "X-API-Token", + "in": "header", + "required": true + }, + { + "description": "批量资源信息", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.BatchReadyResourceRequest" + } + } + ], + "responses": { + "200": { + "description": "批量添加成功", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "400": { + "description": "请求参数错误", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "401": { + "description": "认证失败", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "500": { + "description": "服务器内部错误", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + }, + "/api/public/resources/search": { + "get": { + "description": "搜索资源,支持关键词、标签、分类过滤", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "PublicAPI" + ], + "summary": "资源搜索", + "parameters": [ + { + "type": "string", + "description": "API访问令牌", + "name": "X-API-Token", + "in": "header", + "required": true + }, + { + "type": "string", + "description": "搜索关键词", + "name": "keyword", + "in": "query" + }, + { + "type": "string", + "description": "标签过滤", + "name": "tag", + "in": "query" + }, + { + "type": "string", + "description": "分类过滤", + "name": "category", + "in": "query" + }, + { + "type": "integer", + "default": 1, + "description": "页码", + "name": "page", + "in": "query" + }, + { + "maximum": 100, + "type": "integer", + "default": 20, + "description": "每页数量", + "name": "page_size", + "in": "query" + } + ], + "responses": { + "200": { + "description": "搜索成功", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "401": { + "description": "认证失败", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "500": { + "description": "服务器内部错误", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + } + }, + "definitions": { + "dto.BatchReadyResourceRequest": { + "type": "object", + "required": [ + "resources" + ], + "properties": { + "resources": { + "type": "array", + "items": { + "$ref": "#/definitions/dto.ReadyResourceRequest" + } + } + } + }, + "dto.ReadyResourceRequest": { + "type": "object", + "required": [ + "title", + "url" + ], + "properties": { + "category": { + "type": "string", + "example": "示例分类" + }, + "description": { + "type": "string", + "example": "这是一个示例资源描述" + }, + "extra": { + "type": "string", + "example": "额外信息" + }, + "img": { + "type": "string", + "example": "https://example.com/image.jpg" + }, + "source": { + "type": "string", + "example": "数据来源" + }, + "tags": { + "type": "string", + "example": "标签1,标签2" + }, + "title": { + "type": "string", + "example": "示例资源标题" + }, + "url": { + "type": "string", + "example": "https://example.com/resource" + } + } + } + }, + "securityDefinitions": { + "ApiTokenAuth": { + "description": "API Token认证", + "type": "apiKey", + "name": "X-API-Token", + "in": "header" + } + } +}` + +// SwaggerInfo holds exported Swagger Info so clients can modify it +var SwaggerInfo = &swag.Spec{ + Version: "1.0", + Host: "localhost:8080", + BasePath: "/api/public", + Schemes: []string{}, + Title: "网盘资源管理系统公开API", + Description: "网盘资源管理系统的公开API接口文档", + InfoInstanceName: "swagger", + SwaggerTemplate: docTemplate, + LeftDelim: "{{", + RightDelim: "}}", +} + +func init() { + swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) +} diff --git a/docs/swagger.json b/docs/swagger.json new file mode 100644 index 0000000..d65d296 --- /dev/null +++ b/docs/swagger.json @@ -0,0 +1,355 @@ +{ + "swagger": "2.0", + "info": { + "description": "网盘资源管理系统的公开API接口文档", + "title": "网盘资源管理系统公开API", + "termsOfService": "http://swagger.io/terms/", + "contact": { + "name": "API Support", + "url": "http://www.swagger.io/support", + "email": "support@swagger.io" + }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + }, + "version": "1.0" + }, + "host": "localhost:8080", + "basePath": "/api/public", + "paths": { + "/api/public/hot-dramas": { + "get": { + "description": "获取热门剧列表,支持分页", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "PublicAPI" + ], + "summary": "获取热门剧列表", + "parameters": [ + { + "type": "string", + "description": "API访问令牌", + "name": "X-API-Token", + "in": "header", + "required": true + }, + { + "type": "integer", + "default": 1, + "description": "页码", + "name": "page", + "in": "query" + }, + { + "maximum": 100, + "type": "integer", + "default": 20, + "description": "每页数量", + "name": "page_size", + "in": "query" + } + ], + "responses": { + "200": { + "description": "获取成功", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "401": { + "description": "认证失败", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "500": { + "description": "服务器内部错误", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + }, + "/api/public/resources/add": { + "post": { + "description": "通过公开API添加单个资源到待处理列表", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "PublicAPI" + ], + "summary": "单个添加资源", + "parameters": [ + { + "type": "string", + "description": "API访问令牌", + "name": "X-API-Token", + "in": "header", + "required": true + }, + { + "description": "资源信息", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.ReadyResourceRequest" + } + } + ], + "responses": { + "200": { + "description": "添加成功", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "400": { + "description": "请求参数错误", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "401": { + "description": "认证失败", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "500": { + "description": "服务器内部错误", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + }, + "/api/public/resources/batch-add": { + "post": { + "description": "通过公开API批量添加多个资源到待处理列表", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "PublicAPI" + ], + "summary": "批量添加资源", + "parameters": [ + { + "type": "string", + "description": "API访问令牌", + "name": "X-API-Token", + "in": "header", + "required": true + }, + { + "description": "批量资源信息", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.BatchReadyResourceRequest" + } + } + ], + "responses": { + "200": { + "description": "批量添加成功", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "400": { + "description": "请求参数错误", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "401": { + "description": "认证失败", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "500": { + "description": "服务器内部错误", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + }, + "/api/public/resources/search": { + "get": { + "description": "搜索资源,支持关键词、标签、分类过滤", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "PublicAPI" + ], + "summary": "资源搜索", + "parameters": [ + { + "type": "string", + "description": "API访问令牌", + "name": "X-API-Token", + "in": "header", + "required": true + }, + { + "type": "string", + "description": "搜索关键词", + "name": "keyword", + "in": "query" + }, + { + "type": "string", + "description": "标签过滤", + "name": "tag", + "in": "query" + }, + { + "type": "string", + "description": "分类过滤", + "name": "category", + "in": "query" + }, + { + "type": "integer", + "default": 1, + "description": "页码", + "name": "page", + "in": "query" + }, + { + "maximum": 100, + "type": "integer", + "default": 20, + "description": "每页数量", + "name": "page_size", + "in": "query" + } + ], + "responses": { + "200": { + "description": "搜索成功", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "401": { + "description": "认证失败", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "500": { + "description": "服务器内部错误", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + } + }, + "definitions": { + "dto.BatchReadyResourceRequest": { + "type": "object", + "required": [ + "resources" + ], + "properties": { + "resources": { + "type": "array", + "items": { + "$ref": "#/definitions/dto.ReadyResourceRequest" + } + } + } + }, + "dto.ReadyResourceRequest": { + "type": "object", + "required": [ + "title", + "url" + ], + "properties": { + "category": { + "type": "string", + "example": "示例分类" + }, + "description": { + "type": "string", + "example": "这是一个示例资源描述" + }, + "extra": { + "type": "string", + "example": "额外信息" + }, + "img": { + "type": "string", + "example": "https://example.com/image.jpg" + }, + "source": { + "type": "string", + "example": "数据来源" + }, + "tags": { + "type": "string", + "example": "标签1,标签2" + }, + "title": { + "type": "string", + "example": "示例资源标题" + }, + "url": { + "type": "string", + "example": "https://example.com/resource" + } + } + } + }, + "securityDefinitions": { + "ApiTokenAuth": { + "description": "API Token认证", + "type": "apiKey", + "name": "X-API-Token", + "in": "header" + } + } +} \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml new file mode 100644 index 0000000..a966c30 --- /dev/null +++ b/docs/swagger.yaml @@ -0,0 +1,246 @@ +basePath: /api/public +definitions: + dto.BatchReadyResourceRequest: + properties: + resources: + items: + $ref: '#/definitions/dto.ReadyResourceRequest' + type: array + required: + - resources + type: object + dto.ReadyResourceRequest: + properties: + category: + example: 示例分类 + type: string + description: + example: 这是一个示例资源描述 + type: string + extra: + example: 额外信息 + type: string + img: + example: https://example.com/image.jpg + type: string + source: + example: 数据来源 + type: string + tags: + example: 标签1,标签2 + type: string + title: + example: 示例资源标题 + type: string + url: + example: https://example.com/resource + type: string + required: + - title + - url + type: object +host: localhost:8080 +info: + contact: + email: support@swagger.io + name: API Support + url: http://www.swagger.io/support + description: 网盘资源管理系统的公开API接口文档 + license: + name: Apache 2.0 + url: http://www.apache.org/licenses/LICENSE-2.0.html + termsOfService: http://swagger.io/terms/ + title: 网盘资源管理系统公开API + version: "1.0" +paths: + /api/public/hot-dramas: + get: + consumes: + - application/json + description: 获取热门剧列表,支持分页 + parameters: + - description: API访问令牌 + in: header + name: X-API-Token + required: true + type: string + - default: 1 + description: 页码 + in: query + name: page + type: integer + - default: 20 + description: 每页数量 + in: query + maximum: 100 + name: page_size + type: integer + produces: + - application/json + responses: + "200": + description: 获取成功 + schema: + additionalProperties: true + type: object + "401": + description: 认证失败 + schema: + additionalProperties: true + type: object + "500": + description: 服务器内部错误 + schema: + additionalProperties: true + type: object + summary: 获取热门剧列表 + tags: + - PublicAPI + /api/public/resources/add: + post: + consumes: + - application/json + description: 通过公开API添加单个资源到待处理列表 + parameters: + - description: API访问令牌 + in: header + name: X-API-Token + required: true + type: string + - description: 资源信息 + in: body + name: data + required: true + schema: + $ref: '#/definitions/dto.ReadyResourceRequest' + produces: + - application/json + responses: + "200": + description: 添加成功 + schema: + additionalProperties: true + type: object + "400": + description: 请求参数错误 + schema: + additionalProperties: true + type: object + "401": + description: 认证失败 + schema: + additionalProperties: true + type: object + "500": + description: 服务器内部错误 + schema: + additionalProperties: true + type: object + summary: 单个添加资源 + tags: + - PublicAPI + /api/public/resources/batch-add: + post: + consumes: + - application/json + description: 通过公开API批量添加多个资源到待处理列表 + parameters: + - description: API访问令牌 + in: header + name: X-API-Token + required: true + type: string + - description: 批量资源信息 + in: body + name: data + required: true + schema: + $ref: '#/definitions/dto.BatchReadyResourceRequest' + produces: + - application/json + responses: + "200": + description: 批量添加成功 + schema: + additionalProperties: true + type: object + "400": + description: 请求参数错误 + schema: + additionalProperties: true + type: object + "401": + description: 认证失败 + schema: + additionalProperties: true + type: object + "500": + description: 服务器内部错误 + schema: + additionalProperties: true + type: object + summary: 批量添加资源 + tags: + - PublicAPI + /api/public/resources/search: + get: + consumes: + - application/json + description: 搜索资源,支持关键词、标签、分类过滤 + parameters: + - description: API访问令牌 + in: header + name: X-API-Token + required: true + type: string + - description: 搜索关键词 + in: query + name: keyword + type: string + - description: 标签过滤 + in: query + name: tag + type: string + - description: 分类过滤 + in: query + name: category + type: string + - default: 1 + description: 页码 + in: query + name: page + type: integer + - default: 20 + description: 每页数量 + in: query + maximum: 100 + name: page_size + type: integer + produces: + - application/json + responses: + "200": + description: 搜索成功 + schema: + additionalProperties: true + type: object + "401": + description: 认证失败 + schema: + additionalProperties: true + type: object + "500": + description: 服务器内部错误 + schema: + additionalProperties: true + type: object + summary: 资源搜索 + tags: + - PublicAPI +securityDefinitions: + ApiTokenAuth: + description: API Token认证 + in: header + name: X-API-Token + type: apiKey +swagger: "2.0" diff --git a/go.mod b/go.mod index ee9f832..66e3228 100644 --- a/go.mod +++ b/go.mod @@ -6,45 +6,68 @@ toolchain go1.23.3 require ( github.com/gin-contrib/cors v1.4.0 - github.com/gin-gonic/gin v1.9.1 + github.com/gin-gonic/gin v1.10.1 github.com/golang-jwt/jwt/v5 v5.2.0 github.com/joho/godotenv v1.5.1 github.com/lib/pq v1.10.9 - golang.org/x/crypto v0.39.0 + golang.org/x/crypto v0.40.0 gorm.io/driver/postgres v1.6.0 gorm.io/gorm v1.30.0 ) require ( - github.com/bytedance/sonic v1.9.1 // indirect - github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect - github.com/gabriel-vasile/mimetype v1.4.2 // indirect - github.com/gin-contrib/sse v0.1.0 // indirect + github.com/KyleBanks/depth v1.2.1 // indirect + github.com/PuerkitoBio/purell v1.2.1 // indirect + github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect + github.com/bytedance/sonic v1.13.3 // indirect + github.com/bytedance/sonic/loader v0.3.0 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect + github.com/cloudwego/base64x v0.1.5 // indirect + github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect + github.com/gabriel-vasile/mimetype v1.4.9 // indirect + github.com/gin-contrib/sse v1.1.0 // indirect + github.com/go-openapi/jsonpointer v0.21.1 // indirect + github.com/go-openapi/jsonreference v0.21.0 // indirect + github.com/go-openapi/spec v0.21.0 // indirect + github.com/go-openapi/swag v0.23.1 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator/v10 v10.14.0 // indirect + github.com/go-playground/validator/v10 v10.27.0 // indirect github.com/go-resty/resty/v2 v2.16.5 // indirect - github.com/goccy/go-json v0.10.2 // indirect + github.com/goccy/go-json v0.10.5 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgx/v5 v5.7.5 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/cpuid/v2 v2.2.4 // indirect - github.com/leodido/go-urn v1.2.4 // indirect - github.com/mattn/go-isatty v0.0.19 // indirect + github.com/klauspost/cpuid/v2 v2.3.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mailru/easyjson v0.9.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/pelletier/go-toml/v2 v2.0.8 // indirect + github.com/pelletier/go-toml/v2 v2.2.4 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect + github.com/swaggo/files v1.0.1 // indirect + github.com/swaggo/gin-swagger v1.6.0 // indirect + github.com/swaggo/swag v1.16.4 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect - github.com/ugorji/go/codec v1.2.11 // indirect - golang.org/x/arch v0.3.0 // indirect - golang.org/x/net v0.33.0 // indirect + github.com/ugorji/go/codec v1.3.0 // indirect + github.com/urfave/cli/v2 v2.27.7 // indirect + github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342 // indirect + go.yaml.in/yaml/v2 v2.4.2 // indirect + golang.org/x/arch v0.19.0 // indirect + golang.org/x/net v0.42.0 // indirect golang.org/x/sync v0.16.0 // indirect - golang.org/x/sys v0.33.0 // indirect + golang.org/x/sys v0.34.0 // indirect golang.org/x/text v0.27.0 // indirect - google.golang.org/protobuf v1.30.0 // indirect + golang.org/x/tools v0.35.0 // indirect + google.golang.org/protobuf v1.36.6 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + sigs.k8s.io/yaml v1.5.0 // indirect ) diff --git a/go.sum b/go.sum index 40476aa..2c3cee6 100644 --- a/go.sum +++ b/go.sum @@ -1,22 +1,56 @@ +github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= +github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= +github.com/PuerkitoBio/purell v1.2.1 h1:QsZ4TjvwiMpat6gBCBxEQI0rcS9ehtkKtSpiUnd9N28= +github.com/PuerkitoBio/purell v1.2.1/go.mod h1:ZwHcC/82TOaovDi//J/804umJFFmbOHPngi8iYYv/Eo= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= +github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= +github.com/bytedance/sonic v1.13.3 h1:MS8gmaH16Gtirygw7jV91pDCN33NyMrPbN7qiYhEsF0= +github.com/bytedance/sonic v1.13.3/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA= +github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= +github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4= +github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/cpuguy83/go-md2man/v2 v2.0.7 h1:zbFlGlXEAKlwXpmvle3d8Oe3YnkKIK4xSRTd3sHPnBo= +github.com/cpuguy83/go-md2man/v2 v2.0.7/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= +github.com/gabriel-vasile/mimetype v1.4.9 h1:5k+WDwEsD9eTLL8Tz3L0VnmVh9QxGjRmjBvAG7U/oYY= +github.com/gabriel-vasile/mimetype v1.4.9/go.mod h1:WnSQhFKJuBlRyLiKohA/2DtIlPFAbguNaG7QCHcyGok= github.com/gin-contrib/cors v1.4.0 h1:oJ6gwtUl3lqV0WEIwM/LxPF1QZ5qe2lGWdY2+bz7y0g= github.com/gin-contrib/cors v1.4.0/go.mod h1:bs9pNM0x/UsmHPBWT2xZz9ROh8xYjYkiURUfmBoMlcs= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= +github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/gin-gonic/gin v1.10.1 h1:T0ujvqyCSqRopADpgPgiTT63DUQVSfojyME59Ei63pQ= +github.com/gin-gonic/gin v1.10.1/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +github.com/go-openapi/jsonpointer v0.21.1 h1:whnzv/pNXtK2FbX/W9yJfRmE2gsmkfahjMKB0fZvcic= +github.com/go-openapi/jsonpointer v0.21.1/go.mod h1:50I1STOfbY1ycR8jGz8DaMeLCdXiI6aDteEdRNNzpdk= +github.com/go-openapi/jsonreference v0.21.0 h1:Rs+Y7hSXT83Jacb7kFyjn4ijOuVGSvOdF2+tg1TRrwQ= +github.com/go-openapi/jsonreference v0.21.0/go.mod h1:LmZmgsrTkVg9LG4EaHeY8cBDslNPMo06cago5JNLkm4= +github.com/go-openapi/spec v0.21.0 h1:LTVzPc3p/RzRnkQqLRndbAzjY0d0BCL72A6j3CdL9ZY= +github.com/go-openapi/spec v0.21.0/go.mod h1:78u6VdPw81XU44qEWGhtr982gJ5BWg2c0I5XwVMotYk= +github.com/go-openapi/swag v0.23.1 h1:lpsStH0n2ittzTnbaSloVZLuB5+fvSY/+hnagBjSNZU= +github.com/go-openapi/swag v0.23.1/go.mod h1:STZs8TbRvEQQKUA+JZNAm3EWlgaOBGpyFDqQnDHMef0= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= @@ -29,11 +63,15 @@ github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91 github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos= github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js= github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4= +github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo= github.com/go-resty/resty/v2 v2.16.5 h1:hBKqmWrr7uRc3euHVqmh1HTHcKn99Smr7o5spptdhTM= github.com/go-resty/resty/v2 v2.16.5/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA= github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw= github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= @@ -54,11 +92,16 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= +github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= +github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= @@ -70,11 +113,17 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4= +github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -83,12 +132,19 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo= github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= +github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= +github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -101,50 +157,98 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/swaggo/files v1.0.1 h1:J1bVJ4XHZNq0I46UU90611i9/YzdrF7x92oX1ig5IdE= +github.com/swaggo/files v1.0.1/go.mod h1:0qXmMNH6sXNf+73t65aKeB+ApmgxdnkQzVTAj2uaMUg= +github.com/swaggo/gin-swagger v1.6.0 h1:y8sxvQ3E20/RCyrXeFfg60r6H0Z+SwpTjMYsMm+zy8M= +github.com/swaggo/gin-swagger v1.6.0/go.mod h1:BG00cCEy294xtVpyIAHG6+e2Qzj/xKlRdOqDkvq0uzo= +github.com/swaggo/swag v1.16.4 h1:clWJtd9LStiG3VeijiCfOVODP6VpHtKdQy9ELFG3s1A= +github.com/swaggo/swag v1.16.4/go.mod h1:VBsHJRsDvfYvqoiMKnsdwhNV9LEMHgEDZcyVYX0sxPg= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA= +github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= +github.com/urfave/cli/v2 v2.27.7 h1:bH59vdhbjLv3LAvIu6gd0usJHgoTTPhCFib8qqOwXYU= +github.com/urfave/cli/v2 v2.27.7/go.mod h1:CyNAG/xg+iAOg0N4MPGZqVmv2rCoP267496AOXUZjA4= +github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342 h1:FnBeRrxr7OU4VvAzt5X7s6266i6cSVkkFPS0TuXWbIg= +github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI= +go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.19.0 h1:LmbDQUodHThXE+htjrnmVD73M//D9GTH6wFZjyDkjyU= +golang.org/x/arch v0.19.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM= golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U= +golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM= +golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I= golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4= +golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs= +golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw= golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA= +golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4= golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.35.0 h1:mBffYraMEf7aa0sB+NuKnuCy8qI/9Bughn8dC2Gu5r0= +golang.org/x/tools v0.35.0/go.mod h1:NKdj5HkL/73byiZSJjqJgKn3ep7KjFkBOkR/Hps3VPw= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY= +google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= @@ -154,4 +258,7 @@ gorm.io/driver/postgres v1.6.0 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4= gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo= gorm.io/gorm v1.30.0 h1:qbT5aPv1UH8gI99OsRlvDToLxW5zR7FzS9acZDOZcgs= gorm.io/gorm v1.30.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= +sigs.k8s.io/yaml v1.5.0 h1:M10b2U7aEUY6hRtU870n2VTPgR5RZiL/I6Lcc2F4NUQ= +sigs.k8s.io/yaml v1.5.0/go.mod h1:wZs27Rbxoai4C0f8/9urLZtZtF3avA3gKvGyPdDqTO4= diff --git a/handlers/public_api_handler.go b/handlers/public_api_handler.go new file mode 100644 index 0000000..a3080ea --- /dev/null +++ b/handlers/public_api_handler.go @@ -0,0 +1,334 @@ +package handlers + +import ( + "net/http" + "res_db/db/converter" + "res_db/db/dto" + "strconv" + + "github.com/gin-gonic/gin" +) + +// PublicAPIHandler 公开API处理器 +type PublicAPIHandler struct{} + +// NewPublicAPIHandler 创建公开API处理器 +func NewPublicAPIHandler() *PublicAPIHandler { + return &PublicAPIHandler{} +} + +// AddSingleResource godoc +// @Summary 单个添加资源 +// @Description 通过公开API添加单个资源到待处理列表 +// @Tags PublicAPI +// @Accept json +// @Produce json +// @Param X-API-Token header string true "API访问令牌" +// @Param data body dto.ReadyResourceRequest true "资源信息" +// @Success 200 {object} map[string]interface{} "添加成功" +// @Failure 400 {object} map[string]interface{} "请求参数错误" +// @Failure 401 {object} map[string]interface{} "认证失败" +// @Failure 500 {object} map[string]interface{} "服务器内部错误" +// @Router /api/public/resources/add [post] +func (h *PublicAPIHandler) AddSingleResource(c *gin.Context) { + var req dto.ReadyResourceRequest + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "success": false, + "message": "请求参数错误: " + err.Error(), + "code": 400, + }) + return + } + + // 验证必填字段 + if req.Title == "" { + c.JSON(http.StatusBadRequest, gin.H{ + "success": false, + "message": "标题不能为空", + "code": 400, + }) + return + } + + if req.Url == "" { + c.JSON(http.StatusBadRequest, gin.H{ + "success": false, + "message": "URL不能为空", + "code": 400, + }) + return + } + + // 转换为实体 + readyResource := converter.RequestToReadyResource(&req) + if readyResource == nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "success": false, + "message": "数据转换失败", + "code": 500, + }) + return + } + + // 设置来源 + readyResource.Source = "公开API" + + // 保存到数据库 + err := repoManager.ReadyResourceRepository.Create(readyResource) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "success": false, + "message": "添加资源失败: " + err.Error(), + "code": 500, + }) + return + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "资源添加成功,已进入待处理列表", + "data": gin.H{ + "id": readyResource.ID, + }, + "code": 200, + }) +} + +// AddBatchResources godoc +// @Summary 批量添加资源 +// @Description 通过公开API批量添加多个资源到待处理列表 +// @Tags PublicAPI +// @Accept json +// @Produce json +// @Param X-API-Token header string true "API访问令牌" +// @Param data body dto.BatchReadyResourceRequest true "批量资源信息" +// @Success 200 {object} map[string]interface{} "批量添加成功" +// @Failure 400 {object} map[string]interface{} "请求参数错误" +// @Failure 401 {object} map[string]interface{} "认证失败" +// @Failure 500 {object} map[string]interface{} "服务器内部错误" +// @Router /api/public/resources/batch-add [post] +func (h *PublicAPIHandler) AddBatchResources(c *gin.Context) { + var req dto.BatchReadyResourceRequest + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "success": false, + "message": "请求参数错误: " + err.Error(), + "code": 400, + }) + return + } + + if len(req.Resources) == 0 { + c.JSON(http.StatusBadRequest, gin.H{ + "success": false, + "message": "资源列表不能为空", + "code": 400, + }) + return + } + + // 验证每个资源 + for i, resource := range req.Resources { + if resource.Title == "" { + c.JSON(http.StatusBadRequest, gin.H{ + "success": false, + "message": "第" + strconv.Itoa(i+1) + "个资源标题不能为空", + "code": 400, + }) + return + } + + if resource.Url == "" { + c.JSON(http.StatusBadRequest, gin.H{ + "success": false, + "message": "第" + strconv.Itoa(i+1) + "个资源URL不能为空", + "code": 400, + }) + return + } + } + + // 批量保存 + var createdResources []uint + for _, resourceReq := range req.Resources { + readyResource := converter.RequestToReadyResource(&resourceReq) + if readyResource != nil { + readyResource.Source = "公开API批量添加" + err := repoManager.ReadyResourceRepository.Create(readyResource) + if err == nil { + createdResources = append(createdResources, readyResource.ID) + } + } + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "批量添加成功,共添加 " + strconv.Itoa(len(createdResources)) + " 个资源", + "data": gin.H{ + "created_count": len(createdResources), + "created_ids": createdResources, + }, + "code": 200, + }) +} + +// SearchResources godoc +// @Summary 资源搜索 +// @Description 搜索资源,支持关键词、标签、分类过滤 +// @Tags PublicAPI +// @Accept json +// @Produce json +// @Param X-API-Token header string true "API访问令牌" +// @Param keyword query string false "搜索关键词" +// @Param tag query string false "标签过滤" +// @Param category query string false "分类过滤" +// @Param page query int false "页码" default(1) +// @Param page_size query int false "每页数量" default(20) maximum(100) +// @Success 200 {object} map[string]interface{} "搜索成功" +// @Failure 401 {object} map[string]interface{} "认证失败" +// @Failure 500 {object} map[string]interface{} "服务器内部错误" +// @Router /api/public/resources/search [get] +func (h *PublicAPIHandler) SearchResources(c *gin.Context) { + // 获取查询参数 + keyword := c.Query("keyword") + tag := c.Query("tag") + category := c.Query("category") + pageStr := c.DefaultQuery("page", "1") + pageSizeStr := c.DefaultQuery("page_size", "20") + + page, err := strconv.Atoi(pageStr) + if err != nil || page < 1 { + page = 1 + } + + pageSize, err := strconv.Atoi(pageSizeStr) + if err != nil || pageSize < 1 || pageSize > 100 { + pageSize = 20 + } + + // 构建搜索条件 + params := map[string]interface{}{ + "page": page, + "page_size": pageSize, + } + + if keyword != "" { + params["search"] = keyword + } + + if tag != "" { + params["tag"] = tag + } + + if category != "" { + params["category"] = category + } + + // 执行搜索 + resources, total, err := repoManager.ResourceRepository.SearchWithFilters(params) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "success": false, + "message": "搜索失败: " + err.Error(), + "code": 500, + }) + return + } + + // 转换为响应格式 + var resourceResponses []gin.H + for _, resource := range resources { + resourceResponses = append(resourceResponses, gin.H{ + "id": resource.ID, + "title": resource.Title, + "url": resource.URL, + "description": resource.Description, + "view_count": resource.ViewCount, + "created_at": resource.CreatedAt.Format("2006-01-02 15:04:05"), + "updated_at": resource.UpdatedAt.Format("2006-01-02 15:04:05"), + }) + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "搜索成功", + "data": gin.H{ + "resources": resourceResponses, + "total": total, + "page": page, + "page_size": pageSize, + }, + "code": 200, + }) +} + +// GetHotDramas godoc +// @Summary 获取热门剧列表 +// @Description 获取热门剧列表,支持分页 +// @Tags PublicAPI +// @Accept json +// @Produce json +// @Param X-API-Token header string true "API访问令牌" +// @Param page query int false "页码" default(1) +// @Param page_size query int false "每页数量" default(20) maximum(100) +// @Success 200 {object} map[string]interface{} "获取成功" +// @Failure 401 {object} map[string]interface{} "认证失败" +// @Failure 500 {object} map[string]interface{} "服务器内部错误" +// @Router /api/public/hot-dramas [get] +func (h *PublicAPIHandler) GetHotDramas(c *gin.Context) { + pageStr := c.DefaultQuery("page", "1") + pageSizeStr := c.DefaultQuery("page_size", "20") + + page, err := strconv.Atoi(pageStr) + if err != nil || page < 1 { + page = 1 + } + + pageSize, err := strconv.Atoi(pageSizeStr) + if err != nil || pageSize < 1 || pageSize > 100 { + pageSize = 20 + } + + // 获取热门剧 + hotDramas, total, err := repoManager.HotDramaRepository.FindAll(page, pageSize) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "success": false, + "message": "获取热门剧失败: " + err.Error(), + "code": 500, + }) + return + } + + // 转换为响应格式 + var hotDramaResponses []gin.H + for _, drama := range hotDramas { + hotDramaResponses = append(hotDramaResponses, gin.H{ + "id": drama.ID, + "title": drama.Title, + "description": drama.CardSubtitle, // 使用副标题作为描述 + "img": drama.PosterURL, // 使用海报URL作为图片 + "url": drama.DoubanURI, // 使用豆瓣链接作为URL + "rating": drama.Rating, + "year": drama.Year, + "region": drama.Region, + "genres": drama.Genres, + "category": drama.Category, + "created_at": drama.CreatedAt.Format("2006-01-02 15:04:05"), + "updated_at": drama.UpdatedAt.Format("2006-01-02 15:04:05"), + }) + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "获取热门剧成功", + "data": gin.H{ + "hot_dramas": hotDramaResponses, + "total": total, + "page": page, + "page_size": pageSize, + }, + "code": 200, + }) +} diff --git a/handlers/user_handler.go b/handlers/user_handler.go index 75a4c59..12ab1e7 100644 --- a/handlers/user_handler.go +++ b/handlers/user_handler.go @@ -205,6 +205,44 @@ func UpdateUser(c *gin.Context) { SuccessResponse(c, gin.H{"message": "用户更新成功"}) } +// ChangePassword 修改用户密码(管理员) +func ChangePassword(c *gin.Context) { + idStr := c.Param("id") + id, err := strconv.ParseUint(idStr, 10, 32) + if err != nil { + ErrorResponse(c, "无效的ID", http.StatusBadRequest) + return + } + + var req dto.ChangePasswordRequest + if err := c.ShouldBindJSON(&req); err != nil { + ErrorResponse(c, err.Error(), http.StatusBadRequest) + return + } + + user, err := repoManager.UserRepository.FindByID(uint(id)) + if err != nil { + ErrorResponse(c, "用户不存在", http.StatusNotFound) + return + } + + // 哈希新密码 + hashedPassword, err := middleware.HashPassword(req.NewPassword) + if err != nil { + ErrorResponse(c, "密码加密失败", http.StatusInternalServerError) + return + } + + user.Password = hashedPassword + err = repoManager.UserRepository.Update(user) + if err != nil { + ErrorResponse(c, err.Error(), http.StatusInternalServerError) + return + } + + SuccessResponse(c, gin.H{"message": "密码修改成功"}) +} + // DeleteUser 删除用户(管理员) func DeleteUser(c *gin.Context) { idStr := c.Param("id") diff --git a/main.go b/main.go index e666cf3..68080a6 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,23 @@ +// @title 网盘资源管理系统公开API +// @version 1.0 +// @description 网盘资源管理系统的公开API接口文档 +// @termsOfService http://swagger.io/terms/ + +// @contact.name API Support +// @contact.url http://www.swagger.io/support +// @contact.email support@swagger.io + +// @license.name Apache 2.0 +// @license.url http://www.apache.org/licenses/LICENSE-2.0.html + +// @host localhost:8080 +// @BasePath /api/public + +// @securityDefinitions.apikey ApiTokenAuth +// @in header +// @name X-API-Token +// @description API Token认证 + package main import ( @@ -10,9 +30,13 @@ import ( "res_db/handlers" "res_db/middleware" + _ "res_db/docs" + "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/joho/godotenv" + swaggerFiles "github.com/swaggo/files" + ginSwagger "github.com/swaggo/gin-swagger" ) func main() { @@ -73,9 +97,29 @@ func main() { // 将Repository管理器注入到handlers中 handlers.SetRepositoryManager(repoManager) + // 设置公开API中间件的Repository管理器 + middleware.SetRepositoryManager(repoManager) + + // 创建公开API处理器 + publicAPIHandler := handlers.NewPublicAPIHandler() + // API路由 api := r.Group("/api") { + // 公开API路由(需要API Token认证) + publicAPI := api.Group("/public") + publicAPI.Use(middleware.PublicAPIAuth()) + { + // 单个添加资源 + publicAPI.POST("/resources/add", publicAPIHandler.AddSingleResource) + // 批量添加资源 + publicAPI.POST("/resources/batch-add", publicAPIHandler.AddBatchResources) + // 资源搜索 + publicAPI.GET("/resources/search", publicAPIHandler.SearchResources) + // 热门剧 + publicAPI.GET("/hot-dramas", publicAPIHandler.GetHotDramas) + } + // 认证路由 api.POST("/auth/login", handlers.Login) api.POST("/auth/register", handlers.Register) @@ -137,6 +181,7 @@ func main() { api.GET("/users", middleware.AuthMiddleware(), middleware.AdminMiddleware(), handlers.GetUsers) api.POST("/users", middleware.AuthMiddleware(), middleware.AdminMiddleware(), handlers.CreateUser) api.PUT("/users/:id", middleware.AuthMiddleware(), middleware.AdminMiddleware(), handlers.UpdateUser) + api.PUT("/users/:id/password", middleware.AuthMiddleware(), middleware.AdminMiddleware(), handlers.ChangePassword) api.DELETE("/users/:id", middleware.AuthMiddleware(), middleware.AdminMiddleware(), handlers.DeleteUser) // 搜索统计路由 @@ -174,6 +219,9 @@ func main() { // 静态文件服务 r.Static("/uploads", "./uploads") + // 注册Swagger UI路由 + r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) + port := os.Getenv("PORT") if port == "" { port = "8080" diff --git a/middleware/public_api.go b/middleware/public_api.go new file mode 100644 index 0000000..a60ec1e --- /dev/null +++ b/middleware/public_api.go @@ -0,0 +1,93 @@ +package middleware + +import ( + "net/http" + "res_db/db/repo" + + "github.com/gin-gonic/gin" +) + +var repoManager *repo.RepositoryManager + +// SetRepositoryManager 设置Repository管理器 +func SetRepositoryManager(rm *repo.RepositoryManager) { + repoManager = rm +} + +// PublicAPIAuth 公开API认证中间件 +func PublicAPIAuth() gin.HandlerFunc { + return func(c *gin.Context) { + // 获取API Token + apiToken := c.GetHeader("X-API-Token") + if apiToken == "" { + // 尝试从查询参数获取 + apiToken = c.Query("api_token") + } + + if apiToken == "" { + c.JSON(http.StatusUnauthorized, gin.H{ + "success": false, + "message": "缺少API Token", + "code": 401, + }) + c.Abort() + return + } + + // 验证API Token + if repoManager == nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "success": false, + "message": "系统未初始化", + "code": 500, + }) + c.Abort() + return + } + + config, err := repoManager.SystemConfigRepository.FindFirst() + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "success": false, + "message": "系统配置获取失败", + "code": 500, + }) + c.Abort() + return + } + + if config.ApiToken == "" { + c.JSON(http.StatusServiceUnavailable, gin.H{ + "success": false, + "message": "API Token未配置", + "code": 503, + }) + c.Abort() + return + } + + if config.ApiToken != apiToken { + c.JSON(http.StatusUnauthorized, gin.H{ + "success": false, + "message": "API Token无效", + "code": 401, + }) + c.Abort() + return + } + + // 检查维护模式 + if config.MaintenanceMode { + c.JSON(http.StatusServiceUnavailable, gin.H{ + "success": false, + "message": "系统维护中,请稍后再试", + "code": 503, + }) + c.Abort() + return + } + + // 验证通过,继续处理 + c.Next() + } +} diff --git a/migration_add_api_token.sql b/migration_add_api_token.sql new file mode 100644 index 0000000..7324ac0 --- /dev/null +++ b/migration_add_api_token.sql @@ -0,0 +1,17 @@ +-- 为system_configs表添加api_token字段 +-- 执行时间: 2024-12-19 + +-- 添加api_token字段 +ALTER TABLE system_configs +ADD COLUMN api_token VARCHAR(100) UNIQUE; + +-- 为现有记录生成默认的api_token +UPDATE system_configs +SET api_token = CONCAT('api_', MD5(RANDOM()::text), '_', EXTRACT(EPOCH FROM NOW())::bigint) +WHERE api_token IS NULL; + +-- 添加索引以提高查询性能 +CREATE INDEX idx_system_configs_api_token ON system_configs(api_token); + +-- 添加注释 +COMMENT ON COLUMN system_configs.api_token IS '公开API访问令牌,用于API认证'; \ No newline at end of file diff --git a/web/components/BatchAddResource.vue b/web/components/BatchAddResource.vue index 68185e2..d6d1600 100644 --- a/web/components/BatchAddResource.vue +++ b/web/components/BatchAddResource.vue @@ -3,17 +3,18 @@
-

格式1:标题和URL两行一组

+

格式要求:标题和URL两行为一组,标题为必填项

 电影标题1
 https://pan.baidu.com/s/123456
 电影标题2
-https://pan.baidu.com/s/789012
-

格式2:只有URL,系统自动判断

-
-https://pan.baidu.com/s/123456
 https://pan.baidu.com/s/789012
-https://pan.baidu.com/s/345678
+电视剧标题3 +https://pan.quark.cn/s/345678 +

+ + 注意:标题为必填项,不能为空 +

@@ -22,7 +23,7 @@ https://pan.baidu.com/s/345678 v-model="batchInput" rows="15" class="w-full px-3 py-2 border border-gray-300 dark:border-gray-700 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-900 dark:text-gray-100" - placeholder="请输入资源内容,支持两种格式..." + placeholder="请输入资源内容,格式:标题和URL两行为一组..." >
@@ -46,13 +47,69 @@ const batchInput = ref('') const readyResourceApi = useReadyResourceApi() +// 验证输入格式 +const validateInput = () => { + if (!batchInput.value.trim()) { + throw new Error('请输入资源内容') + } + + const lines = batchInput.value.split(/\r?\n/).map(line => line.trim()).filter(Boolean) + + if (lines.length === 0) { + throw new Error('请输入有效的资源内容') + } + + // 检查是否为偶数行(标题+URL为一组) + if (lines.length % 2 !== 0) { + throw new Error('资源格式错误:标题和URL必须成对出现,请检查是否缺少标题或URL') + } + + // 检查每组的标题是否为空 + for (let i = 0; i < lines.length; i += 2) { + const title = lines[i] + const url = lines[i + 1] + + if (!title) { + throw new Error(`第${i + 1}行标题不能为空`) + } + + if (!url) { + throw new Error(`第${i + 2}行URL不能为空`) + } + + // 验证URL格式 + try { + new URL(url) + } catch { + throw new Error(`第${i + 2}行URL格式无效: ${url}`) + } + } +} + // 批量添加提交 const handleSubmit = async () => { loading.value = true try { - if (!batchInput.value.trim()) throw new Error('请输入资源内容') - const res: any = await readyResourceApi.createReadyResourcesFromText(batchInput.value) - emit('success', `成功添加 ${res.count || 0} 个资源,资源已进入待处理列表,处理完成后会自动入库`) + validateInput() + + // 解析输入内容 + const lines = batchInput.value.split(/\r?\n/).map(line => line.trim()).filter(Boolean) + const resources = [] + + for (let i = 0; i < lines.length; i += 2) { + const title = lines[i] + const url = lines[i + 1] + + resources.push({ + title: title, + url: url, + source: '批量添加' + }) + } + + // 调用API添加资源 + const res: any = await readyResourceApi.batchCreateReadyResources(resources) + emit('success', `成功添加 ${res.count || resources.length} 个资源,资源已进入待处理列表,处理完成后会自动入库`) batchInput.value = '' } catch (e: any) { emit('error', e.message || '批量添加失败') diff --git a/web/components/SingleAddResource.vue b/web/components/SingleAddResource.vue index 64e80e5..fd893ed 100644 --- a/web/components/SingleAddResource.vue +++ b/web/components/SingleAddResource.vue @@ -3,12 +3,13 @@
@@ -171,6 +172,9 @@ const removeTag = (tag: string) => { // 验证表单 const validateForm = () => { + if (!form.value.title.trim()) { + throw new Error('标题不能为空') + } if (!form.value.url.trim()) { throw new Error('请输入至少一个URL') } @@ -213,7 +217,7 @@ const handleSubmit = async () => { // 为每个URL创建一个资源 for (const url of urls) { const resourceData = { - title: form.value.title || undefined, // 如果为空则传undefined + title: form.value.title, // 标题必填 description: form.value.description || undefined, // 添加描述 url: url, category: form.value.category || '', diff --git a/web/composables/useApi.ts b/web/composables/useApi.ts index ff980be..12fed5c 100644 --- a/web/composables/useApi.ts +++ b/web/composables/useApi.ts @@ -701,11 +701,22 @@ export const useUserApi = () => { return parseApiResponse(response) } + const changePassword = async (id: number, newPassword: string) => { + const response = await $fetch(`/users/${id}/password`, { + baseURL: config.public.apiBase, + method: 'PUT', + body: { new_password: newPassword }, + headers: getAuthHeaders() as Record + }) + return parseApiResponse(response) + } + return { getUsers, getUser, createUser, updateUser, deleteUser, + changePassword, } } \ No newline at end of file diff --git a/web/pages/api-docs.vue b/web/pages/api-docs.vue new file mode 100644 index 0000000..78ae3a8 --- /dev/null +++ b/web/pages/api-docs.vue @@ -0,0 +1,371 @@ + + + + + \ No newline at end of file diff --git a/web/pages/index.vue b/web/pages/index.vue index 50de2c3..21a3fda 100644 --- a/web/pages/index.vue +++ b/web/pages/index.vue @@ -23,32 +23,31 @@ {{ systemConfig?.site_title || '网盘资源管理系统' }} -