]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20250319113646
authorqydysky <qydysky@foxmail.com>
Wed, 19 Mar 2025 11:36:34 +0000 (11:36 +0000)
committerGitHub <noreply@github.com>
Wed, 19 Mar 2025 11:36:34 +0000 (11:36 +0000)
web/Web.go
web/Web_test.go

index ab7ba8461d898d0190a887c68ed17665765265f1..87c6b2e093265374b0d28470063a022de5ee2b25 100644 (file)
@@ -6,6 +6,7 @@ import (
        "errors"
        "net"
        "net/http"
+       "os"
        "strconv"
        "strings"
        "sync"
@@ -53,19 +54,28 @@ func (t *Web) Shutdown(ctx ...context.Context) {
 
 type WebSync struct {
        Server *http.Server
+       FD     uintptr
        mux    *http.ServeMux
        wrs    *WebPath
 }
 
 func NewSyncMap(conf *http.Server, m *WebPath, matchFunc ...func(path string) (func(w http.ResponseWriter, r *http.Request), bool)) (o *WebSync) {
-       if o, e := NewSyncMapNoPanic(conf, m, matchFunc...); e != nil {
+       if o, e := NewSyncMapNoPanic(conf, 0, m, matchFunc...); e != nil {
                panic(e)
        } else {
                return o
        }
 }
 
-func NewSyncMapNoPanic(conf *http.Server, m *WebPath, matchFunc ...func(path string) (func(w http.ResponseWriter, r *http.Request), bool)) (o *WebSync, err error) {
+func NewSyncMapFD(conf *http.Server, fd uintptr, m *WebPath, matchFunc ...func(path string) (func(w http.ResponseWriter, r *http.Request), bool)) (o *WebSync) {
+       if o, e := NewSyncMapNoPanic(conf, fd, m, matchFunc...); e != nil {
+               panic(e)
+       } else {
+               return o
+       }
+}
+
+func NewSyncMapNoPanic(conf *http.Server, fd uintptr, m *WebPath, matchFunc ...func(path string) (func(w http.ResponseWriter, r *http.Request), bool)) (o *WebSync, err error) {
 
        o = new(WebSync)
 
@@ -77,13 +87,35 @@ func NewSyncMapNoPanic(conf *http.Server, m *WebPath, matchFunc ...func(path str
 
        matchFunc = append(matchFunc, o.wrs.Load)
 
-       ln, err := net.Listen("tcp", conf.Addr)
-       if err != nil {
-               return nil, err
+       var ln net.Listener
+       if fd != 0 {
+               if tmp, err := net.FileListener(os.NewFile(fd, "")); err != nil {
+                       return nil, err
+               } else {
+                       ln = tmp
+               }
+       } else {
+               if tmp, err := net.Listen("tcp", conf.Addr); err != nil {
+                       return nil, err
+               } else {
+                       ln = tmp
+               }
        }
        if conf.TLSConfig != nil {
                ln = tls.NewListener(ln, conf.TLSConfig)
        }
+       switch t := ln.(type) {
+       case *net.TCPListener:
+               c, _ := t.SyscallConn()
+               c.Control(func(fd uintptr) {
+                       o.FD = fd
+               })
+       case *net.UnixListener:
+               c, _ := t.SyscallConn()
+               c.Control(func(fd uintptr) {
+                       o.FD = fd
+               })
+       }
        go o.Server.Serve(ln)
 
        if o.Server.Handler == nil {
index 634cc11e019e62cef57176845d2acced98407083..5840f6177ce013c71be8e5488b89fcb54ec6ce46 100644 (file)
@@ -17,13 +17,66 @@ import (
        reqf "github.com/qydysky/part/reqf"
 )
 
+func TestMain1(t *testing.T) {
+       var fd uintptr
+       r := reqf.New()
+       var stop func(ctx ...context.Context)
+       var stop2 func(ctx ...context.Context)
+       {
+               ser := &http.Server{Addr: "127.0.0.1:18081"}
+               wp := &WebPath{}
+               wp.Store("/test/", func(w http.ResponseWriter, r *http.Request) {
+                       w.Write([]byte("1"))
+               })
+               o, e := NewSyncMapNoPanic(ser, 0, wp, wp.Load)
+               if e != nil {
+                       t.Fatal(e)
+               }
+               fd = o.FD
+               stop = o.Shutdown
+       }
+       time.Sleep(time.Second)
+       {
+               r.Reqf(reqf.Rval{
+                       Url: "http://127.0.0.1:18081/test/",
+               })
+               if !bytes.Equal(r.Respon, []byte("1")) {
+                       t.Fatal(r.Respon)
+               }
+       }
+       {
+               ser := &http.Server{Addr: "127.0.0.1:18082"}
+               wp := &WebPath{}
+               wp.Store("/test/", func(w http.ResponseWriter, r *http.Request) {
+                       w.Write([]byte("2"))
+               })
+               o, e := NewSyncMapNoPanic(ser, fd, wp, wp.Load)
+               if e != nil {
+                       t.Fatal(e)
+               }
+               fd = o.FD
+               stop2 = o.Shutdown
+       }
+       stop()
+       time.Sleep(time.Second)
+       {
+               r.Reqf(reqf.Rval{
+                       Url: "http://127.0.0.1:18081/test/",
+               })
+               if !bytes.Equal(r.Respon, []byte("2")) {
+                       t.Fatal(r.Respon)
+               }
+       }
+       stop2()
+}
+
 func TestMain(t *testing.T) {
        ser := &http.Server{
                Addr: "127.0.0.1:18081",
        }
        wp := &WebPath{}
-       t.Log(NewSyncMapNoPanic(ser, wp, wp.Load))
-       t.Log(NewSyncMapNoPanic(ser, wp, wp.Load))
+       t.Log(NewSyncMapNoPanic(ser, 0, wp, wp.Load))
+       t.Log(NewSyncMapNoPanic(ser, 0, wp, wp.Load))
 }
 
 func Test_Exprier(t *testing.T) {