Replace unix socket logic with go.balki.me/anyhttp
Now also support systemd socket activation
This commit is contained in:
		
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							@@ -1,3 +1,5 @@
 | 
				
			|||||||
module go.balki.me/tglistbot
 | 
					module go.balki.me/tglistbot
 | 
				
			||||||
 | 
					
 | 
				
			||||||
go 1.20
 | 
					go 1.20
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					require go.balki.me/anyhttp v0.1.0
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
				
			|||||||
 | 
					go.balki.me/anyhttp v0.1.0 h1:ULzLWS1pRWMEduHHJxXCbvxoTmxNaWSNANV9gQ0Pigw=
 | 
				
			||||||
 | 
					go.balki.me/anyhttp v0.1.0/go.mod h1:JhfekOIjgVODoVqUCficjpIgmB3wwlB7jhN0eN2EZ/s=
 | 
				
			||||||
							
								
								
									
										50
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										50
									
								
								main.go
									
									
									
									
									
								
							@@ -4,21 +4,18 @@ package main
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"errors"
 | 
					 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"io/fs"
 | 
					 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"net"
 | 
					 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path"
 | 
						"path"
 | 
				
			||||||
	"runtime/debug"
 | 
						"runtime/debug"
 | 
				
			||||||
	"strconv"
 | 
					 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"go.balki.me/anyhttp"
 | 
				
			||||||
	"go.balki.me/tglistbot/glist"
 | 
						"go.balki.me/tglistbot/glist"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -42,19 +39,12 @@ func main() {
 | 
				
			|||||||
		log.Panicln("TGLB_API_TOKEN is empty")
 | 
							log.Panicln("TGLB_API_TOKEN is empty")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	port, unixSocketPath := func() (int, string) {
 | 
						addr := func() string {
 | 
				
			||||||
		portStr := os.Getenv("TGLB_PORT")
 | 
							addr := os.Getenv("TGLB_PORT")
 | 
				
			||||||
 | 
							if addr == "" {
 | 
				
			||||||
		defaultPort := 28923
 | 
								return "28923"
 | 
				
			||||||
 | 
					 | 
				
			||||||
		if strings.HasPrefix(portStr, "unix/") {
 | 
					 | 
				
			||||||
			return defaultPort, strings.TrimPrefix(portStr, "unix/")
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							return addr
 | 
				
			||||||
		if port, err := strconv.Atoi(portStr); err == nil {
 | 
					 | 
				
			||||||
			return port, ""
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		return defaultPort, ""
 | 
					 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	dataPath := func() string {
 | 
						dataPath := func() string {
 | 
				
			||||||
@@ -85,14 +75,7 @@ func main() {
 | 
				
			|||||||
		return "unknown"
 | 
							return "unknown"
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	listeningOn := func() string {
 | 
						log.Printf("List bot (%s) starting with datapath: %q, %s\n", version, dataPath, addr)
 | 
				
			||||||
		if unixSocketPath != "" {
 | 
					 | 
				
			||||||
			return fmt.Sprintf("socket: %q", unixSocketPath)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		return fmt.Sprintf("port: %v", port)
 | 
					 | 
				
			||||||
	}()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	log.Printf("List bot (%s) starting with datapath: %q, %s\n", version, dataPath, listeningOn)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var chats sync.Map
 | 
						var chats sync.Map
 | 
				
			||||||
	if err := loadData(dataPath, &chats); err != nil {
 | 
						if err := loadData(dataPath, &chats); err != nil {
 | 
				
			||||||
@@ -160,24 +143,7 @@ func main() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if unixSocketPath != "" {
 | 
						log.Panicln(anyhttp.ListenAndServe(addr, nil))
 | 
				
			||||||
		// Remove old one
 | 
					 | 
				
			||||||
		if err := os.Remove(unixSocketPath); err != nil && !errors.Is(err, fs.ErrNotExist) {
 | 
					 | 
				
			||||||
			log.Panicf("Failed to remove unix socket : %q err: %v\n", unixSocketPath, err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		l, err := net.Listen("unix", unixSocketPath)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			log.Panicf("Unable to listen to unix socket : %q err: %v\n", unixSocketPath, err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if err = os.Chmod(unixSocketPath, 0666); err != nil {
 | 
					 | 
				
			||||||
			log.Panicf("Failed to set permission of unix socket %q err: %v\n", unixSocketPath, err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		log.Panicln(http.Serve(l, nil))
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	log.Panicln(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func handleTextAdded(gl *glist.GList, text string) {
 | 
					func handleTextAdded(gl *glist.GList, text string) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user