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
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
10 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{}) {
...
@@ -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
,
"~~"
))
}
}
...
...
templates/file_not_exist.html
View file @
e907d7c6
...
@@ -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
templates/index.html
View file @
e907d7c6
...
@@ -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>
...
...
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