Commit 67ac824a by lzzzzl

Merge remote-tracking branch 'origin/master'

parents ee221d82 1b1acd6b
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) { ...@@ -67,7 +67,7 @@ func (wbh *WebHandler) indexhandle(w http.ResponseWriter, r *http.Request) {
func (wbh *WebHandler) Indexhandle2(ctx iris.Context) { func (wbh *WebHandler) Indexhandle2(ctx iris.Context) {
innerip := ctx.URLParam("innerip") innerip := ctx.URLParam("innerip")
server_direc := ctx.URLParam("server_direc") server_direc := ctx.URLParam("server_direc")
file := ctx.URLParam("file") file := ctx.URLParam("filename")
if innerip != "" { if innerip != "" {
ip_files, err := get_files(wbh.Directory + "/" + innerip) ip_files, err := get_files(wbh.Directory + "/" + innerip)
......
package main package main
import ( import (
"github.com/kataras/iris/v12"
"os" "os"
"web_log/code" "web_log/code"
) )
...@@ -16,6 +17,7 @@ func main() { ...@@ -16,6 +17,7 @@ func main() {
if logenv == "PRODUCT" { if logenv == "PRODUCT" {
Directory = "/data/soft/rsync_log" Directory = "/data/soft/rsync_log"
} }
code.See(Directory, TestPort) app := code.GetApp(Directory, TestPort)
app.Run(iris.Addr("0.0.0.0:9997"), iris.WithCharset("UTF-8"))
} }
package main
import (
"os"
"web_log/code"
)
// example
func main() {
Directory := "E:\\test_logs" // directory of the log
TestPort := 9997
logenv := os.Getenv("GOLOGENV")
if logenv == "TEST" {
Directory = "/data/soft/bigdatav3.0/test_rsync_log"
}
if logenv == "PRODUCT" {
Directory = "/data/soft/rsync_log"
}
code.See(Directory, TestPort)
}
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