All checks were successful
CI / build (push) Successful in 38s
- ComputeDailyTotals: only assume working from dayStart when first event is 'out' (cross-midnight session); otherwise start idle. Fixes inflated work/break counts in reports, status, export, burnout, salary, and RPG. - leagueToggleCallback: add settings back keyboard when RPG is disabled. - webhook: add graceful HTTP server shutdown on context cancellation.
66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
tgbot "github.com/go-telegram/bot"
|
|
|
|
"worktimeBot/internal/handler"
|
|
)
|
|
|
|
// startWebhook registers the webhook with Telegram and starts the HTTPS/HTTP listener loop.
|
|
func startWebhook(ctx context.Context, b *tgbot.Bot, h *handler.Handler, webhookURL string) {
|
|
if _, err := b.DeleteWebhook(ctx, &tgbot.DeleteWebhookParams{}); err != nil {
|
|
slog.Warn("failed to delete webhook", "error", err)
|
|
}
|
|
|
|
if _, err := b.SetWebhook(ctx, &tgbot.SetWebhookParams{URL: webhookURL}); err != nil {
|
|
slog.Error("set webhook", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
listenAddr := getEnvDefault("BOT_LISTEN", ":8080")
|
|
tlsCert := os.Getenv("BOT_TLS_CERT")
|
|
tlsKey := os.Getenv("BOT_TLS_KEY")
|
|
|
|
server := &http.Server{
|
|
Addr: listenAddr,
|
|
Handler: b.WebhookHandler(),
|
|
}
|
|
|
|
go func() {
|
|
<-ctx.Done()
|
|
slog.Info("shutting down webhook server")
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
if err := server.Shutdown(shutdownCtx); err != nil {
|
|
slog.Error("webhook server shutdown", "error", err)
|
|
}
|
|
}()
|
|
|
|
if tlsCert != "" && tlsKey != "" {
|
|
slog.Info("starting HTTPS webhook", "addr", listenAddr)
|
|
go func() {
|
|
if err := server.ListenAndServeTLS(tlsCert, tlsKey); err != nil && err != http.ErrServerClosed {
|
|
slog.Error("HTTPS server", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
}()
|
|
} else {
|
|
slog.Info("starting HTTP webhook (TLS by reverse proxy)", "addr", listenAddr)
|
|
go func() {
|
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
slog.Error("HTTP server", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
}()
|
|
}
|
|
|
|
slog.Info("webhook registered", "url", webhookURL)
|
|
b.StartWebhook(ctx)
|
|
}
|