From: qydysky Date: Sat, 2 Dec 2023 00:47:01 +0000 (+0800) Subject: 1 X-Git-Tag: v0.28.20231202005014 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=fb88a2e1d5c4b9ac9c1d41e72026309df8764a20;p=part%2F.git 1 --- diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 14eb277..a9f0cd6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 }} diff --git a/web/Web.go b/web/Web.go index e46495f..a60eb00 100644 --- a/web/Web.go +++ b/web/Web.go @@ -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 diff --git a/web/Web_test.go b/web/Web_test.go index 8bc2cf7..9149d2e 100644 --- a/web/Web_test.go +++ b/web/Web_test.go @@ -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" {