增加uinx配置
This commit is contained in:
+36
-3
@@ -11,10 +11,12 @@ import (
|
||||
"log" // 日志
|
||||
"math" // 数学运算(Log、幂)
|
||||
"math/rand" // 随机数(刷盘时打乱顺序、概率性去重/裁剪)
|
||||
"net" // net.Listen(Unix socket)
|
||||
"net/http" // HTTP 服务端
|
||||
"net/url" // URL 解析
|
||||
"os" // 文件系统(静态文件读取)
|
||||
"regexp" // 正则表达式(site: 过滤语法)
|
||||
"runtime" // runtime.GOOS(平台判断)
|
||||
"sort" // 排序
|
||||
"strconv" // 字符串转整数
|
||||
"strings" // 字符串操作(URL 清洗)
|
||||
@@ -324,15 +326,46 @@ func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// ListenAndServe 启动搜索服务器(带超时保护)。
|
||||
func (s *Server) ListenAndServe(addr string) error {
|
||||
log.Printf("[search] listening on %s", addr)
|
||||
// 在 Linux/macOS 下,若 unixSocket 非空,则同时在 TCP 和 Unix socket 上监听。
|
||||
// 在 Windows 下,unixSocket 参数被忽略,只监听 TCP。
|
||||
func (s *Server) ListenAndServe(addr string, unixSocket string) error {
|
||||
handler := s.Handler()
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: s.Handler(),
|
||||
Handler: handler,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 60 * time.Second,
|
||||
IdleTimeout: 120 * time.Second,
|
||||
}
|
||||
|
||||
// Linux/macOS 下且配置了 socket 路径时,额外启动 Unix socket 监听
|
||||
if unixSocket != "" && runtime.GOOS != "windows" {
|
||||
// 清理旧的 socket 文件(上次异常退出可能残留)
|
||||
_ = os.Remove(unixSocket)
|
||||
ln, err := net.Listen("unix", unixSocket)
|
||||
if err != nil {
|
||||
log.Printf("[search] unix socket failed (%s): %v — continuing with TCP only", unixSocket, err)
|
||||
} else {
|
||||
// 设置 socket 文件权限:www 用户和 nginx 都可访问
|
||||
if err := os.Chmod(unixSocket, 0660); err != nil {
|
||||
log.Printf("[search] chmod unix socket: %v", err)
|
||||
}
|
||||
log.Printf("[search] also listening on unix:%s", unixSocket)
|
||||
unixSrv := &http.Server{
|
||||
Handler: handler,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 60 * time.Second,
|
||||
IdleTimeout: 120 * time.Second,
|
||||
}
|
||||
go func() {
|
||||
if err := unixSrv.Serve(ln); err != nil && err != http.ErrServerClosed {
|
||||
log.Printf("[search] unix socket serve error: %v", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("[search] listening on %s", addr)
|
||||
return srv.ListenAndServe()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user