Skip to content

Commit

Permalink
优化节点列表页面
Browse files Browse the repository at this point in the history
  • Loading branch information
侯锐 committed Aug 22, 2019
1 parent 47f9e3b commit 1f82693
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
48 changes: 48 additions & 0 deletions common/constants.go

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ func root(w http.ResponseWriter, r *http.Request) {

// 显示节点信息
func nodes(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
if r.Method != http.MethodGet {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
sendResponse(w, "Only allow method [GET].")
return
}

sendResponse(w, " NodeId Host\n")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
table := ""

nodes := node.GetNodes()
sort.Sort(nodes) // 对节点列表进行排序
for _, n := range nodes {
Expand All @@ -71,8 +73,9 @@ func nodes(w http.ResponseWriter, r *http.Request) {
if n.NodeId == common.LocalNodeId {
me = "▴"
}
sendResponse(w, fmt.Sprintf("%s%s %10s %15s:%-5d\n", star, me, n.NodeId, n.Ip, n.HTTPPort))
table += fmt.Sprintf(common.NodePageTableTemplate, star, me, n.NodeId, n.Ip, n.HTTPPort)
}
sendResponse(w, fmt.Sprintf(common.NodesPage, strings.Replace(table, " ", " ", -1)))
}

// 读写数据
Expand All @@ -98,7 +101,7 @@ func handlerEntries(w http.ResponseWriter, r *http.Request) {
// key不为空,返回指定数据
value, ok := common.GetEntryByKey(key)
if ok {
sendResponse(w, fmt.Sprintf(`{"%s": "%s"}`, key, value))
sendResponse(w, fmt.Sprintf(`Result for key [%s]: {"%s": "%s"}`, key, key, value))
} else {
w.WriteHeader(http.StatusNotFound) // 没找到相应的key
sendResponse(w, fmt.Sprintf("can't found key [%s]", key))
Expand Down Expand Up @@ -262,7 +265,11 @@ func getFromAndSize(fromStr, sizeStr string, entriesLength int) (from, size int)
from = 0
}

if size > entriesLength || size < 0 {
if size > entriesLength {
size = entriesLength
}

if size < 0 {
size = 10
}

Expand Down
5 changes: 4 additions & 1 deletion network/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ func handleConnection(c net.Conn) {
// 读取一个字节
readByte := func() (byte, bool) {
buf, success := readBytes(1)
return buf[0], success
if !success {
return 0, false
}
return buf[0], true
}

// 读取一个字符串
Expand Down

0 comments on commit 1f82693

Please sign in to comment.