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
1ae14b4b
authored
Dec 24, 2020
by
lichenggang
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
重构代码
parent
30cddf73
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
148 additions
and
84 deletions
code/server.go
code/webloghandler.go
code/ws.go
templates/file_not_exist.html → web/views/file_not_exist.html
templates/index.html → web/views/index.html
templates/innnerip.html → web/views/innnerip.html
templates/page.html → web/views/page.html
templates/server_direc.html → web/views/server_direc.html
code/server.go
View file @
1ae14b4b
...
...
@@ -37,7 +37,7 @@ 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
)
{
template_path
:=
"
template
s/file_not_exist.html"
template_path
:=
"
web/view
s/file_not_exist.html"
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/html"
)
w
.
WriteHeader
(
200
)
tmpl
,
err
:=
template
.
ParseFiles
(
template_path
)
...
...
@@ -56,57 +56,9 @@ func server(port int, directory string) {
log
.
Printf
(
"[seelog] error:%+v"
,
err
)
}
}()
var
webhandler
=
WebHandler
{
Directory
:
directory
}
// 返回页面
http
.
HandleFunc
(
"/index"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
queryform
,
err
:=
url
.
ParseQuery
(
r
.
URL
.
RawQuery
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
if
li_innerip
,
ok
:=
queryform
[
"innerip"
];
ok
{
innerip
:=
li_innerip
[
0
]
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
]
str_filepath
:=
innerip
+
"~~"
+
server_direc
+
"~~"
+
file
filename
:=
innerip
+
"/"
+
server_direc
+
"/"
+
file
_
,
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
)
}
else
{
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
,
"~~"
))
}
})
http
.
HandleFunc
(
"/index"
,
webhandler
.
indexhandle
)
// socket链接
http
.
Handle
(
"/ws"
,
websocket
.
Handler
(
gen
(
directory
,
manager
.
filemap
)))
...
...
@@ -120,9 +72,10 @@ func server(port int, directory string) {
}
// 创建client对象
func
gen
(
directory
string
,
file_map
map
[
string
]
map
[
client
]
bool
)
func
(
ws
*
websocket
.
Conn
)
{
func
gen
(
directory
string
,
file_map
map
[
string
]
map
[
Client
]
bool
)
func
(
ws
*
websocket
.
Conn
)
{
f
:=
func
(
ws
*
websocket
.
Conn
)
{
cli
:=
&
c
lient
{
time
.
Now
()
.
String
(),
ws
,
make
(
chan
[]
byte
,
1024
),
""
}
cli
:=
&
C
lient
{
time
.
Now
()
.
String
(),
ws
,
make
(
chan
[]
byte
,
1024
),
""
}
queryform
,
err
:=
url
.
ParseQuery
(
ws
.
Request
()
.
URL
.
RawQuery
)
if
err
!=
nil
{
...
...
@@ -144,7 +97,7 @@ func gen(directory string, file_map map[string]map[client]bool) func(ws *websock
log
.
Fatal
(
err
)
return
}
cli
.
i
nitstr
=
lastcontent_msg
cli
.
I
nitstr
=
lastcontent_msg
// 监控文件
if
_
,
ok
:=
file_map
[
filename
];
ok
{
...
...
@@ -152,7 +105,7 @@ func gen(directory string, file_map map[string]map[client]bool) func(ws *websock
fmt
.
Printf
(
"%s has been monitored
\n
"
,
filename
)
}
else
{
go
monitor
(
file_path
,
filename
)
l
:=
make
(
map
[
c
lient
]
bool
)
l
:=
make
(
map
[
C
lient
]
bool
)
l
[
*
cli
]
=
true
file_map
[
filename
]
=
l
fmt
.
Printf
(
"Add a new monitored file [%s]
\n
"
,
filename
)
...
...
code/webloghandler.go
0 → 100644
View file @
1ae14b4b
package
code
import
(
"fmt"
"log"
"net/http"
"net/url"
"os"
"strings"
)
type
WebHandler
struct
{
Directory
string
}
func
(
wbh
*
WebHandler
)
indexhandle
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
queryform
,
err
:=
url
.
ParseQuery
(
r
.
URL
.
RawQuery
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
if
li_innerip
,
ok
:=
queryform
[
"innerip"
];
ok
{
innerip
:=
li_innerip
[
0
]
ip_files
,
err
:=
get_files
(
wbh
.
Directory
+
"/"
+
innerip
)
if
err
!=
nil
{
return_error_res
(
w
,
wbh
.
Directory
+
"/"
+
innerip
)
return
}
if
li_server_direc
,
ok
:=
queryform
[
"server_direc"
];
ok
{
server_direc
:=
li_server_direc
[
0
]
ip_server_files
,
err
:=
get_files
(
wbh
.
Directory
+
"/"
+
innerip
+
"/"
+
server_direc
)
if
err
!=
nil
{
return_error_res
(
w
,
wbh
.
Directory
+
"/"
+
innerip
+
"/"
+
server_direc
)
return
}
if
li_filename
,
ok
:=
queryform
[
"filename"
];
ok
{
file
:=
li_filename
[
0
]
str_filepath
:=
innerip
+
"~~"
+
server_direc
+
"~~"
+
file
filename
:=
innerip
+
"/"
+
server_direc
+
"/"
+
file
_
,
err
:=
os
.
Stat
(
wbh
.
Directory
+
"/"
+
filename
)
if
err
!=
nil
{
return_error_res
(
w
,
filename
)
return
}
fmt
.
Printf
(
"view log [%s]
\n
"
,
file
)
return_res
(
w
,
"web/views/page.html"
,
str_filepath
)
}
else
{
return_res
(
w
,
"web/views/server_direc.html"
,
strings
.
Join
(
ip_server_files
,
"~~"
))
}
}
else
{
return_res
(
w
,
"web/views/innnerip.html"
,
strings
.
Join
(
ip_files
,
"~~"
))
}
}
else
{
ips
,
err
:=
get_files
(
wbh
.
Directory
)
if
err
!=
nil
{
return_error_res
(
w
,
wbh
.
Directory
)
return
}
return_res
(
w
,
"web/views/index.html"
,
strings
.
Join
(
ips
,
"~~"
))
}
}
//func (wbh *WebHandler) Indexhandle2(ctx iris.Context) {
// innerip := ctx.URLParam("innerip")
// server_direc := ctx.URLParam("server_direc")
// file := ctx.URLParam("file")
//
// if innerip != "" {
// ip_files, err := get_files(wbh.Directory + "/" + innerip)
//
// if err != nil {
// return_error_res(ctx.ResponseWriter(), wbh.Directory+"/"+innerip)
// return
// }
// if server_direc != "" {
// ip_server_files, err := get_files(wbh.Directory + "/" + innerip + "/" + server_direc)
// if err != nil {
// return_error_res(ctx.ResponseWriter(), wbh.Directory+"/"+innerip+"/"+server_direc)
// return
// }
// if file != "" {
// str_filepath := innerip + "~~" + server_direc + "~~" + file
// filename := innerip + "/" + server_direc + "/" + file
//
// _, err := os.Stat(wbh.Directory + "/" + filename)
// if err != nil {
// return_error_res(ctx.ResponseWriter(), filename)
// return
// }
// fmt.Printf("view log [%s]\n", file)
// return_res(ctx.ResponseWriter(), "web/views/page.html", str_filepath)
// } else {
// return_res(ctx.ResponseWriter(), "web/views/server_direc.html", strings.Join(ip_server_files, "~~"))
// }
// } else {
//
// return_res(ctx.ResponseWriter(), "web/views/innnerip.html", strings.Join(ip_files, "~~"))
// }
// } else {
// ips, err := get_files(wbh.Directory)
// if err != nil {
// return_error_res(ctx.ResponseWriter(), wbh.Directory)
// return
// }
// return_res(ctx.ResponseWriter(), "web/views/index.html", strings.Join(ips, "~~"))
// }
//
//}
code/ws.go
View file @
1ae14b4b
...
...
@@ -8,32 +8,32 @@ import (
)
// websocket客户端
type
c
lient
struct
{
i
d
string
s
ocket
*
websocket
.
Conn
s
end
chan
[]
byte
i
nitstr
string
type
C
lient
struct
{
I
d
string
S
ocket
*
websocket
.
Conn
S
end
chan
[]
byte
I
nitstr
string
}
// 客户端管理
type
clientManager
struct
{
clients
map
[
*
c
lient
]
bool
clients
map
[
*
C
lient
]
bool
broadcast
chan
[]
byte
register
chan
*
c
lient
unregister
chan
*
c
lient
filemap
map
[
string
]
map
[
c
lient
]
bool
register
chan
*
C
lient
unregister
chan
*
C
lient
filemap
map
[
string
]
map
[
C
lient
]
bool
}
var
manager
=
clientManager
{
broadcast
:
make
(
chan
[]
byte
),
register
:
make
(
chan
*
c
lient
),
unregister
:
make
(
chan
*
c
lient
),
clients
:
make
(
map
[
*
c
lient
]
bool
),
register
:
make
(
chan
*
C
lient
),
unregister
:
make
(
chan
*
C
lient
),
clients
:
make
(
map
[
*
C
lient
]
bool
),
filemap
:
getfilemap
(),
}
func
getfilemap
()
map
[
string
]
map
[
c
lient
]
bool
{
file_map
:=
make
(
map
[
string
]
map
[
c
lient
]
bool
)
func
getfilemap
()
map
[
string
]
map
[
C
lient
]
bool
{
file_map
:=
make
(
map
[
string
]
map
[
C
lient
]
bool
)
return
file_map
}
...
...
@@ -52,7 +52,7 @@ func (manager *clientManager) start() {
case
conn
:=
<-
manager
.
unregister
:
if
_
,
ok
:=
manager
.
clients
[
conn
];
ok
{
close
(
conn
.
s
end
)
close
(
conn
.
S
end
)
delete
(
manager
.
clients
,
conn
)
for
_
,
map_tmp
:=
range
manager
.
filemap
{
delete
(
map_tmp
,
*
conn
)
...
...
@@ -67,18 +67,18 @@ func (manager *clientManager) start() {
map_tmp
:=
manager
.
filemap
[
filename
]
for
conn
,
_
:=
range
map_tmp
{
select
{
case
conn
.
s
end
<-
[]
byte
(
msg
)
:
case
conn
.
S
end
<-
[]
byte
(
msg
)
:
default
:
close
(
conn
.
s
end
)
close
(
conn
.
S
end
)
delete
(
manager
.
clients
,
&
conn
)
}
}
//for conn := range manager.clients {
// select {
// case conn.
s
end <- message:
// case conn.
S
end <- message:
// default:
// close(conn.
s
end)
// close(conn.
S
end)
// delete(manager.clients, conn)
// }
//}
...
...
@@ -86,46 +86,46 @@ func (manager *clientManager) start() {
}
}
func
(
c
*
c
lient
)
write
()
{
func
(
c
*
C
lient
)
write
()
{
//defer func() {
// manager.unregister <- c
// c.
s
ocket.Close()
// c.
S
ocket.Close()
//}()
c
.
socket
.
Write
([]
byte
(
c
.
i
nitstr
))
c
.
Socket
.
Write
([]
byte
(
c
.
I
nitstr
))
for
{
select
{
case
message
,
ok
:=
<-
c
.
s
end
:
case
message
,
ok
:=
<-
c
.
S
end
:
if
!
ok
{
c
.
s
ocket
.
WriteClose
(
1
)
c
.
S
ocket
.
WriteClose
(
1
)
return
}
c
.
s
ocket
.
Write
(
message
)
c
.
S
ocket
.
Write
(
message
)
}
}
}
func
(
c
*
c
lient
)
read
()
{
func
(
c
*
C
lient
)
read
()
{
defer
func
()
{
manager
.
unregister
<-
c
}()
//
for
{
var
reply
string
if
err
:=
websocket
.
Message
.
Receive
(
c
.
s
ocket
,
&
reply
);
err
!=
nil
{
if
err
:=
websocket
.
Message
.
Receive
(
c
.
S
ocket
,
&
reply
);
err
!=
nil
{
fmt
.
Println
(
"Error! Can't receive message..."
)
break
}
if
reply
!=
"heart"
{
manager
.
unregister
<-
c
c
.
s
ocket
.
Close
()
c
.
S
ocket
.
Close
()
}
else
{
fmt
.
Println
(
"heart package..."
)
}
reply
=
""
//time.Sleep(2000 * time.Millisecond)
//jsonMessage, _ := json.Marshal(&Message{Sender: c.
i
d, Content: string(message)})
//jsonMessage, _ := json.Marshal(&Message{Sender: c.
I
d, Content: string(message)})
//manager.broadcast <- jsonMessage
}
}
...
...
template
s/file_not_exist.html
→
web/view
s/file_not_exist.html
View file @
1ae14b4b
File moved
template
s/index.html
→
web/view
s/index.html
View file @
1ae14b4b
File moved
template
s/innnerip.html
→
web/view
s/innnerip.html
View file @
1ae14b4b
File moved
template
s/page.html
→
web/view
s/page.html
View file @
1ae14b4b
File moved
template
s/server_direc.html
→
web/view
s/server_direc.html
View file @
1ae14b4b
File moved
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