浏览代码

修改xweb支持wxhttp

renzheng 8 年之前
父节点
当前提交
56989bba4d

+ 57 - 4
common/src/github.com/go-xweb/xweb/server.go

@@ -57,11 +57,11 @@ func NewServer(args ...string) *Server {
 		ServerNumber++
 	}
 	s := &Server{
-		Config:  Config,
-		Env:     map[string]interface{}{},
-		Apps:    map[string]*App{},
+		Config:       Config,
+		Env:          map[string]interface{}{},
+		Apps:         map[string]*App{},
 		AppsNamePath: map[string]string{},
-		Name:    name,
+		Name:         name,
 	}
 	Servers[s.Name] = s
 
@@ -171,6 +171,59 @@ func (s *Server) Process(w http.ResponseWriter, req *http.Request) {
 }
 
 // Run starts the web application and serves HTTP requests for s
+func (s *Server) RunBase(addr string, mux *http.ServeMux) {
+	addrs := strings.Split(addr, ":")
+	s.Config.Addr = addrs[0]
+	s.Config.Port, _ = strconv.Atoi(addrs[1])
+
+	s.initServer()
+
+	//mux := http.NewServeMux()
+	if s.Config.Profiler {
+		mux.Handle("/debug/pprof", http.HandlerFunc(pprof.Index))
+		mux.Handle("/debug/pprof/heap", pprof.Handler("heap"))
+		mux.Handle("/debug/pprof/block", pprof.Handler("block"))
+		mux.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
+		mux.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
+
+		mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
+		mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
+		mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
+
+		mux.Handle("/debug/pprof/startcpuprof", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
+			StartCPUProfile()
+		}))
+		mux.Handle("/debug/pprof/stopcpuprof", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
+			StopCPUProfile()
+		}))
+		mux.Handle("/debug/pprof/memprof", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
+			runtime.GC()
+			runtimePprof.WriteHeapProfile(rw)
+		}))
+		mux.Handle("/debug/pprof/gc", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
+			PrintGCSummary(rw)
+		}))
+
+	}
+
+	if c, err := XHook.Call("MuxHandle", mux); err == nil {
+		if ret := XHook.Value(c, 0); ret != nil {
+			mux = ret.(*http.ServeMux)
+		}
+	}
+	mux.Handle("/", s)
+
+	s.Logger.Infof("http server is listening %s", addr)
+
+	l, err := net.Listen("tcp", addr)
+	if err != nil {
+		s.Logger.Error("ListenAndServe:", err)
+	}
+	s.l = l
+	err = http.Serve(s.l, mux)
+	s.l.Close()
+}
+
 func (s *Server) Run(addr string) {
 	addrs := strings.Split(addr, ":")
 	s.Config.Addr = addrs[0]

+ 3 - 0
common/src/github.com/go-xweb/xweb/xweb.go

@@ -84,6 +84,9 @@ func Process(c http.ResponseWriter, req *http.Request) {
 func Run(addr string) {
 	mainServer.Run(addr)
 }
+func RunBase(addr string, mux *http.ServeMux) {
+	mainServer.RunBase(addr, mux)
+}
 
 func SimpleTLSConfig(certFile, keyFile string) (*tls.Config, error) {
 	config := &tls.Config{}

+ 1 - 1
common/src/qfw/util/encrypt_test.go

@@ -71,7 +71,7 @@ func TestEncrypt(t *testing.T) {
 	a2 := regexp.MustCompile("<[^>]+?>|[\\s\u3000\u2003\u00a0]")
 	a1 := regexp.MustCompile("[^0-9A-Za-z\u4e00-\u9fa5]+")
 	log.Println(a1.ReplaceAllString(a2.ReplaceAllString(Test2, ""), "") == a1.ReplaceAllString(a2.ReplaceAllString(Test1, ""), ""))
-	log.Println("", DecodeArticleId("QVdBX1EWBgMHBBVfR1xUEgMFBFBGWkhW"))
+	log.Println("", DecodeArticleId("QVdEV1RFBAMHBBVfR1xUEgMFCFAQXUBZ"))
 	se := &SimpleEncrypt{Key: "topnet2015topnet2015"}
 	log.Println(GetSubDay(1453683600 + 8*3600))
 	log.Println(se.EncodeString("582bf5de9386b5d8f9805adf"))

+ 24 - 0
common/src/qfw/util/mongodb/mongodbSim.go

@@ -124,6 +124,30 @@ func (m *MongodbSim) Update(c string, query interface{}, set interface{}, upsert
 	return b
 }
 
+func (m *MongodbSim) UpdateById(c string, id interface{}, set interface{}) bool {
+	defer util.Catch()
+	sess := m.GetMgoConn()
+	b := false
+	if sess != nil {
+		defer m.DestoryMongoConn(sess)
+		coll := sess.DB(m.DbName).C(c)
+		var q interface{}
+		if sid, ok := id.(string); ok {
+			q = M{"_id": util.StringTOBsonId(sid)}
+		} else {
+			q = M{"_id": id}
+		}
+		err := coll.Update(q, ObjToM(set))
+		if nil != err {
+			log.Println("UpdateByIdError", err)
+			b = false
+		} else {
+			b = true
+		}
+	}
+	return b
+}
+
 //批量更新
 func (m *MongodbSim) UpdateBulk(c string, doc ...[]map[string]interface{}) bool {
 	defer util.Catch()