Commit 23ea8bab by lichenggang

采用iris框架

parent 830d16ca
package code
import (
"fmt"
"github.com/kataras/iris/v12"
"log"
"net/url"
"os"
"strings"
"time"
)
func GetApp(directory string, port int) *iris.Application {
app := iris.New()
w := &WebHandler{"E:\\test_logs"}
app.Get("/index", w.Indexhandle2)
app.Get("/ws", genCtxHand(directory, manager.filemap))
go manager.start()
return app
}
func genCtxHand(directory string, file_map map[string]map[Client]bool) func(ctx iris.Context) {
f := func(ctx iris.Context) {
w := ctx.ResponseWriter()
r := ctx.Request()
// 完成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)
}
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
}
......@@ -67,7 +67,7 @@ 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")
file := ctx.URLParam("filename")
if innerip != "" {
ip_files, err := get_files(wbh.Directory + "/" + innerip)
......
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