feat: add task webhooks

This commit is contained in:
Nite07 2024-11-14 18:57:11 +08:00
parent 1d873c9cea
commit 5d7f558ed4
4 changed files with 29 additions and 0 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
* text=auto eol=lf

0
build.sh Normal file → Executable file
View File

View File

@ -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"`

View File

@ -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))
}
}
}