func (self *GLWeb) Run(hp string) { log.Infoln("GLWeb Running") log.SetLevel(log.DebugLevel) self.glsroute = make(map[string]*route) self.gls = make(map[string]*glf) self.glps = make(map[string]string) self.loadgls() self.fnotify() log.Infoln("GLWeb ListenAndServe...") go func() { if err := http.ListenAndServe(hp, self); err != nil { log.Errorln("GLWeb ListenAndServe Error.", err) } }() signalChan := make(chan os.Signal) //创建一个信号量的chan,缓存为1,(0,1)意义不大 signal.Notify(signalChan, os.Interrupt, os.Kill) //让进城收集信号量。 <-signalChan log.Infoln("GLWeb Stop.") self.fmonitor.End() if db != nil { db.Close() } for _, g := range self.gls { g.close() } log.Infoln("GLWeb Close") }
func (this *DB) Open(db_user, db_pwd, db_host, db_database string) { // db_host := this.yoghourt.cm.sc.Item("db_host").Value() // db_user := this.yoghourt.cm.sc.Item("db_user").Value() // db_pwd := this.yoghourt.cm.sc.Item("db_pwd").Value() // db_database := this.yoghourt.cm.sc.Item("db_database").Value() if db_host != "" && db_user != "" && db_pwd != "" && db_database != "" { var err error this.db, err = sql.Open("mysql", db_user+":"+db_pwd+"@tcp("+db_host+")/"+db_database+"?charset=utf8") if err != nil { log.Errorln("DB Open", err) } } else { log.Errorln("DB Open", "no database info") } }
func (this *DB) Close() { if this.db != nil { err := this.db.Close() if err != nil { log.Errorln("DB Close", err) } } }
func (self *GLWeb) loadgls() { gls, err := WalkDir(self.glspath, ".gl") log.Debugln("gls:", gls) if err != nil { log.Errorln("GLWeb loadgls WalkDir Error.", err) return } for _, g := range gls { self.loadgl(g) } }
func apiErrorln(L *lua.LState) int { n := L.GetTop() as := make([]interface{}, n) for i := 1; i <= n; i++ { any := L.CheckAny(i) as[i-1] = any } log.Errorln(as...) return 0 }
func (self *GLWeb) fnotify() { rootfile, err := filemonitor.NewFileEntry(self.glspath) log.Debugln("rootfile", rootfile.Path()) if err != nil { log.Errorln("file Error:", err) return } fileFilter := filemonitor.NewFileFilter() fileFilter.AddFilter(".gl") self.fobserver = filemonitor.NewFileMonitorObserverByFileEntryAndFileFileter( rootfile, fileFilter, ) self.fobserver.AddListener(self) self.fmonitor = filemonitor.NewFileMonitorByDt(self.fobserver, time.Second) self.fmonitor.Start() log.Infoln("GLWeb FileMonitor Start...") }
func a() { go func() { i := 1 for { glog.Errorln(i) glog.Debugln(i) glog.NewField(). Set("k", "v"). Set("k1", "v1"). Warnln(i) glog.NewTagField("tag"). Set("k", "v").Infoln(i) i++ time.Sleep(time.Second) } }() time.Sleep(time.Second * 2) go glog.Go(a1, 1, 0) time.Sleep(time.Second * 5) }
func (self *GLWeb) loadgl(p string) { fbody, err := ioutil.ReadFile(p) if err != nil { log.Errorln("GLWeb loadgl ReadFile "+p+" Error.", err) return } L := lua.NewState() defer func() { L.SetTop(0) L.Close() }() json.Preload(L) gllog.Preload(L) glstr.Preload(L) if err := L.DoString(string(fbody)); err != nil { log.Errorln("GLWeb loadgl DoFile Error.", err) return } ltask := L.GetGlobal("tasks") if ltask.Type() == lua.LTNil { log.Errorln("GLWeb loadgl Get Tasks Error.", p, " not tasks table.") return } var tasks *Tasks if err := gluamapper.Map(L.GetGlobal("tasks").(*lua.LTable), &tasks); err != nil { log.Errorln("GLWeb loadgl Get Tasks Error.", err) return } log.Infoln("GLWeb gls load ", p, tasks.Name) g := newglf(p, string(fbody)) if tasks.Name == "" { log.Errorln("GLWEB gls load Error tasks name is nil") return } if _, ok := self.gls[tasks.Name]; ok { log.Errorln("GLWeb gls load Error tasks name ", tasks.Name, " exist") return } g.name = tasks.Name for _, t := range tasks.Task { // log.WithFields(log.Fields{ // "name": t.Name, // "method": t.Method, // "action": t.Action, // "version": t.Version, // "function": t.Fn, // }).Infoln("load task") if t.Name == "nil" || t.Method == "nil" || t.Action == "nil" || t.Version == "nil" || t.Fn == "nil" { log.Errorln("GLWeb gls load Error name method action version fn is nil") continue } for n, r := range self.glsroute { if n == t.Name { log.Errorln("GLWeb gls load Error task name ", t.Name, " exist") continue } if r.pattern == t.Action { log.Errorln("GLWeb gls load Error action ", t.Action, " exist") continue } } self.glsroute[t.Name] = newRoute(g, t.Method, t.Action) g.addtask(t) } self.gls[p] = g self.glps[tasks.Name] = p }