36 lines
949 B
Go
36 lines
949 B
Go
package scraper
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type Handler struct {
|
|
db *pgxpool.Pool
|
|
sources []Source
|
|
}
|
|
|
|
func NewHandler(db *pgxpool.Pool, sources []Source) *Handler {
|
|
return &Handler{db: db, sources: sources}
|
|
}
|
|
|
|
// Refresh is owner/manager-triggered (catalogs permission, same gate as
|
|
// production_recipes) — synchronous on purpose: a handful of GET requests
|
|
// against one public site finishes in a couple of seconds, and staff
|
|
// clicking "Обновить" want to see the result land, not poll a job status.
|
|
// StartPeriodic (scraper.go) covers the unattended case.
|
|
func (h *Handler) Refresh(c *fiber.Ctx) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
|
|
errs := RefreshAll(ctx, h.db, h.sources)
|
|
out := fiber.Map{}
|
|
for k, err := range errs {
|
|
out[k] = err.Error()
|
|
}
|
|
return c.JSON(fiber.Map{"errors": out})
|
|
}
|