feat: add task webhooks
This commit is contained in:
parent
1d873c9cea
commit
5d7f558ed4
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
* text=auto eol=lf
|
@ -17,12 +17,17 @@ type config struct {
|
|||||||
Redis redis `json:"redis"`
|
Redis redis `json:"redis"`
|
||||||
OnlineFix onlinefix `json:"online_fix"`
|
OnlineFix onlinefix `json:"online_fix"`
|
||||||
Twitch twitch `json:"twitch"`
|
Twitch twitch `json:"twitch"`
|
||||||
|
Webhooks webhooks `json:"webhooks"`
|
||||||
DatabaseAvaliable bool
|
DatabaseAvaliable bool
|
||||||
OnlineFixAvaliable bool
|
OnlineFixAvaliable bool
|
||||||
MegaAvaliable bool
|
MegaAvaliable bool
|
||||||
RedisAvaliable bool
|
RedisAvaliable bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type webhooks struct {
|
||||||
|
CrawlTask []string `env:"WEBHOOKS_ERROR_TASK" json:"crawl_task"`
|
||||||
|
}
|
||||||
|
|
||||||
type server struct {
|
type server struct {
|
||||||
Port string `env:"SERVER_PORT" json:"port"`
|
Port string `env:"SERVER_PORT" json:"port"`
|
||||||
SecretKey string `env:"SERVER_SECRET_KEY" json:"secret_key"`
|
SecretKey string `env:"SERVER_SECRET_KEY" json:"secret_key"`
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
package task
|
package task
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"pcgamedb/config"
|
||||||
"pcgamedb/crawler"
|
"pcgamedb/crawler"
|
||||||
"pcgamedb/model"
|
"pcgamedb/model"
|
||||||
|
"pcgamedb/utils"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
@ -35,4 +39,23 @@ func Crawl(logger *zap.Logger) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
Clean(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))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user