func NewMuxServer() *Server { sb := routing.NewSrvSessionBuilder("", "/", "tsrv", 60000, 200) mux := routing.NewSessionMux("", sb) srv := &Server{Mux: mux} srv.S = httptest.NewServer(mux) srv.URL = srv.S.URL return srv }
func NewSrvMux(pre string) *routing.SessionMux { sb := routing.NewSrvSessionBuilder("", "/", "ckey", 600000, 1000) mux := routing.NewSessionMux(pre, sb) mux.HFunc("^/adda", pkga.AddA) mux.HFunc("^/dela", pkga.DelA) mux.HFunc("^/addb", pkgb.AddB) mux.HFunc("^/getb", pkgb.GetB) return mux }
func main() { db, err := sql.Open("mysql", test.TDbCon) if err != nil { panic(err.Error()) } defer db.Close() sb := routing.NewSrvSessionBuilder("", "/", "example", 60*60*1000, 10000) mux := routing.NewSessionMux("/example", sb) mux.HFilterFunc("^.*$", func(hs *routing.HTTPSession) routing.HResult { log.D("filt 001") return routing.HRES_CONTINUE }) //http://localhost:8080/example/ok mux.HFunc("^/ok(\\?.*)?$", func(hs *routing.HTTPSession) routing.HResult { hs.MsgRes("OK") return routing.HRES_RETURN }) //http://localhost:8080/example/data mux.HFunc("^/data(\\?.*)?$", func(hs *routing.HTTPSession) routing.HResult { var tid int64 var name string err := hs.ValidRVal(`//valid the argument tid,R|I,R:0,tid is error; name,R|S,L:0,name is empty; `, &tid, &name) if err != nil { return hs.MsgResE(1, err.Error()) } name = "%" + name + "%" var ts []TSt err = dbutil.DbQueryS(db, &ts, "select * from ttable where tid>? and tname like ?", tid, name) if err != nil { return hs.MsgResE(1, err.Error()) } return hs.MsgRes(ts) }) mux.HFunc("^/mdata(\\?.*)?$", func(hs *routing.HTTPSession) routing.HResult { hs.W.Write([]byte("some data\n")) return routing.HRES_RETURN }) s := http.Server{Addr: ":8080", Handler: mux} err = s.ListenAndServe() if err != nil { fmt.Println(err) } }
func NewServer2(h routing.Handler) *Server { sb := routing.NewSrvSessionBuilder("", "/", "tsrv", 60000, 200) mux := routing.NewSessionMux("", sb) mux.H("^.*$", h) return NewMuxServer2(mux) }
func TestSessionMux(t *testing.T) { sb := NewCookieSessionBuilder("", "/t") mux := routing.NewSessionMux("/t", sb) // mux.HFilterFunc("^/a(\\?.*)?$", func(hs *routing.HTTPSession) routing.HResult { hs.S.Set("testing", "abc") hs.S.Flush() hs.S.Flush() return routing.HRES_CONTINUE }) mux.HFilterFunc("^/c(\\?.*)?$", func(hs *routing.HTTPSession) routing.HResult { return routing.HRES_CONTINUE }) mux.HFilterFunc("^/b/[^\\?/]*(\\?.*)?$", func(hs *routing.HTTPSession) routing.HResult { fmt.Println(hs.R.URL.String()) if hs.S.Val("testing") != "abc" { t.Error("not error testing") } hs.S.Set("testing", nil) if hs.S.Val("testing") != nil { t.Error("not nil") } return routing.HRES_CONTINUE }) mux.HFunc("^/a(\\?.*)?$", func(hs *routing.HTTPSession) routing.HResult { if hs.S.Val("testing") != "abc" { t.Error("not error testing") } return routing.HRES_CONTINUE }) http.Handle("/t/", mux) // esb := NewCookieSessionBuilder("", "/e") emux := routing.NewSessionMux("/e", esb) // emux.HFilterFunc("^/a(\\?.*)?$", func(hs *routing.HTTPSession) routing.HResult { hs.S.Set("testing", "abc") fmt.Println(hs.S.Flush()) return routing.HRES_CONTINUE }) emux.HFilterFunc("^/b(\\?.*)?$", func(hs *routing.HTTPSession) routing.HResult { if hs.S.Val("testing") != nil { t.Error("not nil") } return routing.HRES_CONTINUE }) emux.HFilterFunc("^/c(\\?.*)?$", func(hs *routing.HTTPSession) routing.HResult { hs.S.Set("testing", "abc") hs.S.Flush() return routing.HRES_CONTINUE }) emux.HFilterFunc("^/d/[^\\?/]*(\\?.*)?$", func(hs *routing.HTTPSession) routing.HResult { fmt.Println(hs.R.URL.String()) if hs.S.Val("testing") != nil { t.Error("not nil") } return routing.HRES_CONTINUE }) http.Handle("/e/", emux) // go http.ListenAndServe(":2789", nil) options := cookiejar.Options{ PublicSuffixList: publicsuffix.List, } jar, err := cookiejar.New(&options) if err != nil { t.Error(err.Error()) return } c := http.Client{Jar: jar} c.Get("http://127.0.0.1:2789/t/a") c.Get("http://127.0.0.1:2789/t/b/1") c.Get("http://127.0.0.1:2789/t/c") // esb.Crypto = func(bys []byte) ([]byte, error) { return bys, errors.New("crypto error") } c.Get("http://127.0.0.1:2789/e/a") c.Get("http://127.0.0.1:2789/e/b") esb.Crypto = func(bys []byte) ([]byte, error) { return bys, nil } esb.UnCrypto = func(bys []byte) ([]byte, error) { return bys, errors.New("uncrypto error") } c.Get("http://127.0.0.1:2789/e/c") c.Get("http://127.0.0.1:2789/e/d/1") // esb.Crypto = func(bys []byte) ([]byte, error) { return bys, errors.New("uncrypto error") } esb.UnCrypto = func(bys []byte) ([]byte, error) { return bys, errors.New("uncrypto error") } cs := &CookieSession{ Sb: esb, kvs: map[string]interface{}{}, updated: false, } _, err = cs.Crypto() if err == nil { t.Error("not error") } cs.Set("abc", &Abc{}) _, err = cs.Crypto() if err == nil { t.Error("not error") } cs.UnCrypto("") cs.UnCrypto("测试") cs.UnCrypto("e1f") cs.UnCrypto(hex.EncodeToString([]byte("kkjjjjj"))) esb.UnCrypto = func(bys []byte) ([]byte, error) { return bys, nil } cs.UnCrypto(hex.EncodeToString([]byte("kkjjjjj"))) // cs.kvs["abc"] = 1235 // cs.kvs["dg"] = "kkkkk" // fmt.Println("TestSessionMux end") }