add idle support
This commit is contained in:
		
							
								
								
									
										4
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.mod
									
									
									
									
									
								
							@@ -1,5 +1,5 @@
 | 
				
			|||||||
module go.balki.me/tglistbot
 | 
					module go.balki.me/tglistbot
 | 
				
			||||||
 | 
					
 | 
				
			||||||
go 1.20
 | 
					go 1.21
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require go.balki.me/anyhttp v0.2.0
 | 
					require go.balki.me/anyhttp v0.3.0
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.sum
									
									
									
									
									
								
							@@ -1,2 +1,2 @@
 | 
				
			|||||||
go.balki.me/anyhttp v0.2.0 h1:W6aGcmjF5CMJvJYtbYCywxnYoErFhFc76vwaqUG5FAQ=
 | 
					go.balki.me/anyhttp v0.3.0 h1:WtBQ0rnkg567sX/O4ij/+qBbdCIUt5VURSe718sITBY=
 | 
				
			||||||
go.balki.me/anyhttp v0.2.0/go.mod h1:JhfekOIjgVODoVqUCficjpIgmB3wwlB7jhN0eN2EZ/s=
 | 
					go.balki.me/anyhttp v0.3.0/go.mod h1:JhfekOIjgVODoVqUCficjpIgmB3wwlB7jhN0eN2EZ/s=
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										36
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								main.go
									
									
									
									
									
								
							@@ -3,6 +3,7 @@ package main
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
@@ -16,6 +17,7 @@ import (
 | 
				
			|||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"go.balki.me/anyhttp"
 | 
						"go.balki.me/anyhttp"
 | 
				
			||||||
 | 
						"go.balki.me/anyhttp/idle"
 | 
				
			||||||
	"go.balki.me/tglistbot/glist"
 | 
						"go.balki.me/tglistbot/glist"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -26,8 +28,9 @@ var apiToken string
 | 
				
			|||||||
var usage string = `Telegram List bot
 | 
					var usage string = `Telegram List bot
 | 
				
			||||||
Environment variables:
 | 
					Environment variables:
 | 
				
			||||||
TGLB_API_TOKEN (required)      : See https://core.telegram.org/bots#how-do-i-create-a-bot
 | 
					TGLB_API_TOKEN (required)      : See https://core.telegram.org/bots#how-do-i-create-a-bot
 | 
				
			||||||
TGLB_PORT (default 28923): Set numerical port or unix//run/path.sock for unix socket
 | 
					TGLB_ADDR 	   (default 28923) : See https://pkg.go.dev/go.balki.me/anyhttp#readme-address-syntax
 | 
				
			||||||
TGLB_DATA_PATH (default .)     : Directory path where list data is stored
 | 
					TGLB_DATA_PATH (default .)     : Directory path where list data is stored
 | 
				
			||||||
 | 
					TGLB_TIMEOUT   (default 30m)   : Timeout to auto shutdown if using systemd-fd, See https://pkg.go.dev/time#ParseDuration
 | 
				
			||||||
`
 | 
					`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
@@ -60,6 +63,18 @@ func main() {
 | 
				
			|||||||
		return dataPath
 | 
							return dataPath
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						timeout := func() time.Duration {
 | 
				
			||||||
 | 
							timeoutStr := os.Getenv("TGLB_TIMEOUT")
 | 
				
			||||||
 | 
							if timeoutStr == "" {
 | 
				
			||||||
 | 
								return 30 * time.Minute
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							timeout, err := time.ParseDuration(timeoutStr)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								log.Panicf("Invalid timeout: %q\n", timeoutStr)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return timeout
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	glist.DataPath = dataPath
 | 
						glist.DataPath = dataPath
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	version := func() string {
 | 
						version := func() string {
 | 
				
			||||||
@@ -154,7 +169,24 @@ func main() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	log.Panicln(anyhttp.ListenAndServe(addr, nil))
 | 
						addrType, server, done, err := anyhttp.Serve(addr, idle.WrapHandler(nil))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Panicln(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if addrType == anyhttp.SystemdFD {
 | 
				
			||||||
 | 
							log.Println("server started")
 | 
				
			||||||
 | 
							if err := idle.Wait(timeout); err != nil {
 | 
				
			||||||
 | 
								log.Panicln(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							log.Printf("server idle for %v, shutting down\n", timeout)
 | 
				
			||||||
 | 
							ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) // Don't want any stuck connections
 | 
				
			||||||
 | 
							defer cancel()
 | 
				
			||||||
 | 
							if err := server.Shutdown(ctx); err != nil {
 | 
				
			||||||
 | 
								log.Panicln(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							<-done
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func handleTextAdded(gl *glist.GList, text string) {
 | 
					func handleTextAdded(gl *glist.GList, text string) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user