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)
}
})
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
} 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
}
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
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)
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))
+ }
}
}
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()
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,
}
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) {
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" {