Commit e907d7c6 by lichenggang

检测文件是否存在

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