diff --git a/backend/main.go b/backend/main.go index 565c7892..8b91e950 100644 --- a/backend/main.go +++ b/backend/main.go @@ -140,6 +140,7 @@ func main() { authGroup.GET("/spiders/:id/stats", routes.GetSpiderStats) // 爬虫统计数据 authGroup.GET("/spider/types", routes.GetSpiderTypes) // 爬虫类型 // 可配置爬虫 + authGroup.GET("/config_spiders/:id/config", routes.GetConfigSpiderConfig) // 可配置爬虫配置 authGroup.PUT("/config_spiders", routes.PutConfigSpider) // 添加可配置爬虫 authGroup.POST("/config_spiders/:id", routes.PostConfigSpider) // 修改可配置爬虫 authGroup.POST("/config_spiders/:id/upload", routes.UploadConfigSpider) // 上传可配置爬虫 diff --git a/backend/routes/config_spider.go b/backend/routes/config_spider.go index 6f4a2893..6261e2e5 100644 --- a/backend/routes/config_spider.go +++ b/backend/routes/config_spider.go @@ -236,3 +236,50 @@ func UploadConfigSpider(c *gin.Context) { Message: "success", }) } + +func GetConfigSpiderConfig(c *gin.Context) { + id := c.Param("id") + + // 校验ID + if !bson.IsObjectIdHex(id) { + HandleErrorF(http.StatusBadRequest, c, "invalid id") + } + + // 获取爬虫 + spider, err := model.GetSpider(bson.ObjectIdHex(id)) + if err != nil { + HandleError(http.StatusInternalServerError, c, err) + return + } + + // 校验爬虫类别 + if spider.Type != constants.Configurable { + HandleErrorF(http.StatusBadRequest, c, "not a configurable spider") + return + } + + // Spiderfile 目录 + sfPath := filepath.Join(spider.Src, "Spiderfile") + + // 构造配置数据 + configData := entity.ConfigSpiderData{} + + // 读取YAML文件 + yamlFile, err := ioutil.ReadFile(sfPath) + if err != nil { + HandleError(http.StatusInternalServerError, c, err) + return + } + + // 反序列化 + if err := yaml.Unmarshal(yamlFile, &configData); err != nil { + HandleError(http.StatusInternalServerError, c, err) + return + } + + c.JSON(http.StatusOK, Response{ + Status: "ok", + Message: "success", + Data: configData, + }) +}