From 5d7f558ed4df923c24cb49c4060558cde20b4875 Mon Sep 17 00:00:00 2001 From: nite07 Date: Thu, 14 Nov 2024 18:57:11 +0800 Subject: [PATCH] feat: add task webhooks --- .gitattributes | 1 + build.sh | 0 config/config.go | 5 +++++ task/crawl.go | 23 +++++++++++++++++++++++ 4 files changed, 29 insertions(+) create mode 100644 .gitattributes mode change 100644 => 100755 build.sh diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 diff --git a/config/config.go b/config/config.go index 6ef7356..ab251ee 100644 --- a/config/config.go +++ b/config/config.go @@ -17,12 +17,17 @@ type config struct { Redis redis `json:"redis"` OnlineFix onlinefix `json:"online_fix"` Twitch twitch `json:"twitch"` + Webhooks webhooks `json:"webhooks"` DatabaseAvaliable bool OnlineFixAvaliable bool MegaAvaliable bool RedisAvaliable bool } +type webhooks struct { + CrawlTask []string `env:"WEBHOOKS_ERROR_TASK" json:"crawl_task"` +} + type server struct { Port string `env:"SERVER_PORT" json:"port"` SecretKey string `env:"SERVER_SECRET_KEY" json:"secret_key"` diff --git a/task/crawl.go b/task/crawl.go index d0493e5..0413ab5 100644 --- a/task/crawl.go +++ b/task/crawl.go @@ -1,8 +1,12 @@ package task import ( + "net/http" + "net/url" + "pcgamedb/config" "pcgamedb/crawler" "pcgamedb/model" + "pcgamedb/utils" "go.uber.org/zap" ) @@ -35,4 +39,23 @@ func Crawl(logger *zap.Logger) { ) } Clean(logger) + for _, u := range config.Config.Webhooks.CrawlTask { + _, err := url.Parse(u) + if err != nil { + logger.Error("Invalid webhook url", zap.String("url", u), zap.Error(err)) + continue + } + logger.Info("webhook triggered", zap.String("task", "crawl"), zap.String("url", u)) + _, err = utils.Fetch(utils.FetchConfig{ + Url: u, + Method: http.MethodPost, + Headers: map[string]string{ + "Content-Type": "application/json", + }, + Data: games, + }) + if err != nil { + logger.Error("Failed to trigger webhook", zap.String("task", "crawl"), zap.String("url", u), zap.Error(err)) + } + } }