diff --git a/consts.go b/consts.go index d63267b..717195f 100644 --- a/consts.go +++ b/consts.go @@ -3,7 +3,7 @@ package dotweb // Global define const ( // Version current version - Version = "1.7.6" + Version = "1.7.7" ) // Log define diff --git a/core/htmlx.go b/core/htmlx.go new file mode 100644 index 0000000..5f759f8 --- /dev/null +++ b/core/htmlx.go @@ -0,0 +1,21 @@ +package core + +import "strings" + +func CreateTableHtml(title, header, body string) string { + template := `
+ + + + + + + {{header}} + + {{body}} +
{{title}}
` + html := strings.Replace(template, "{{title}}", title, -1) + html = strings.Replace(html, "{{header}}", header, -1) + html = strings.Replace(html, "{{body}}", body, -1) + return html +} diff --git a/core/state.go b/core/state.go index cd57df7..5734f54 100644 --- a/core/state.go +++ b/core/state.go @@ -1,6 +1,7 @@ package core import ( + "fmt" "net/http" "strconv" "strings" @@ -19,6 +20,118 @@ const ( defaultCheckTimeMinutes = 10 ) +var TableHtml = ` + + + + + + + + + + Dotweb + + + + +
+{{tableBody}} +
+ + +` + // NewServerStateInfo return ServerStateInfo which is init func NewServerStateInfo() *ServerStateInfo { state := &ServerStateInfo{ @@ -99,8 +212,8 @@ type ServerStateInfo struct { infoPool *pool } -// ShowHtmlData show server state data html-string format -func (state *ServerStateInfo) ShowHtmlData(version, globalUniqueId string) string { +// ShowHtmlDataRaw show server state data html-string format +func (state *ServerStateInfo) ShowHtmlDataRaw(version, globalUniqueId string) string { data := "
" data += "GlobalUniqueId : " + globalUniqueId data += "
" @@ -145,6 +258,64 @@ func (state *ServerStateInfo) ShowHtmlData(version, globalUniqueId string) strin return data } +// ShowHtmlData show server state data html-table format +func (state *ServerStateInfo) ShowHtmlTableData(version, globalUniqueId string) string { + data := "" + "GlobalUniqueId" + "" + globalUniqueId + "" + data += "" + "HostInfo" + "" + sysx.GetHostName() + "" + data += "" + "CurrentTime" + "" + time.Now().Format("2006-01-02 15:04:05") + "" + data += "" + "ServerVersion" + "" + version + "" + data += "" + "ServerStartTime" + "" + state.ServerStartTime.Format(dateTimeLayout) + "" + data += "" + "TotalRequestCount" + "" + strconv.FormatUint(state.TotalRequestCount, 10) + "" + data += "" + "CurrentRequestCount" + "" + strconv.FormatUint(state.CurrentRequestCount, 10) + "" + data += "" + "TotalErrorCount" + "" + strconv.FormatUint(state.TotalErrorCount, 10) + "" + state.IntervalErrorData.RLock() + data += "" + "IntervalErrorData" + "" + jsonutil.GetJsonString(state.IntervalErrorData.GetCurrentMap()) + "" + state.IntervalErrorData.RUnlock() + state.DetailErrorPageData.RLock() + data += "" + "DetailErrorPageData" + "" + jsonutil.GetJsonString(state.DetailErrorPageData.GetCurrentMap()) + "" + state.DetailErrorPageData.RUnlock() + state.DetailErrorData.RLock() + data += "" + "DetailErrorData" + "" + jsonutil.GetJsonString(state.DetailErrorData.GetCurrentMap()) + "" + state.DetailErrorData.RUnlock() + state.DetailHTTPCodeData.RLock() + data += "" + "DetailHttpCodeData" + "" + jsonutil.GetJsonString(state.DetailHTTPCodeData.GetCurrentMap()) + "" + state.DetailHTTPCodeData.RUnlock() + header := ` + Index + Value + ` + data = CreateTableHtml("Core State", header, data) + + //show IntervalRequestData + intervalRequestData := "" + state.IntervalRequestData.RLock() + for k, v := range state.IntervalRequestData.GetCurrentMap() { + intervalRequestData += "" + k + "" + fmt.Sprint(v) + "" + } + state.IntervalRequestData.RUnlock() + header = ` + Time + Value + ` + data += CreateTableHtml("IntervalRequestData", header, intervalRequestData) + + //show DetailRequestURLData + detailRequestURLData := "" + state.DetailRequestURLData.RLock() + for k, v := range state.DetailRequestURLData.GetCurrentMap() { + detailRequestURLData += "" + k + "" + fmt.Sprint(v) + "" + } + state.DetailRequestURLData.RUnlock() + header = ` + Url + Value + ` + data += CreateTableHtml("DetailRequestURLData", header, detailRequestURLData) + html := strings.Replace(TableHtml, "{{tableBody}}", data, -1) + + return html +} + // QueryIntervalRequestData query request count by query time func (state *ServerStateInfo) QueryIntervalRequestData(queryKey string) uint64 { return state.IntervalRequestData.GetUInt64(queryKey) diff --git a/dotweb_sysgroup.go b/dotweb_sysgroup.go index 66177c4..d7c1241 100644 --- a/dotweb_sysgroup.go +++ b/dotweb_sysgroup.go @@ -1,8 +1,9 @@ package dotweb import ( + "fmt" + "github.com/devfeel/dotweb/core" jsonutil "github.com/devfeel/dotweb/framework/json" - "github.com/devfeel/dotweb/framework/stringx" "runtime" "runtime/debug" "runtime/pprof" @@ -50,7 +51,7 @@ func showIntervalData(ctx Context) error { // snow server status func showServerState(ctx Context) error { - return ctx.WriteHtml(ctx.HttpServer().StateInfo().ShowHtmlData(Version, ctx.HttpServer().DotApp.GlobalUniqueID())) + return ctx.WriteHtml(ctx.HttpServer().StateInfo().ShowHtmlTableData(Version, ctx.HttpServer().DotApp.GlobalUniqueID())) } // query server information @@ -68,11 +69,18 @@ func showQuery(ctx Context) error { func showRouters(ctx Context) error { - result := "" + data := "" + routerCount := len(ctx.HttpServer().router.GetAllRouterExpress()) for k, _ := range ctx.HttpServer().router.GetAllRouterExpress() { method := strings.Split(k, routerExpressSplit)[0] router := strings.Split(k, routerExpressSplit)[1] - result += stringx.CompletionRight(method, " ", 12) + router + "\r\n" + data += "" + method + "" + router + "" } - return ctx.WriteString(result) + header := ` + Method + Router + ` + html := strings.Replace(core.TableHtml, "{{tableBody}}", core.CreateTableHtml("Routers:"+fmt.Sprint(routerCount), header, data), -1) + + return ctx.WriteHtml(html) } diff --git a/version.MD b/version.MD index 4f6b0da..8658d40 100644 --- a/version.MD +++ b/version.MD @@ -1,6 +1,14 @@ ## dotweb版本记录: +#### Version 1.7.7 +* Opt: 优化系统路由dotweb/state、dotweb/routers展现方式,以方便阅读的表格形式输出 +* Feature: 新增core.TableHtml\core.CreateTableHtml()用于生成相关Html代码 +* About: + - 可访问dotweb/state查看当前实例运行时信息 + - 可访问dotweb/routers查看当前实例注册的所有路由信息 +* 2019-11-12 18:00 at ShangHai + #### Version 1.7.6 * Fix: 修复在调用SetMethodNotAllowedHandle时修改StatusCode无效问题 * Opt: 将路由阶段设置405代码逻辑移除,相关逻辑在DefaultMethodNotAllowedHandler实现