Parcourir la source

connection trace web server

刘河 il y a 5 ans
Parent
commit
18ca5d04cc
2 fichiers modifiés avec 104 ajouts et 0 suppressions
  1. 97 0
      lib/mux/web.go
  2. 7 0
      lib/mux/web_test.go

+ 97 - 0
lib/mux/web.go

@@ -0,0 +1,97 @@
+package mux
+
+import (
+	"fmt"
+	"net/http"
+	"sort"
+	"strconv"
+	"time"
+)
+
+type connLog struct {
+	startTime time.Time
+	isClose   bool
+	logs      []string
+}
+
+var m map[int]*connLog
+
+var copyMap map[int]*connLog
+
+func deepCopyMap() {
+	copyMap = make(map[int]*connLog)
+	for k, v := range m {
+		copyMap[k] = &connLog{
+			startTime: v.startTime,
+			isClose:   v.isClose,
+			logs:      v.logs,
+		}
+	}
+}
+
+//func init() {
+//	m = make(map[int]*connLog)
+//	m[0] = &connLog{
+//		startTime: time.Now(),
+//		isClose:   false,
+//		logs:      []string{"111", "222", "333"},
+//	}
+//	m[1] = &connLog{
+//		startTime: time.Now(),
+//		isClose:   false,
+//		logs:      []string{"111", "222", "333", "444"},
+//	}
+//	m[2] = &connLog{
+//		startTime: time.Now(),
+//		isClose:   true,
+//		logs:      []string{"111", "222", "333", "555"},
+//	}
+//}
+
+type IntSlice []int
+
+func (s IntSlice) Len() int { return len(s) }
+
+func (s IntSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+
+func (s IntSlice) Less(i, j int) bool { return s[i] < s[j] }
+
+func NewLogServer() {
+	http.HandleFunc("/", index)
+	http.HandleFunc("/detail", detail)
+	http.HandleFunc("/stash", stash)
+	fmt.Println(http.ListenAndServe(":8899", nil))
+}
+
+func stash(w http.ResponseWriter, r *http.Request) {
+	deepCopyMap()
+	w.Write([]byte("ok"))
+}
+
+func index(w http.ResponseWriter, r *http.Request) {
+	var keys []int
+	for k := range copyMap {
+		keys = append(keys, k)
+	}
+	sort.Sort(IntSlice(keys))
+	var s string
+	for v := range keys {
+		connL := copyMap[v]
+		s += "<a href='/detail?id=" + strconv.Itoa(v) + "'>" + strconv.Itoa(v) + "</a>----------"
+		s += strconv.Itoa(int(time.Now().Unix()-connL.startTime.Unix())) + "s----------"
+		s += strconv.FormatBool(connL.isClose)
+		s += "<br>"
+	}
+	w.Write([]byte(s))
+}
+
+func detail(w http.ResponseWriter, r *http.Request) {
+	id := r.FormValue("id")
+	i, _ := strconv.Atoi(id)
+	v, _ := copyMap[i]
+	var s string
+	for _, vv := range v.logs {
+		s += "<p>" + vv + "</p>"
+	}
+	w.Write([]byte(s))
+}

+ 7 - 0
lib/mux/web_test.go

@@ -0,0 +1,7 @@
+package mux
+
+import "testing"
+
+func TestWeb(t *testing.T) {
+	NewLogServer()
+}