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
a157592d
authored
Dec 24, 2020
by
lzzzzl
Browse files
Options
_('Browse Files')
Download
Plain Diff
Merge remote-tracking branch 'origin/master'
parents
5299425c
830d16ca
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
152 additions
and
62 deletions
code/server.go
code/webloghandler.go
code/ws.go
go.mod
code/server.go
View file @
a157592d
...
...
@@ -3,7 +3,7 @@ package code
import
(
"bytes"
"fmt"
"g
olang.org/x/net
/websocket"
"g
ithub.com/gorilla
/websocket"
"html/template"
"io/ioutil"
"log"
...
...
@@ -60,7 +60,8 @@ func server(port int, directory string) {
// 返回页面
http
.
HandleFunc
(
"/index"
,
webhandler
.
indexhandle
)
// socket链接
http
.
Handle
(
"/ws"
,
websocket
.
Handler
(
gen
(
directory
,
manager
.
filemap
)))
//http.Handle("/ws", websocket.Handler(gen(directory, manager.filemap)))
http
.
HandleFunc
(
"/ws"
,
genHand
(
directory
,
manager
.
filemap
))
go
manager
.
start
()
...
...
@@ -71,13 +72,23 @@ func server(port int, directory string) {
//log.Fatal()
}
// 创建client对象
func
gen
(
directory
string
,
file_map
map
[
string
]
map
[
Client
]
bool
)
func
(
ws
*
websocket
.
Conn
)
{
var
upGrader
=
websocket
.
Upgrader
{
CheckOrigin
:
func
(
r
*
http
.
Request
)
bool
{
return
true
},
}
f
:=
func
(
ws
*
websocket
.
Conn
)
{
cli
:=
&
Client
{
time
.
Now
()
.
String
(),
ws
,
make
(
chan
[]
byte
,
1024
),
""
}
func
genHand
(
directory
string
,
file_map
map
[
string
]
map
[
Client
]
bool
)
func
(
http
.
ResponseWriter
,
*
http
.
Request
)
{
f
:=
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
queryform
,
err
:=
url
.
ParseQuery
(
ws
.
Request
()
.
URL
.
RawQuery
)
// 完成ws协议的握手操作
// Upgrade:websocket
ws
,
err
:=
upGrader
.
Upgrade
(
w
,
r
,
nil
)
if
err
!=
nil
{
return
}
cli
:=
&
Client
{
time
.
Now
()
.
String
(),
ws
,
make
(
chan
[]
byte
,
1024
),
""
}
queryform
,
err
:=
url
.
ParseQuery
(
r
.
URL
.
RawQuery
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
...
...
@@ -119,3 +130,52 @@ func gen(directory string, file_map map[string]map[Client]bool) func(ws *websock
}
return
f
}
// 创建client对象
//func gen(directory string, file_map map[string]map[Client]bool) func(ws *websocket.Conn) {
//
// f := func(ws *websocket.Conn) {
// cli := &Client{time.Now().String(), ws, make(chan []byte, 1024), ""}
//
// queryform, err := url.ParseQuery(ws.Request().URL.RawQuery)
// if err != nil {
// log.Fatal(err)
// }
// filename := queryform["file"][0]
//
// //初始监听时加载最后500行全部内容
// file_path := directory
// for _, s := range strings.Split(filename, "~~") {
// file_path += "/" + s
// }
// _, err = os.Stat(file_path)
// if err != nil {
// return
// }
// lastcontent_msg, err := return_init_str(file_path, 500)
// if err != nil {
// log.Fatal(err)
// return
// }
// cli.Initstr = lastcontent_msg
//
// // 监控文件
// if _, ok := file_map[filename]; ok {
// file_map[filename][*cli] = true
// fmt.Printf("%s has been monitored\n", filename)
// } else {
// go monitor(file_path, filename)
// l := make(map[Client]bool)
// l[*cli] = true
// file_map[filename] = l
// fmt.Printf("Add a new monitored file [%s]\n", filename)
//
// }
//
// //注册client并开始传输信息
// manager.register <- cli
// go cli.read()
// cli.write()
// }
// return f
//}
code/webloghandler.go
View file @
a157592d
...
...
@@ -2,6 +2,7 @@ package code
import
(
"fmt"
"github.com/kataras/iris/v12"
"log"
"net/http"
"net/url"
...
...
@@ -63,49 +64,49 @@ func (wbh *WebHandler) indexhandle(w http.ResponseWriter, r *http.Request) {
}
//
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, "~~"))
//
}
//
//
}
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 @
a157592d
...
...
@@ -2,7 +2,7 @@ package code
import
(
"fmt"
"g
olang.org/x/net
/websocket"
"g
ithub.com/gorilla
/websocket"
"log"
"strings"
)
...
...
@@ -91,15 +91,17 @@ func (c *Client) write() {
// manager.unregister <- c
// c.Socket.Close()
//}()
c
.
Socket
.
Write
([]
byte
(
c
.
Initstr
))
if
err
:=
c
.
Socket
.
WriteMessage
(
websocket
.
TextMessage
,
[]
byte
(
c
.
Initstr
));
err
!=
nil
{
return
}
for
{
select
{
case
message
,
ok
:=
<-
c
.
Send
:
if
!
ok
{
c
.
Socket
.
Write
Close
(
1
)
c
.
Socket
.
Write
Message
(
websocket
.
TextMessage
,
message
)
return
}
c
.
Socket
.
Write
(
message
)
c
.
Socket
.
Write
Message
(
websocket
.
TextMessage
,
message
)
}
}
}
...
...
@@ -110,19 +112,19 @@ func (c *Client) read() {
}()
//
for
{
var
reply
string
if
err
:=
websocket
.
Message
.
Receive
(
c
.
Socket
,
&
reply
);
err
!=
nil
{
_
,
reply
,
err
:=
c
.
Socket
.
ReadMessage
()
if
err
!=
nil
{
fmt
.
Println
(
"Error! Can't receive message..."
)
break
}
if
reply
!=
"heart"
{
if
string
(
reply
)
!=
"heart"
{
manager
.
unregister
<-
c
c
.
Socket
.
Close
()
}
else
{
fmt
.
Println
(
"heart package..."
)
}
reply
=
""
//reply = []byte
//time.Sleep(2000 * time.Millisecond)
//jsonMessage, _ := json.Marshal(&Message{Sender: c.Id, Content: string(message)})
...
...
go.mod
View file @
a157592d
...
...
@@ -2,4 +2,31 @@ module web_log
go 1.14
require golang.org/x/net v0.0.0-20201216054612-986b41b23924
require (
github.com/CloudyKit/jet/v3 v3.0.1 // indirect
github.com/gin-gonic/gin v1.6.3 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/gorilla/websocket v1.4.2
github.com/iris-contrib/jade v1.1.4 // indirect
github.com/iris-contrib/schema v0.0.6 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/kataras/golog v0.1.6 // indirect
github.com/kataras/iris/v12 v12.1.8
github.com/klauspost/compress v1.11.4 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/microcosm-cc/bluemonday v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/ryanuber/columnize v2.1.2+incompatible // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/ugorji/go v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/net v0.0.0-20201224014010-6772e930b67b
golang.org/x/sys v0.0.0-20201223074533-0d417f636930 // indirect
golang.org/x/text v0.3.4 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)
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