Compare commits

...

1 Commits

Author SHA1 Message Date
eleksir
11ea7b8398 Select correct lang in request, use timeout for request, fix sighandler panic 2026-05-03 16:41:32 +03:00
5 changed files with 19 additions and 5 deletions

View File

@@ -1,7 +1,7 @@
## tg-holy-bot ## tg-holy-bot
Телеграммный бот для канала с праздниками. Раз в сутки забирает список праздников с к сайта celebratoday.com и Телеграммный бот для канала с праздниками. Раз в сутки забирает список праздников с к сайта celebratoday.com и
показывает его на канальчике в телеграме. показывает его на канальчике в телеграме. Создан исключительно в обучающих целях, как пример приложения на языке go.
## Как это собрать? ## Как это собрать?

View File

@@ -26,10 +26,12 @@ func APIClient() (string, error) {
_, _, day := time.Now().Date() _, _, day := time.Now().Date()
userAgent := Config.UserAgents[day-1] userAgent := Config.UserAgents[day-1]
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
if err := requests.URL(url).UserAgent(userAgent). if err := requests.URL(url).UserAgent(userAgent).
Header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"). Header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8").
Header("Accept-Language", "en-US,en;q=0.5"). Header("Accept-Language", "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7").
Header("User-Agent", "ru-RU").
Header("Accept-Charset", "utf-8").ToString(&respBody).Fetch(ctx); err != nil { Header("Accept-Charset", "utf-8").ToString(&respBody).Fetch(ctx); err != nil {
return "", fmt.Errorf("Unable to GET %s: %w", url, err) //nolint: staticcheck // Looks like golangci-lint ignoring settings about fucking ST1005 check. We want readable errors despite what wants author of linter or assholes from go dev team. return "", fmt.Errorf("Unable to GET %s: %w", url, err) //nolint: staticcheck // Looks like golangci-lint ignoring settings about fucking ST1005 check. We want readable errors despite what wants author of linter or assholes from go dev team.
} }

View File

@@ -5,6 +5,8 @@ import (
"time" "time"
) )
const maxRetryAttempts = 15
// CronTask sends generated holiday list to the channel. // CronTask sends generated holiday list to the channel.
func CronTask() { func CronTask() {
var ( var (
@@ -22,7 +24,7 @@ func CronTask() {
if err != nil { if err != nil {
errcnt++ errcnt++
log.Errorf("Unable to get holidays (try %d/15): %s", errcnt, err) log.Errorf("Unable to get holidays (try %d/%d): %s", errcnt, maxRetryAttempts, err)
switch errcnt { switch errcnt {
case 1, 2: case 1, 2:

View File

@@ -3,6 +3,7 @@ package holy
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"os" "os"
"tg-holy-bot/internal/log" "tg-holy-bot/internal/log"
@@ -103,6 +104,13 @@ func ReadConfig() error {
return errors.New("Amount of user_agent strings in config must be exactly 31! Refusing to start") //nolint: staticcheck // Looks like golangci-lint ignoring settings about fucking ST1005 check. We want readable errors despite what wants author of linter or assholes from go dev team. return errors.New("Amount of user_agent strings in config must be exactly 31! Refusing to start") //nolint: staticcheck // Looks like golangci-lint ignoring settings about fucking ST1005 check. We want readable errors despite what wants author of linter or assholes from go dev team.
} }
// Validate that all user agent strings are non-empty
for i, userAgent := range sampleConfig.UserAgents {
if userAgent == "" {
return fmt.Errorf("User agent string at index %d is empty! Refusing to start", i) //nolint: staticcheck // Looks like golangci-lint ignoring settings about fucking ST1005 check. We want readable errors despite what wants author of linter or assholes from go dev team.
}
}
log.Infof("Using %s as config file", location) log.Infof("Using %s as config file", location)
Config = sampleConfig Config = sampleConfig

View File

@@ -24,8 +24,10 @@ func SigHandler() {
} }
Shutdown = true Shutdown = true
close(Run) close(Run)
// Ensure we exit cleanly after handling the signal
return
} }
} }