Commit 90f8915d by lichenggang

优化代码,合并html文件

parent b08ea6b2
......@@ -48,13 +48,14 @@ func GetApp(directory string) *iris.Application {
}
})
app.Get("/index", w.Indexhandle2)
app.Get("/index", w.indexhandle)
app.Get("/ws", genCtxHand(directory, manager.filemap))
go manager.start()
return app
}
// file_map -> {filename : {client: True}}
func genCtxHand(directory string, file_map map[string]map[Client]bool) func(ctx iris.Context) {
f := func(ctx iris.Context) {
w := ctx.ResponseWriter()
......
package code
import (
"github.com/gorilla/websocket"
"html/template"
"log"
"net/http"
)
func return_res(w http.ResponseWriter, template_path string, data interface{}) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(200)
tmpl, err := template.ParseFiles(template_path)
if err != nil {
log.Println("err:", err)
return
}
tmpl.Execute(w, data)
}
func return_error_res(w http.ResponseWriter, str_file string) {
template_path := "web/views/file_not_exist.html"
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(200)
tmpl, err := template.ParseFiles(template_path)
if err != nil {
log.Println("err:", err)
return
}
tmpl.Execute(w, str_file)
}
var upGrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
package code
import (
"log"
"sync"
)
// 启动seelog
func See(filePath string, port int) {
// 检查参数
if !checkParam(filePath, port) {
return
}
var wg sync.WaitGroup
wg.Add(1)
go server(port, filePath)
wg.Wait()
}
// 参数验证
func checkParam(filePath string, port int) bool {
if filePath == "" {
log.Println("filePath 不可为空")
return false
}
if port == 0 {
log.Println("port 不可为空")
return false
}
return true
}
package code
import (
"bytes"
"fmt"
"github.com/gorilla/websocket"
"html/template"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
func get_files(direct_path string) ([]string, error) {
var li_file []string
files, err := ioutil.ReadDir(direct_path)
if err != nil {
return nil, err
}
for _, f := range files {
li_file = append(li_file, f.Name())
}
return li_file, nil
}
func return_res(w http.ResponseWriter, template_path string, data interface{}) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(200)
tmpl, err := template.ParseFiles(template_path)
if err != nil {
log.Println("err:", err)
return
}
tmpl.Execute(w, data)
}
func return_error_res(w http.ResponseWriter, str_file string) {
template_path := "web/views/file_not_exist.html"
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(200)
tmpl, err := template.ParseFiles(template_path)
if err != nil {
log.Println("err:", err)
return
}
tmpl.Execute(w, str_file)
}
// 开启 httpServer
func server(port int, directory string) {
defer func() {
if err := recover(); err != nil {
log.Printf("[seelog] error:%+v", err)
}
}()
var webhandler = WebHandler{Directory: directory}
// 返回页面
http.HandleFunc("/index", webhandler.indexhandle)
// socket链接
//http.Handle("/ws", websocket.Handler(gen(directory, manager.filemap)))
http.HandleFunc("/ws", genHand(directory, manager.filemap))
go manager.start()
var bt bytes.Buffer
bt.WriteString("0.0.0.0:")
bt.WriteString(strconv.Itoa(port))
http.ListenAndServe(bt.String(), nil)
//log.Fatal()
}
var upGrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func genHand(directory string, file_map map[string]map[Client]bool) func(http.ResponseWriter, *http.Request) {
f := func(w http.ResponseWriter, r *http.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
}
// 创建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
//}
......@@ -3,84 +3,39 @@ package code
import (
"fmt"
"github.com/kataras/iris/v12"
"log"
"net/http"
"net/url"
"os"
"strings"
"web_log/utils"
)
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, "~~"))
}
type Datafile struct {
Htmltitle string
CurFiles string
UrlLocation string
}
var base_path = "web/views/base.html"
func (wbh *WebHandler) loginhandle(ctx iris.Context, data interface{}) {
return_res(ctx.ResponseWriter(), "web/views/login.html", data)
}
func (wbh *WebHandler) Indexhandle2(ctx iris.Context) {
func (wbh *WebHandler) indexhandle(ctx iris.Context) {
innerip := ctx.URLParam("innerip")
server_direc := ctx.URLParam("server_direc")
file := ctx.URLParam("filename")
if innerip != "" {
ip_files, err := get_files(wbh.Directory + "/" + innerip)
ip_files, err := utils.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)
ip_server_files, err := utils.Get_files(wbh.Directory + "/" + innerip + "/" + server_direc)
if err != nil {
return_error_res(ctx.ResponseWriter(), wbh.Directory+"/"+innerip+"/"+server_direc)
return
......@@ -97,19 +52,21 @@ func (wbh *WebHandler) Indexhandle2(ctx iris.Context) {
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, "~~"))
data := Datafile{Htmltitle: "选择文件", CurFiles: strings.Join(ip_server_files, "~~"), UrlLocation: "&filename="}
return_res(ctx.ResponseWriter(), base_path, data)
}
} else {
return_res(ctx.ResponseWriter(), "web/views/innnerip.html", strings.Join(ip_files, "~~"))
data := Datafile{Htmltitle: "选择目录", CurFiles: strings.Join(ip_files, "~~"), UrlLocation: "&server_direc="}
return_res(ctx.ResponseWriter(), base_path, data)
}
} else {
ips, err := get_files(wbh.Directory)
ips, err := utils.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, "~~"))
data := Datafile{Htmltitle: "选择服务器", CurFiles: strings.Join(ips, "~~"), UrlLocation: "?innerip="}
return_res(ctx.ResponseWriter(), base_path, data)
}
}
package utils
import "os"
import (
"io/ioutil"
"os"
)
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
......@@ -12,3 +15,14 @@ func PathExists(path string) (bool, error) {
}
return false, err
}
func Get_files(direct_path string) ([]string, error) {
var li_file []string
files, err := ioutil.ReadDir(direct_path)
if err != nil {
return nil, err
}
for _, f := range files {
li_file = append(li_file, f.Name())
}
return li_file, nil
}
......@@ -3,23 +3,25 @@
<head>
<meta charset="UTF-8">
<title>服务器</title>
<title></title>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<div style="font-size: 20px" >选择服务器</div>
<div id="files" style="display: none">{{ . }}</div>
<div style="font-size: 20px" >{{ .Htmltitle }}</div>
<div id="files" style="display: none">{{ .CurFiles }}</div>
<div id="show" style="overflow-y: scroll;"></div>
<script>
$("#show").height(document.documentElement.scrollHeight-50)
files = document.getElementById("files").innerHTML
snsArr=files.split("~~");
snsArr.forEach((item, index)=>{
$('#show').append("<a style='padding: 10px' href=" + window.location.href + "?innerip=" + item + ">"+ item +"</a>")
$('#show').append("<a style='padding: 10px' href=" + window.location.href + {{ .UrlLocation }} + item + ">"+ item +"</a>")
})
</script>
<style>
html{
width: 100%;
......@@ -48,6 +50,6 @@
color: red;
background-color: #8cbbda;
}
</style >
</style >
</body>
</html>
\ No newline at end of file
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>选服务</title>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<div style="font-size: 20px">选择服务</div>
<div id="files" style="display: none">{{ . }}</div>
<div id="show" style="overflow-y: scroll;"></div>
<script>
$("#show").height(document.documentElement.scrollHeight-50)
files = document.getElementById("files").innerHTML
snsArr=files.split("~~");
snsArr.forEach((item, index)=>{
$('#show').append("<a style='padding: 10px' href=" + window.location.href + "&server_direc=" + item + ">"+ item +"</a>")
})
</script>
<style>
html{
width: 100%;
height: 100%;
overflow: hidden;
}
body{
width: 100%;
height: 100%;
font-family: 'Open Sans',sans-serif;
margin: 0;
background-color: #cccccc;
}
a:active,
a:visited,
a:link {
display: block;
text-decoration: none;
margin: 6px 10px 6px 0px;
padding: 2px 6px 2px 6px;
color: #00527f;
background-color: #d9e8f3;
border: 1px solid #004266;
}
a:hover {
color: red;
background-color: #8cbbda;
}
</style >
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>选日志文件</title>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<div style="font-size: 20px">选择日志文件</div>
<div id="files" style="display: none">{{ . }}</div>
<div id="show" style="overflow-y: scroll;"></div>
<script>
$("#show").height(document.documentElement.scrollHeight-50)
files = document.getElementById("files").innerHTML
snsArr=files.split("~~");
snsArr.forEach((item, index)=>{
$('#show').append("<a style='padding: 10px' href=" + window.location.href + "&filename=" + item + ">"+ item +"</a>")
})
</script>
<style>
html{
width: 100%;
height: 100%;
overflow: hidden;
}
body{
width: 100%;
height: 100%;
font-family: 'Open Sans',sans-serif;
margin: 0;
background-color: #cccccc;
}
a:active,
a:visited,
a:link {
display: block;
text-decoration: none;
margin: 6px 10px 6px 0px;
padding: 2px 6px 2px 6px;
color: #00527f;
background-color: #d9e8f3;
border: 1px solid #004266;
}
a:hover {
color: red;
background-color: #8cbbda;
}
</style >
</body>
</html>
\ No newline at end of file
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