]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20241021164252
authorqydysky <qydysky@foxmail.com>
Mon, 21 Oct 2024 16:37:52 +0000 (00:37 +0800)
committerqydysky <qydysky@foxmail.com>
Mon, 21 Oct 2024 16:37:52 +0000 (00:37 +0800)
web/Web.go
web/Web_test.go

index 439d01a5b86e176c4fcde6d1e3c0eefe3be286a9..c9a4a9d37fba3e3508f2f724f8dd1421ab0764b2 100644 (file)
@@ -57,6 +57,14 @@ type WebSync struct {
 }
 
 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 {
+               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) {
 
        o = new(WebSync)
 
@@ -66,22 +74,22 @@ func NewSyncMap(conf *http.Server, m *WebPath, matchFunc ...func(path string) (f
        o.Server = conf
        o.wrs = m
 
-       if o.Server.Handler == nil {
-               o.mux = http.NewServeMux()
-               o.Server.Handler = o.mux
-       }
-
        matchFunc = append(matchFunc, o.wrs.Load)
 
        ln, err := net.Listen("tcp", conf.Addr)
        if err != nil {
-               panic(err)
+               return nil, err
        }
        if conf.TLSConfig != nil {
                ln = tls.NewListener(ln, conf.TLSConfig)
        }
        go o.Server.Serve(ln)
 
+       if o.Server.Handler == nil {
+               o.mux = http.NewServeMux()
+               o.Server.Handler = o.mux
+       }
+
        o.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
                f, ok := matchFunc[0](r.URL.Path)
                if ok {
index da5456930a53a750f15ef03d651d94d880660ab1..634cc11e019e62cef57176845d2acced98407083 100644 (file)
@@ -17,6 +17,15 @@ import (
        reqf "github.com/qydysky/part/reqf"
 )
 
+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))
+}
+
 func Test_Exprier(t *testing.T) {
        exp := NewExprier(1)
        if key, e := exp.Reg(time.Second); e != nil {
@@ -144,6 +153,24 @@ func Test_path(t *testing.T) {
        failIfNot(t, res, "f1f2f1")
 }
 
+func Test_path2(t *testing.T) {
+       var m WebPath
+       var res string
+       var f1 = func(_ http.ResponseWriter, _ *http.Request) { res += "f1" }
+       var f2 = func(_ http.ResponseWriter, _ *http.Request) { res += "f2" }
+       m.Store("/1", f1)
+       failIfNot(t, res, "")
+       if sf1, ok := m.Load("/1"); ok {
+               sf1(nil, nil)
+       }
+       failIfNot(t, res, "f1")
+       m.Store("/1", f2)
+       if sf1, ok := m.Load("/1"); ok {
+               sf1(nil, nil)
+       }
+       failIfNot(t, res, "f1f2")
+}
+
 func Test_Store(t *testing.T) {
        var webPath = WebPath{}
        var res = ""
@@ -622,3 +649,35 @@ func Test1(b *testing.T) {
                }
        }
 }
+
+func Test_limit(t *testing.T) {
+       host, _, _ := net.SplitHostPort("[fe80::aab8:e0ff:fe03:8ce5]:80")
+       // if _, cidrx, err := net.ParseCIDR("::/0"); err != nil {
+       //      panic(err)
+       // } else {
+       t.Log(host)
+       // }
+
+       limit := Limits{}
+       limit.AddLimitItem(NewLimitItem(0).Cidr("::/0"))
+
+       var m WebPath
+       m.Store("/", func(w http.ResponseWriter, r *http.Request) {
+               if limit.AddCount(r) {
+                       w.Write([]byte("fail"))
+               }
+               t.Log(limit.g[0].available)
+               w.Write([]byte("ok"))
+               time.Sleep(time.Second)
+       })
+
+       o := NewSyncMap(&http.Server{
+               Addr: "127.0.0.1:13003",
+       }, &m)
+       defer o.Shutdown()
+
+       r := reqf.New()
+       r.Reqf(reqf.Rval{
+               Url: "http://localhost:13003/",
+       })
+}