Skip to content

Commit 68e92aa

Browse files
authored
Merge pull request #225 from devfeel/develop
optimize html create code
2 parents 0f05666 + f8eca77 commit 68e92aa

4 files changed

Lines changed: 148 additions & 119 deletions

File tree

core/htmlx.go

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,120 @@ package core
22

33
import "strings"
44

5-
func CreateTableHtml(title, header, body string) string {
5+
var tableHtml = `<html>
6+
<html><head>
7+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;">
10+
11+
<meta name="Generator" content="EditPlus">
12+
<meta name="Author" content="">
13+
<meta name="Keywords" content="">
14+
<meta name="Description" content="">
15+
<title>Dotweb</title>
16+
<style>
17+
.overtable {
18+
width: 100%;
19+
overflow: hidden;
20+
overflow-x: auto;
21+
}
22+
body {
23+
max-width: 780px;
24+
margin:0 auto;
25+
font-family: 'trebuchet MS', 'Lucida sans', Arial;
26+
font-size: 1rem;
27+
color: #444;
28+
}
29+
table {
30+
font-family: 'trebuchet MS', 'Lucida sans', Arial;
31+
*border-collapse: collapse;
32+
/* IE7 and lower */
33+
border-spacing: 0;
34+
width: 100%;
35+
border-collapse: collapse;
36+
overflow-x: auto
37+
}
38+
caption {
39+
font-family: 'Microsoft Yahei', 'trebuchet MS', 'Lucida sans', Arial;
40+
text-align: left;
41+
padding: .5rem;
42+
font-weight: bold;
43+
font-size: 110%;
44+
color: #666;
45+
}
46+
tr {
47+
border-top: 1px solid #dfe2e5
48+
}
49+
tr:nth-child(2n) {
50+
background-color: #f6f8fa
51+
}
52+
td,
53+
th {
54+
border: 1px solid #dfe2e5;
55+
padding: .6em 1em;
56+
}
57+
.bordered tr:hover {
58+
background: #fbf8e9;
59+
}
60+
.bordered td,
61+
.bordered th {
62+
border: 1px solid #ccc;
63+
padding: 10px;
64+
text-align: left;
65+
}
66+
</style>
67+
<script>
68+
69+
(function(doc, win) {
70+
window.MPIXEL_RATIO = (function () {
71+
var Mctx = document.createElement("canvas").getContext("2d"),
72+
Mdpr = window.devicePixelRatio || 1,
73+
Mbsr = Mctx.webkitBackingStorePixelRatio ||
74+
Mctx.mozBackingStorePixelRatio ||
75+
Mctx.msBackingStorePixelRatio ||
76+
Mctx.oBackingStorePixelRatio ||
77+
Mctx.backingStorePixelRatio || 1;
78+
79+
return Mdpr/Mbsr;
80+
})();
81+
82+
function addEventListeners(ele,type,callback){
83+
84+
try{ // Chrome、FireFox、Opera、Safari、IE9.0及其以上版本
85+
ele.addEventListener(type,callback,false);
86+
}catch(e){
87+
try{ // IE8.0及其以下版本
88+
ele.attachEvent('on' + type,callback);
89+
}catch(e){ // 早期浏览器
90+
ele['on' + type] = callback;
91+
}
92+
}
93+
}
94+
95+
var docEl = doc.documentElement,
96+
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
97+
window.recalc = function() {
98+
var clientWidth = docEl.clientWidth < 768 ? docEl.clientWidth : 768;
99+
if (!clientWidth) return;
100+
docEl.style.fontSize = 10 * (clientWidth / 320) * window.MPIXEL_RATIO + 'px';
101+
};
102+
window.recalc();
103+
104+
addEventListeners(win, resizeEvt, recalc);
105+
})(document, window);
106+
107+
</script>
108+
</head>
109+
<body>
110+
<div class="overtable">
111+
{{tableBody}}
112+
</div>
113+
</body>
114+
</html>
115+
`
116+
117+
// CreateTablePart create a table part html by replacing flags
118+
func CreateTablePart(title, header, body string) string {
6119
template := `<br><table class="bordered">
7120
<colgroup>
8121
<col width="40%">
@@ -19,3 +132,27 @@ func CreateTableHtml(title, header, body string) string {
19132
html = strings.Replace(html, "{{body}}", body, -1)
20133
return html
21134
}
135+
136+
// CreateTableHtml create a complete page html by replacing {{tableBody}} and table part html
137+
func CreateTableHtml(title, header, body string) string {
138+
template := `<br><table class="bordered">
139+
<colgroup>
140+
<col width="40%">
141+
<col width="60%">
142+
</colgroup>
143+
<caption>{{title}}</caption>
144+
<thead>
145+
{{header}}
146+
</thead>
147+
{{body}}
148+
</table>`
149+
data := strings.Replace(template, "{{title}}", title, -1)
150+
data = strings.Replace(data, "{{header}}", header, -1)
151+
data = strings.Replace(data, "{{body}}", body, -1)
152+
return CreateHtml(data)
153+
}
154+
155+
// CreateHtml create a complete page html by replacing {{tableBody}}
156+
func CreateHtml(tableBody string) string {
157+
return strings.Replace(tableHtml, "{{tableBody}}", tableBody, -1)
158+
}

core/state.go

Lines changed: 4 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -20,118 +20,6 @@ const (
2020
defaultCheckTimeMinutes = 10
2121
)
2222

23-
var TableHtml = `<html>
24-
<html><head>
25-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
26-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
27-
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;">
28-
29-
<meta name="Generator" content="EditPlus">
30-
<meta name="Author" content="">
31-
<meta name="Keywords" content="">
32-
<meta name="Description" content="">
33-
<title>Dotweb</title>
34-
<style>
35-
.overtable {
36-
width: 100%;
37-
overflow: hidden;
38-
overflow-x: auto;
39-
}
40-
body {
41-
max-width: 780px;
42-
margin:0 auto;
43-
font-family: 'trebuchet MS', 'Lucida sans', Arial;
44-
font-size: 1rem;
45-
color: #444;
46-
}
47-
table {
48-
font-family: 'trebuchet MS', 'Lucida sans', Arial;
49-
*border-collapse: collapse;
50-
/* IE7 and lower */
51-
border-spacing: 0;
52-
width: 100%;
53-
border-collapse: collapse;
54-
overflow-x: auto
55-
}
56-
caption {
57-
font-family: 'Microsoft Yahei', 'trebuchet MS', 'Lucida sans', Arial;
58-
text-align: left;
59-
padding: .5rem;
60-
font-weight: bold;
61-
font-size: 110%;
62-
color: #666;
63-
}
64-
tr {
65-
border-top: 1px solid #dfe2e5
66-
}
67-
tr:nth-child(2n) {
68-
background-color: #f6f8fa
69-
}
70-
td,
71-
th {
72-
border: 1px solid #dfe2e5;
73-
padding: .6em 1em;
74-
}
75-
.bordered tr:hover {
76-
background: #fbf8e9;
77-
}
78-
.bordered td,
79-
.bordered th {
80-
border: 1px solid #ccc;
81-
padding: 10px;
82-
text-align: left;
83-
}
84-
</style>
85-
<script>
86-
87-
(function(doc, win) {
88-
window.MPIXEL_RATIO = (function () {
89-
var Mctx = document.createElement("canvas").getContext("2d"),
90-
Mdpr = window.devicePixelRatio || 1,
91-
Mbsr = Mctx.webkitBackingStorePixelRatio ||
92-
Mctx.mozBackingStorePixelRatio ||
93-
Mctx.msBackingStorePixelRatio ||
94-
Mctx.oBackingStorePixelRatio ||
95-
Mctx.backingStorePixelRatio || 1;
96-
97-
return Mdpr/Mbsr;
98-
})();
99-
100-
function addEventListeners(ele,type,callback){
101-
102-
try{ // Chrome、FireFox、Opera、Safari、IE9.0及其以上版本
103-
ele.addEventListener(type,callback,false);
104-
}catch(e){
105-
try{ // IE8.0及其以下版本
106-
ele.attachEvent('on' + type,callback);
107-
}catch(e){ // 早期浏览器
108-
ele['on' + type] = callback;
109-
}
110-
}
111-
}
112-
113-
var docEl = doc.documentElement,
114-
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
115-
window.recalc = function() {
116-
var clientWidth = docEl.clientWidth < 768 ? docEl.clientWidth : 768;
117-
if (!clientWidth) return;
118-
docEl.style.fontSize = 10 * (clientWidth / 320) * window.MPIXEL_RATIO + 'px';
119-
};
120-
window.recalc();
121-
122-
addEventListeners(win, resizeEvt, recalc);
123-
})(document, window);
124-
125-
</script>
126-
</head>
127-
<body>
128-
<div class="overtable">
129-
{{tableBody}}
130-
</div>
131-
</body>
132-
</html>
133-
`
134-
13523
// NewServerStateInfo return ServerStateInfo which is init
13624
func NewServerStateInfo() *ServerStateInfo {
13725
state := &ServerStateInfo{
@@ -284,7 +172,7 @@ func (state *ServerStateInfo) ShowHtmlTableData(version, globalUniqueId string)
284172
<th>Index</th>
285173
<th>Value</th>
286174
</tr>`
287-
data = CreateTableHtml("Core State", header, data)
175+
data = CreateTablePart("Core State", header, data)
288176

289177
//show IntervalRequestData
290178
intervalRequestData := ""
@@ -297,7 +185,7 @@ func (state *ServerStateInfo) ShowHtmlTableData(version, globalUniqueId string)
297185
<th>Time</th>
298186
<th>Value</th>
299187
</tr>`
300-
data += CreateTableHtml("IntervalRequestData", header, intervalRequestData)
188+
data += CreateTablePart("IntervalRequestData", header, intervalRequestData)
301189

302190
//show DetailRequestURLData
303191
detailRequestURLData := ""
@@ -310,9 +198,8 @@ func (state *ServerStateInfo) ShowHtmlTableData(version, globalUniqueId string)
310198
<th>Url</th>
311199
<th>Value</th>
312200
</tr>`
313-
data += CreateTableHtml("DetailRequestURLData", header, detailRequestURLData)
314-
html := strings.Replace(TableHtml, "{{tableBody}}", data, -1)
315-
201+
data += CreateTablePart("DetailRequestURLData", header, detailRequestURLData)
202+
html := CreateHtml(data)
316203
return html
317204
}
318205

dotweb_sysgroup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func showRouters(ctx Context) error {
8080
<th>Method</th>
8181
<th>Router</th>
8282
</tr>`
83-
html := strings.Replace(core.TableHtml, "{{tableBody}}", core.CreateTableHtml("Routers:"+fmt.Sprint(routerCount), header, data), -1)
83+
html := core.CreateTableHtml("Routers:"+fmt.Sprint(routerCount), header, data)
8484

8585
return ctx.WriteHtml(html)
8686
}

version.MD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## dotweb版本记录:
22

3+
#### Version 1.7.9
4+
* Opt: optimize html create code
5+
* Opt: optimize core.CreateTablePart\core.CreateTableHtml\core.CreateHtml
6+
* 2019-11-20 07:00 at ShangHai
7+
38
#### Version 1.7.8
49
* Opt: optimize tree.go
510
* Opt: Fix some panic information when a 'catch-all' wildcard conflict occurs.

0 commit comments

Comments
 (0)