完成開發 思迅數據同步接口

This commit is contained in:
Jack 2025-11-22 01:42:27 +08:00
parent d9754e7f54
commit 5370f16243
9 changed files with 70 additions and 42 deletions

View File

@ -53,7 +53,7 @@ fafa-crawler
使用以下命令运行应用程序: 使用以下命令运行应用程序:
``` ```
go run src/main.go go run main.go
``` ```
服务器将在环境变量 `PORT` 指定的端口上启动,默认为 `3000` 服务器将在环境变量 `PORT` 指定的端口上启动,默认为 `3000`

View File

@ -1,15 +1,18 @@
[mysql] [server]
port = 3001
[mysql_local]
host = "localhost" host = "localhost"
port = 3306 port = 3306
user = "root" user = "root"
password = "1234567890" password = "1234567890"
name = "goods_library" name = "goods_library"
[mysql_prod] [mysql]
host = "43.139.72.196" host = "43.139.72.196"
port = 13307 port = 13307
user = "webprod" user = "webprod"
password = "v9W2ER6KG9HVtH1mjAytRvdr" password = "J1XivNvAcR21}pA6Cysm.E29"
name = "mall_prod" name = "mall_prod"
max_idle_conns = 10 max_idle_conns = 10

30
main.go
View File

@ -2,28 +2,23 @@ package main
import ( import (
"log" "log"
"os"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/logger" "github.com/gofiber/fiber/v3/middleware/logger"
"github.com/joho/godotenv" "github.com/spf13/viper"
"fafa-crawler/src/controller" "fafa-crawler/src/controller"
// "fafa-crawler/src/services"
) )
func main() { func main() {
// 初始化配置
initConfig()
app := fiber.New() app := fiber.New()
// 添加日志中间件 // 添加日志中间件
app.Use(logger.New()) app.Use(logger.New())
// Load environment variables from .env file
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
}
// Initialize controller and services // Initialize controller and services
ctrl := controller.Controller{} ctrl := controller.Controller{}
@ -37,9 +32,22 @@ func main() {
app.Post("/sixun/data/sync", ctrl.SyncSiXunGoodsData) app.Post("/sixun/data/sync", ctrl.SyncSiXunGoodsData)
// Start the server // Start the server
port := os.Getenv("PORT") port := viper.GetString("server.port")
if port == "" { if port == "" {
port = "3000" port = "3001"
} }
log.Fatal(app.Listen(":"+port, fiber.ListenConfig{EnablePrefork: true})) log.Fatal(app.Listen(":"+port, fiber.ListenConfig{EnablePrefork: true}))
} }
func initConfig() {
// 设置配置文件路径和类型
viper.SetConfigFile("./config/config.toml")
viper.SetConfigType("toml")
// 读取配置文件
err := viper.ReadInConfig()
if err != nil {
log.Printf("Warning: unable to read config file: %s", err)
return
}
}

View File

@ -8,9 +8,16 @@ import (
) )
var ( var (
hdyColly *colly_service.HdyCollyService
productService *services.ProductService
sxGoodsService *services.SxGoodsService
)
func init() {
hdyColly = colly_service.NewHdyCollyService() hdyColly = colly_service.NewHdyCollyService()
productService = services.NewProductService() productService = services.NewProductService()
) sxGoodsService = services.NewSxGoodsService()
}
// APIResponse 标准API响应结构体(符合国内大厂规范) // APIResponse 标准API响应结构体(符合国内大厂规范)
// 包含状态码、消息、数据和时间戳字段 // 包含状态码、消息、数据和时间戳字段

View File

@ -1,13 +1,15 @@
package controller package controller
import ( import (
"fafa-crawler/src/services"
"log" "log"
"strings"
"time" "time"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
) )
var sixunFilePath = "/Users/panjunjie/work/fafa_library/sixun/"
// SyncSiXunGoodsData 处理思迅爬取的数据,上传到小发同城图片库 // SyncSiXunGoodsData 处理思迅爬取的数据,上传到小发同城图片库
func (c *Controller) SyncSiXunGoodsData(ctx fiber.Ctx) error { func (c *Controller) SyncSiXunGoodsData(ctx fiber.Ctx) error {
start := time.Now() start := time.Now()
@ -23,16 +25,18 @@ func (c *Controller) SyncSiXunGoodsData(ctx fiber.Ctx) error {
return ctx.Status(fiber.StatusBadRequest).JSON(Error(fiber.StatusBadRequest, "请提供图片源目录路径")) return ctx.Status(fiber.StatusBadRequest).JSON(Error(fiber.StatusBadRequest, "请提供图片源目录路径"))
} }
sxGoodsService := services.NewSxGoodsService()
successCnt := 0 successCnt := 0
totalSrcDataCnt := int64(0) totalSrcDataCnt := int64(0)
totalFilteredDataCnt := int64(0) totalFilteredDataCnt := int64(0)
totalSavedDataCnt := int64(0) totalSavedDataCnt := int64(0)
totalFilteredImgCnt := int64(0) totalFilteredImgCnt := int64(0)
if !strings.HasSuffix(sixunFilePath, "/") {
sixunFilePath += "/"
}
for _, folder := range params.Folders { for _, folder := range params.Folders {
path := processFilePath + folder path := sixunFilePath + folder
// 处理思迅商品数据 // 处理思迅商品数据
srcDataCnt, filteredDataCnt, savedDataCnt, filteredImgCnt := sxGoodsService.ProcessSiXunGoodsData(path) srcDataCnt, filteredDataCnt, savedDataCnt, filteredImgCnt := sxGoodsService.ProcessSiXunGoodsData(path)

View File

@ -13,9 +13,15 @@ var (
once sync.Once once sync.Once
dbGorm *gorm.DB dbGorm *gorm.DB
ormQ *mapper.Query ormQ *mapper.Query
// 服务实例
productService *ProductService
sxGoodsService *SxGoodsService
) )
func init() { func init() {
once.Do(func() {
// 初始化数据库连接
if dbGorm == nil { if dbGorm == nil {
dbutil.InitMySQLDB() dbutil.InitMySQLDB()
dbGorm = dbutil.MySQLDB dbGorm = dbutil.MySQLDB
@ -23,6 +29,15 @@ func init() {
// 初始化 gorm 的 mapper // 初始化 gorm 的 mapper
mapper.SetDefault(dbGorm) mapper.SetDefault(dbGorm)
ormQ = mapper.Use(dbGorm) ormQ = mapper.Use(dbGorm)
// 确保数据库连接不为nil
if dbGorm == nil {
panic("数据库连接未初始化")
}
// 初始化所有服务实例
productService = &ProductService{db: dbGorm}
sxGoodsService = &SxGoodsService{db: dbGorm}
})
} }

View File

@ -14,7 +14,6 @@ import (
) )
var ( var (
productService *ProductService
filePath = "/media/images/goods_library/" filePath = "/media/images/goods_library/"
) )
@ -22,10 +21,7 @@ type ProductService struct {
db *gorm.DB // Add database connection db *gorm.DB // Add database connection
} }
func NewProductService() *ProductService { // Pass database connection func NewProductService() *ProductService {
once.Do(func() {
productService = &ProductService{db: dbGorm} // Initialize with database connection
})
return productService return productService
} }

View File

@ -23,18 +23,13 @@ const (
BatchSize = 400 BatchSize = 400
) )
var ( var ()
sxGoodsService *SxGoodsService
)
type SxGoodsService struct { type SxGoodsService struct {
db *gorm.DB // Add database connection db *gorm.DB // Add database connection
} }
func NewSxGoodsService() *SxGoodsService { // Pass database connection func NewSxGoodsService() *SxGoodsService {
once.Do(func() {
sxGoodsService = &SxGoodsService{db: dbGorm} // Initialize with database connection
})
return sxGoodsService return sxGoodsService
} }
@ -190,6 +185,7 @@ func (s *SxGoodsService) JsonDataConvBeans(path string) (srcDataCnt, targetDataC
if product.Category2Nd == "" { if product.Category2Nd == "" {
product.Category2Nd = product.Category1St product.Category2Nd = product.Category1St
} }
product.Category = product.Category2Nd
// 如果Intro为空使用Title的值 // 如果Intro为空使用Title的值
if product.Intro == "" { if product.Intro == "" {

View File

@ -1 +0,0 @@
package services