Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
lichenggang
/
web_log
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
e907d7c6
authored
Dec 23, 2020
by
lichenggang
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
检测文件是否存在
parent
474613cb
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
22 additions
and
11 deletions
code/server.go
templates/file_not_exist.html
templates/index.html
code/server.go
View file @
e907d7c6
...
...
@@ -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
,
"~~"
))
}
...
...
templates/file_not_exist.html
View file @
e907d7c6
...
...
@@ -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
templates/index.html
View file @
e907d7c6
...
...
@@ -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>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment