ghttp.Server启动失败时增加报错提醒,完善glog backtrace信息,增加g.Map类型

This commit is contained in:
John 2018-04-19 13:41:06 +08:00
parent 7bdb528f5d
commit b3c672b08b
4 changed files with 13 additions and 4 deletions

3
g/g.go
View File

@ -7,4 +7,5 @@
package g
// 常用map数据结构
type Map map[string]interface{}

View File

@ -12,6 +12,7 @@ import (
"strings"
"reflect"
"net/http"
"gitee.com/johng/gf/g/os/glog"
"gitee.com/johng/gf/g/os/gcache"
"gitee.com/johng/gf/g/container/gmap"
"gitee.com/johng/gf/g/container/gtype"
@ -133,6 +134,7 @@ func (s *Server) Run() error {
s.startCloseQueueLoop()
// 执行端口监听
if err := s.server.ListenAndServe(); err != nil {
glog.Error(err)
return err
}
s.status = 1

View File

@ -21,6 +21,7 @@ import (
"gitee.com/johng/gf/g/os/gfilepool"
"runtime"
"strconv"
"gitee.com/johng/gf/g/util/gregx"
)
type Logger struct {
@ -247,6 +248,7 @@ func (l *Logger) checkLogIO() {
fpath := path + string(filepath.Separator) + fname
if fp, err := gfilepool.OpenWithPool(fpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 600); err == nil {
l.SetLogIO(fp.File())
fp.Close()
} else {
fmt.Fprintln(os.Stderr, err)
}
@ -293,7 +295,10 @@ func (l *Logger) backtrace() string {
backtrace := "Trace:\n"
for i := 1; i < 10000; i++ {
if _, cfile, cline, ok := runtime.Caller(i + 3); ok {
backtrace += strconv.Itoa(i) + ". " + cfile + ":" + strconv.Itoa(cline) + "\n"
// 不打印出go源码路径
if !gregx.IsMatchString("^" + runtime.GOROOT(), cfile) {
backtrace += strconv.Itoa(i) + ". " + cfile + ":" + strconv.Itoa(cline) + "\n"
}
} else {
break
}

View File

@ -3,18 +3,19 @@ package main
import (
"gitee.com/johng/gf/g/net/ghttp"
"gitee.com/johng/gf/g/frame/gins"
"gitee.com/johng/gf/g"
)
func main() {
s := ghttp.GetServer()
s.BindHandler("/template2", func(r *ghttp.Request){
tplcontent := `id:{{.id}}, name:{{.name}}`
content, _ := gins.View().ParseContent(tplcontent, map[string]interface{}{
content, _ := gins.View().ParseContent(tplcontent, g.Map{
"id" : 123,
"name" : "john",
})
r.Response.Write(content)
})
s.SetPort(8199)
//s.SetPort(80)
s.Run()
}