diff --git a/examples/ngrok/main.go b/examples/ngrok/main.go index ba400bf..5c2e234 100644 --- a/examples/ngrok/main.go +++ b/examples/ngrok/main.go @@ -4,6 +4,8 @@ import ( "context" "fmt" "os" + "os/signal" + "syscall" "github.com/fasthttp/router" "github.com/valyala/fasthttp" @@ -23,10 +25,15 @@ func main() { os.Exit(1) } - ctx := context.Background() + // Initialize signal handling + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + + // Initialize done chan + done := make(chan struct{}, 1) // Create a new Ngrok tunnel to connect local network with the Internet & have HTTPS domain for bot - tun, err := ngrok.Listen(ctx, + tun, err := ngrok.Listen(context.Background(), // Forward connections to localhost:8080 config.HTTPEndpoint(config.WithForwardsTo(":8080")), // Authenticate into Ngrok using NGROK_AUTHTOKEN env (optional) @@ -50,6 +57,7 @@ func main() { Router: router.New(), }, // Override default start func to use Ngrok tunnel + // Note: When server is stopped, the Ngrok tunnel always returns an error, so it should be handled by user StartFunc: func(_ string) error { return srv.Serve(tun) }, @@ -61,18 +69,34 @@ func main() { }), ) + // Handle stop signal (Ctrl+C) + go func() { + // Wait for stop signal + <-sigs + + fmt.Println("Stopping...") + + // Stop reviving updates from update channel and shutdown webhook server + _ = bot.StopWebhook() + fmt.Println("Webhook done") + + // Notify that stop is done + done <- struct{}{} + }() + // Start server for receiving requests from the Telegram go func() { _ = bot.StartWebhook("") }() - // Stop reviving updates from update channel and shutdown webhook server - defer func() { - _ = bot.StopWebhook() + // Loop through all updates when they came + go func() { + for update := range updates { + fmt.Printf("Update: %+v\n", update) + } }() - // Loop through all updates when they came - for update := range updates { - fmt.Printf("Update: %+v\n", update) - } + // Wait for the stop process to be completed + <-done + fmt.Println("Done") } diff --git a/webhook.go b/webhook.go index b65ed51..7dc0de8 100644 --- a/webhook.go +++ b/webhook.go @@ -191,8 +191,10 @@ func (b *Bot) StartWebhook(address string) error { if err := ctx.server.Start(address); err != nil { ctx.runningLock.Lock() - ctx.running = false - close(ctx.stop) + if ctx.running { + close(ctx.stop) + ctx.running = false + } b.webhookContext = nil ctx.runningLock.Unlock()