From: qydysky Date: Mon, 21 Oct 2024 16:37:52 +0000 (+0800) Subject: 1 X-Git-Tag: v0.28.20241021164252 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=5088086e76b5db149f1f9d2b027c4369c4c20e29;p=part%2F.git 1 --- diff --git a/web/Web.go b/web/Web.go index 439d01a..c9a4a9d 100644 --- a/web/Web.go +++ b/web/Web.go @@ -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 { diff --git a/web/Web_test.go b/web/Web_test.go index da54569..634cc11 100644 --- a/web/Web_test.go +++ b/web/Web_test.go @@ -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/", + }) +}