]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20231202005014
authorqydysky <qydysky@foxmail.com>
Sat, 2 Dec 2023 00:47:01 +0000 (08:47 +0800)
committerqydysky <qydysky@foxmail.com>
Sat, 2 Dec 2023 00:47:01 +0000 (08:47 +0800)
.github/workflows/test.yml
web/Web.go
web/Web_test.go

index 14eb2772f0723009a5f600433a3790c502164b3a..a9f0cd67a8b601917d977e192eb441f8d602992e 100644 (file)
@@ -43,10 +43,10 @@ jobs:
 
     - name: Set Release Name
       run: |
-        echo "TIME=$(date +"%Y%m%d")" >> $GITHUB_ENV
+        echo "TIME=$(date +"%Y%m%d%H%M%S")" >> $GITHUB_ENV
         echo "HASH=$(git rev-parse --short "$GITHUB_SHA")" >> $GITHUB_ENV
 
     - name: Upload a Release Asset
       uses: softprops/action-gh-release@v1
       with:
-        tag_name: v0.28.0+${{ env.TIME }}${{ env.HASH }}
+        tag_name: v0.28.${{ env.TIME }}
index e46495ff183264209eee55afca180758ddad7438..a60eb00caa9c2ddd72cc41ad789204ec264d86c8 100644 (file)
@@ -70,8 +70,11 @@ func NewSyncMap(conf *http.Server, m *WebPath) (o *WebSync) {
        go o.Server.ListenAndServe()
 
        o.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
-               if f, ok := o.wrs.Load(r.URL.Path); ok {
+               f, ok := o.wrs.Load(r.URL.Path)
+               if ok {
                        f(w, r)
+               } else {
+                       w.WriteHeader(http.StatusNotFound)
                }
        })
 
@@ -94,15 +97,13 @@ type WebPath struct {
 func (t *WebPath) Load(path string) (func(w http.ResponseWriter, r *http.Request), bool) {
        t.RLock()
        defer t.RUnlock()
-       if t.path == path || t.f == nil {
+       if t.path == path {
                // 操作本节点
-               return t.f, true
+               return t.f, t.f != nil
        } else if lp, ltp := len(path), len(t.path); lp > ltp && path[:ltp] == t.path && (path[ltp] == '/' || t.path[ltp-1] == '/') {
                // 操作sameP节点
                if t.sameP != nil {
-                       if f, ok := t.sameP.Load(path); ok {
-                               return f, true
-                       }
+                       return t.sameP.Load(path)
                }
                if t.path[ltp-1] == '/' {
                        return t.f, true
@@ -115,9 +116,7 @@ func (t *WebPath) Load(path string) (func(w http.ResponseWriter, r *http.Request
        } else {
                // 操作next节点
                if t.next != nil {
-                       if f, ok := t.next.Load(path); ok {
-                               return f, true
-                       }
+                       return t.next.Load(path)
                }
                return nil, false
        }
@@ -126,7 +125,7 @@ func (t *WebPath) Load(path string) (func(w http.ResponseWriter, r *http.Request
 func (t *WebPath) Store(path string, f func(w http.ResponseWriter, r *http.Request)) {
        t.Lock()
        defer t.Unlock()
-       if t.path == path || t.f == nil {
+       if t.path == path || (t.path == "" && t.f == nil) {
                // 操作本节点
                t.path = path
                t.f = f
index 8bc2cf78c6226983a05782e5ca301164f894d500..9149d2ef0df763957a869c37397612bd2eee59af 100644 (file)
@@ -59,6 +59,9 @@ func Test_ServerSyncMap(t *testing.T) {
 
                ResStruct{0, "ok", d{"0", []string{"0"}, map[string]int{"0": 1}}}.Write(w)
        })
+       m.Store("/2/", func(w http.ResponseWriter, _ *http.Request) {
+               panic(1)
+       })
 
        time.Sleep(time.Second)
 
@@ -70,6 +73,13 @@ func Test_ServerSyncMap(t *testing.T) {
                if !bytes.Equal(r.Respon, []byte("{\"code\":0,\"message\":\"ok\",\"data\":{\"a\":\"0\",\"b\":[\"0\"],\"c\":{\"0\":1}}}")) {
                        t.Error(string(r.Respon))
                }
+               m.Store("/2/", nil)
+               r.Reqf(reqf.Rval{
+                       Url: "http://127.0.0.1:13000/2/",
+               })
+               if r.Response.StatusCode != 404 {
+                       t.Error(string(r.Respon))
+               }
        }
 }
 
@@ -79,7 +89,7 @@ func Test_ClientBlock(t *testing.T) {
                w.Write([]byte("1"))
        })
        s := NewSyncMap(&http.Server{
-               Addr:         "127.0.0.1:13000",
+               Addr:         "127.0.0.1:13001",
                WriteTimeout: time.Millisecond,
        }, &m)
        defer s.Shutdown()
@@ -126,7 +136,7 @@ func Test_ClientBlock(t *testing.T) {
                        close(c)
                }()
                r.Reqf(reqf.Rval{
-                       Url:         "http://127.0.0.1:13000/to",
+                       Url:         "http://127.0.0.1:13001/to",
                        SaveToPipe:  &pio.IOpipe{R: rc, W: wc},
                        WriteLoopTO: 5000,
                        Async:       true,
@@ -155,7 +165,7 @@ func Test_ServerSyncMapP(t *testing.T) {
        }
 
        o := NewSyncMap(&http.Server{
-               Addr: "127.0.0.1:9090",
+               Addr: "127.0.0.1:13002",
        }, &m)
        defer o.Shutdown()
        m.Store("/1/2", func(w http.ResponseWriter, _ *http.Request) {
@@ -183,56 +193,55 @@ func Test_ServerSyncMapP(t *testing.T) {
        r := reqf.New()
        res := ResStruct{}
        r.Reqf(reqf.Rval{
-               Url: "http://127.0.0.1:9090/conn",
+               Url: "http://127.0.0.1:13002/conn",
        })
        json.Unmarshal(r.Respon, &res)
        if res.Message != "ok" {
                t.Fatal("")
        }
        r.Reqf(reqf.Rval{
-               Url: "http://127.0.0.1:9090/",
+               Url: "http://127.0.0.1:13002/",
        })
        json.Unmarshal(r.Respon, &res)
        if data, ok := res.Data.(map[string]any); !ok || data["path"].(string) != "/" {
                t.Fatal("")
        }
        r.Reqf(reqf.Rval{
-               Url: "http://127.0.0.1:9090/1",
+               Url: "http://127.0.0.1:13002/1",
        })
        json.Unmarshal(r.Respon, &res)
        if data, ok := res.Data.(map[string]any); !ok || data["path"].(string) != "/" {
                t.Fatal("")
        }
        r.Reqf(reqf.Rval{
-               Url: "http://127.0.0.1:9090/1/",
+               Url: "http://127.0.0.1:13002/1/",
        })
        json.Unmarshal(r.Respon, &res)
        if data, ok := res.Data.(map[string]any); !ok || data["path"].(string) != "/1/" {
                t.Fatal("")
        }
        r.Reqf(reqf.Rval{
-               Url: "http://127.0.0.1:9090/2",
+               Url: "http://127.0.0.1:13002/2",
        })
-       json.Unmarshal(r.Respon, &res)
-       if data, ok := res.Data.(map[string]any); !ok || data["path"].(string) != "/" {
+       if r.Response.StatusCode != 404 {
                t.Fatal("")
        }
        r.Reqf(reqf.Rval{
-               Url: "http://127.0.0.1:9090/1/23",
+               Url: "http://127.0.0.1:13002/1/23",
        })
        json.Unmarshal(r.Respon, &res)
        if data, ok := res.Data.(map[string]any); !ok || data["path"].(string) != "/1/" {
                t.Fatal("")
        }
        r.Reqf(reqf.Rval{
-               Url: "http://127.0.0.1:9090/1/2/3",
+               Url: "http://127.0.0.1:13002/1/2/3",
        })
        json.Unmarshal(r.Respon, &res)
        if data, ok := res.Data.(map[string]any); !ok || data["path"].(string) != "/1/" {
                t.Fatal("")
        }
        r.Reqf(reqf.Rval{
-               Url: "http://127.0.0.1:9090/1/2",
+               Url: "http://127.0.0.1:13002/1/2",
        })
        json.Unmarshal(r.Respon, &res)
        if data, ok := res.Data.(map[string]any); !ok || data["path"].(string) != "/1/2" {