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