Commit 474613cb by lichenggang

优化初次加载

parent 01b03eea
package code
import (
"bufio"
"bytes"
"io"
"log"
"os"
"strings"
)
func return_byte(file_path string, lines_num int) (string, error) {
lines := int64(lines_num)
file, err := os.Open(file_path)
if err != nil {
log.Println(err)
return "", err
}
fileInfo, _ := file.Stat()
buf := bufio.NewReader(file)
offset := fileInfo.Size() % 8192
data := make([]byte, 8192) // 一行的数据
totalByte := make([][][]byte, 0)
readLines := int64(0)
for i := int64(0); i <= fileInfo.Size()/8192; i++ {
readByte := make([][]byte, 0) // 读取一页的数据
file.Seek(fileInfo.Size()-offset-8192*i, io.SeekStart)
data = make([]byte, 8192)
n, err := buf.Read(data)
if err == io.EOF {
if strings.TrimSpace(string(bytes.Trim(data, "\x00"))) != "" {
readLines++
readByte = append(readByte, data)
totalByte = append(totalByte, readByte)
}
if readLines > lines {
break
}
continue
}
if err != nil {
log.Println("Read file error:", err)
return "", err
}
strs := strings.Split(string(data[:n]), "\n")
if len(strs) == 1 {
b := bytes.Trim([]byte(strs[0]), "\x00")
if len(b) == 0 {
continue
}
}
if (readLines + int64(len(strs))) > lines {
strs = strs[int64(len(strs))-lines+readLines:]
}
for j := 0; j < len(strs); j++ {
readByte = append(readByte, bytes.Trim([]byte(strs[j]+"\n"), "\x00"))
}
readByte[len(readByte)-1] = bytes.TrimSuffix(readByte[len(readByte)-1], []byte("\n"))
totalByte = append(totalByte, readByte)
readLines += int64(len(strs))
if readLines >= lines {
break
}
}
totalByte = ReverseByteArray(totalByte)
file.Close()
return ByteArrayToString(totalByte), nil
}
func ReverseByteArray(s [][][]byte) [][][]byte {
for from, to := 0, len(s)-1; from < to; from, to = from+1, to-1 {
s[from], s[to] = s[to], s[from]
}
return s
}
func ByteArrayToString(buf [][][]byte) string {
str := make([]string, 0)
for _, v := range buf {
for _, vv := range v {
str = append(str, string(vv))
}
}
return strings.Join(str, "")
}
......@@ -15,13 +15,16 @@ import (
"time"
)
func get_files(direct_path string) []string {
func get_files(direct_path string) ([]string, error) {
var li_file []string
files, _ := ioutil.ReadDir(direct_path)
files, err := ioutil.ReadDir(direct_path)
if err != nil {
return nil, err
}
for _, f := range files {
li_file = append(li_file, f.Name())
}
return li_file
return li_file, nil
}
func return_res(w http.ResponseWriter, template_path string, data interface{}) {
w.Header().Set("Content-Type", "text/html")
......@@ -61,10 +64,16 @@ func server(port int, directory string) {
if li_innerip, ok := queryform["innerip"]; ok {
innerip := li_innerip[0]
ip_files := get_files(directory + "/" + innerip)
ip_files, err := get_files(directory + "/" + innerip)
if err != nil {
return_error_res(w, directory+"/"+innerip)
}
if li_server_direc, ok := queryform["server_direc"]; ok {
server_direc := li_server_direc[0]
ip_server_files := get_files(directory + "/" + innerip + "/" + server_direc)
ip_server_files, err := get_files(directory + "/" + innerip + "/" + server_direc)
if err != nil {
return_error_res(w, directory+"/"+innerip+"/"+server_direc)
}
if li_filename, ok := queryform["filename"]; ok {
file := li_filename[0]
str_filepath := innerip + "~~" + server_direc + "~~" + file
......@@ -83,7 +92,10 @@ func server(port int, directory string) {
return_res(w, "templates/innnerip.html", strings.Join(ip_files, "~~"))
}
} else {
ips := get_files(directory)
ips, err := get_files(directory)
if err != nil {
return_error_res(w, directory)
}
return_res(w, "templates/index.html", strings.Join(ips, "~~"))
}
......@@ -94,7 +106,7 @@ func server(port int, directory string) {
go manager.start()
var bt bytes.Buffer
bt.WriteString("127.0.0.1:")
bt.WriteString("0.0.0.0:")
bt.WriteString(strconv.Itoa(port))
http.ListenAndServe(bt.String(), nil)
//log.Fatal()
......@@ -110,28 +122,22 @@ func gen(directory string, file_map map[string]map[client]bool) func(ws *websock
}
filename := queryform["file"][0]
//初始监听时加载全部内容
//初始监听时加载最后500行全部内容
file_path := directory
for _, s := range strings.Split(filename, "~~") {
file_path += "/" + s
}
fileInfo, err := os.Stat(file_path)
_, err = os.Stat(file_path)
if err != nil {
return
}
allcontent_file, err := os.Open(file_path)
offset := fileInfo.Size()
allcontent_msg := make([]byte, offset)
lastcontent_msg, err := return_byte(file_path, 500)
if err != nil {
log.Printf("[seelog] error:%v", err.Error())
}
_, err = allcontent_file.Read(allcontent_msg)
if err != nil {
log.Printf("[seelog] error:%v", err.Error())
log.Fatal(err)
return
}
all_content := filename + "$$" + string(allcontent_msg)
all_content := filename + "$$" + lastcontent_msg
manager.broadcast <- []byte(all_content)
allcontent_file.Close()
// 监控文件
if _, ok := file_map[filename]; ok {
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>alert("File not exist: {{ . }} ,redirect to log page")
window.location.url=window.location.host + "/log"
</script>
</body>
</html>
\ No newline at end of file
......@@ -153,7 +153,7 @@
</div>
</header>
<div id="log"></div>
<div id="log" style="padding: 1px"></div>
</body>
<style>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment