Commit e907d7c6 by lichenggang

检测文件是否存在

parent 474613cb
......@@ -37,12 +37,15 @@ func return_res(w http.ResponseWriter, template_path string, data interface{}) {
tmpl.Execute(w, data)
}
func return_error_res(w http.ResponseWriter, str_file string) {
var ct bytes.Buffer
ct.WriteString("<script>alert(\"File not exist: ")
ct.WriteString(str_file)
ct.WriteString("\")</script>")
w.Write([]byte(ct.String()))
return
template_path := "templates/file_not_exist.html"
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(200)
tmpl, err := template.ParseFiles(template_path)
if err != nil {
log.Println("err:", err)
return
}
tmpl.Execute(w, str_file)
}
// 开启 httpServer
......@@ -55,8 +58,7 @@ func server(port int, directory string) {
}()
// 返回页面
//http.HandleFunc("/log",page)
http.HandleFunc("/log", func(w http.ResponseWriter, r *http.Request) {
http.HandleFunc("/index", func(w http.ResponseWriter, r *http.Request) {
queryform, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
log.Fatal(err)
......@@ -67,12 +69,14 @@ func server(port int, directory string) {
ip_files, err := get_files(directory + "/" + innerip)
if err != nil {
return_error_res(w, directory+"/"+innerip)
return
}
if li_server_direc, ok := queryform["server_direc"]; ok {
server_direc := li_server_direc[0]
ip_server_files, err := get_files(directory + "/" + innerip + "/" + server_direc)
if err != nil {
return_error_res(w, directory+"/"+innerip+"/"+server_direc)
return
}
if li_filename, ok := queryform["filename"]; ok {
file := li_filename[0]
......@@ -82,6 +86,7 @@ func server(port int, directory string) {
_, err := os.Stat(directory + "/" + filename)
if err != nil {
return_error_res(w, filename)
return
}
fmt.Printf("view log [%s]\n", file)
return_res(w, "templates/page.html", str_filepath)
......@@ -89,12 +94,14 @@ func server(port int, directory string) {
return_res(w, "templates/server_direc.html", strings.Join(ip_server_files, "~~"))
}
} else {
return_res(w, "templates/innnerip.html", strings.Join(ip_files, "~~"))
}
} else {
ips, err := get_files(directory)
if err != nil {
return_error_res(w, directory)
return
}
return_res(w, "templates/index.html", strings.Join(ips, "~~"))
}
......
......@@ -5,8 +5,11 @@
<title>Title</title>
</head>
<body>
<script>alert("File not exist: {{ . }} ,redirect to log page")
window.location.url=window.location.host + "/log"
<script>
alert("File not exist: {{ . }} ,redirect to index page")
window.location.href = "index"
</script>
</body>
</html>
\ No newline at end of file
......@@ -15,7 +15,7 @@
files = document.getElementById("files").innerHTML
snsArr=files.split("~~");
snsArr.forEach((item, index)=>{
$('#show').append("<a style='padding: 10px' href=" + window.location.href + "?&innerip=" + item + ">"+ item +"</a>")
$('#show').append("<a style='padding: 10px' href=" + window.location.href + "?innerip=" + item + ">"+ item +"</a>")
})
</script>
......
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