Commit 1ae14b4b by lichenggang

重构代码

parent 30cddf73
......@@ -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 := "templates/file_not_exist.html"
template_path := "web/views/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 := &client{time.Now().String(), ws, make(chan []byte, 1024), ""}
cli := &Client{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.initstr = lastcontent_msg
cli.Initstr = 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[client]bool)
l := make(map[Client]bool)
l[*cli] = true
file_map[filename] = l
fmt.Printf("Add a new monitored file [%s]\n", filename)
......
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, "~~"))
// }
//
//}
......@@ -8,32 +8,32 @@ import (
)
// websocket客户端
type client struct {
id string
socket *websocket.Conn
send chan []byte
initstr string
type Client struct {
Id string
Socket *websocket.Conn
Send chan []byte
Initstr string
}
// 客户端管理
type clientManager struct {
clients map[*client]bool
clients map[*Client]bool
broadcast chan []byte
register chan *client
unregister chan *client
filemap map[string]map[client]bool
register chan *Client
unregister chan *Client
filemap map[string]map[Client]bool
}
var manager = clientManager{
broadcast: make(chan []byte),
register: make(chan *client),
unregister: make(chan *client),
clients: make(map[*client]bool),
register: make(chan *Client),
unregister: make(chan *Client),
clients: make(map[*Client]bool),
filemap: getfilemap(),
}
func getfilemap() map[string]map[client]bool {
file_map := make(map[string]map[client]bool)
func getfilemap() map[string]map[Client]bool {
file_map := make(map[string]map[Client]bool)
return file_map
}
......@@ -52,7 +52,7 @@ func (manager *clientManager) start() {
case conn := <-manager.unregister:
if _, ok := manager.clients[conn]; ok {
close(conn.send)
close(conn.Send)
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.send <- []byte(msg):
case conn.Send <- []byte(msg):
default:
close(conn.send)
close(conn.Send)
delete(manager.clients, &conn)
}
}
//for conn := range manager.clients {
// select {
// case conn.send <- message:
// case conn.Send <- message:
// default:
// close(conn.send)
// close(conn.Send)
// delete(manager.clients, conn)
// }
//}
......@@ -86,46 +86,46 @@ func (manager *clientManager) start() {
}
}
func (c *client) write() {
func (c *Client) write() {
//defer func() {
// manager.unregister <- c
// c.socket.Close()
// c.Socket.Close()
//}()
c.socket.Write([]byte(c.initstr))
c.Socket.Write([]byte(c.Initstr))
for {
select {
case message, ok := <-c.send:
case message, ok := <-c.Send:
if !ok {
c.socket.WriteClose(1)
c.Socket.WriteClose(1)
return
}
c.socket.Write(message)
c.Socket.Write(message)
}
}
}
func (c *client) read() {
func (c *Client) read() {
defer func() {
manager.unregister <- c
}()
//
for {
var reply string
if err := websocket.Message.Receive(c.socket, &reply); err != nil {
if err := websocket.Message.Receive(c.Socket, &reply); err != nil {
fmt.Println("Error! Can't receive message...")
break
}
if reply != "heart" {
manager.unregister <- c
c.socket.Close()
c.Socket.Close()
} else {
fmt.Println("heart package...")
}
reply = ""
//time.Sleep(2000 * time.Millisecond)
//jsonMessage, _ := json.Marshal(&Message{Sender: c.id, Content: string(message)})
//jsonMessage, _ := json.Marshal(&Message{Sender: c.Id, Content: string(message)})
//manager.broadcast <- jsonMessage
}
}
......
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