Commit a9711d52 by lichenggang

version v1.0.3

- 优化前端文件
- 增加文件大小和最后修改日期的显示(进行降序排序)
parent 1db55a3b
......@@ -15,7 +15,7 @@ type User struct {
Password string
}
func GetApp(directory string) *iris.Application {
func GetApp(directory string, deBug bool) *iris.Application {
app := iris.New()
w := &WebHandler{directory}
......@@ -37,17 +37,19 @@ func GetApp(directory string) *iris.Application {
w.loginhandle(ctx, msg)
}
})
app.Use(func(ctx iris.Context) {
sessionid := ctx.GetCookie("sessionid")
ok1, msg := VerifySessionID(sessionid)
fmt.Println(msg)
if ok1 {
ctx.Next()
} else {
w.loginhandle(ctx, "未登录")
}
if !deBug {
app.Use(func(ctx iris.Context) {
sessionid := ctx.GetCookie("sessionid")
ok1, msg := VerifySessionID(sessionid)
fmt.Println(msg)
if ok1 {
ctx.Next()
} else {
w.loginhandle(ctx, "未登录")
}
})
}
})
app.Get("/index", w.indexhandle)
app.Get("/ws", genCtxHand(directory, manager.filemap))
go manager.start()
......
......@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/kataras/iris/v12"
"os"
"strings"
"web_log/utils"
)
......@@ -13,10 +12,16 @@ type WebHandler struct {
}
type Datafile struct {
Htmltitle string
CurFiles string
CurFiles []FileInfo
UrlLocation string
}
type FileInfo struct {
Name string
Size string
LastModify string
}
var base_path = "web/views/base.html"
func (wbh *WebHandler) loginhandle(ctx iris.Context, data interface{}) {
......@@ -27,46 +32,46 @@ func (wbh *WebHandler) indexhandle(ctx iris.Context) {
server_direc := ctx.URLParam("server_direc")
file := ctx.URLParam("filename")
if innerip != "" {
ip_files, err := utils.Get_files(wbh.Directory + "/" + innerip)
if innerip != "" && server_direc != "" && 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(), wbh.Directory+"/"+innerip)
return_error_res(ctx.ResponseWriter(), filename)
return
}
if 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
}
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 {
data := Datafile{Htmltitle: "选择文件", CurFiles: strings.Join(ip_server_files, "~~"), UrlLocation: "&filename="}
return_res(ctx.ResponseWriter(), base_path, data)
}
} else {
data := Datafile{Htmltitle: "选择目录", CurFiles: strings.Join(ip_files, "~~"), UrlLocation: "&server_direc="}
return_res(ctx.ResponseWriter(), base_path, data)
}
fmt.Printf("view log [%s]\n", file)
return_res(ctx.ResponseWriter(), "web/views/page.html", str_filepath)
return
} else {
ips, err := utils.Get_files(wbh.Directory)
var htmltitle, urlloction string
dir := wbh.Directory
if innerip == "" && server_direc == "" && file == "" {
htmltitle = "选择服务器"
urlloction = "?innerip="
}
if innerip != "" && server_direc == "" && file == "" {
htmltitle = "选择服务"
urlloction = "&server_direc="
dir += "/" + innerip
}
if innerip != "" && server_direc != "" && file == "" {
htmltitle = "选择文件"
urlloction = "&filename="
dir += "/" + innerip + "/" + server_direc
}
filesinfo, err := utils.PathFileInfo(dir)
if err != nil {
return_error_res(ctx.ResponseWriter(), wbh.Directory)
return
return_error_res(ctx.ResponseWriter(), dir)
}
data := Datafile{Htmltitle: "选择服务器", CurFiles: strings.Join(ips, "~~"), UrlLocation: "?innerip="}
var fs = make([]FileInfo, 0)
for _, v := range filesinfo {
fs = append(fs, FileInfo{Name: v["file_name"], Size: v["file_size"], LastModify: v["last_modify"]})
}
data := Datafile{Htmltitle: htmltitle, CurFiles: fs, UrlLocation: urlloction}
return_res(ctx.ResponseWriter(), base_path, data)
}
}
......@@ -5,6 +5,7 @@ import (
"github.com/kataras/iris/v12"
"web_log/code"
"web_log/config"
"web_log/utils"
)
// example
......@@ -14,7 +15,9 @@ func main() {
addrs := "0.0.0.0:" + config.Port
fmt.Printf("查看日志目录: %s \n用户配置文件: %s\n", config.Directory, config.UserConfigPath)
app := code.GetApp(config.Directory)
//启动前获取当前系统环境,如果是win则开始调试模式,此时不需要登录
app := code.GetApp(config.Directory, utils.RunEnv())
app.Run(iris.Addr(addrs), iris.WithCharset("UTF-8"))
}
package utils
import (
"encoding/json"
"io/ioutil"
"math"
"os"
"runtime"
"sort"
"strconv"
)
func PathExists(path string) (bool, error) {
......@@ -28,20 +31,68 @@ func Get_files(direct_path string) ([]string, error) {
return li_file, nil
}
// (fileSize string, lastTime string)
func PathFileInfo(path string) string {
files, _ := ioutil.ReadDir(path)
var index int
var infos = make([]map[string]interface{}, len(files))
type MapsSort struct {
Key string
MapList []map[string]interface{}
}
func (m *MapsSort) Len() int {
return len(m.MapList)
}
func (m *MapsSort) Less(i, j int) bool {
return m.MapList[i][m.Key].(float64) > m.MapList[j][m.Key].(float64)
}
func (m *MapsSort) Swap(i, j int) {
m.MapList[i], m.MapList[j] = m.MapList[j], m.MapList[i]
}
func Unwrap(num int64, retain int) float64 {
return float64(num) / math.Pow10(retain)
}
// 返回文件信息[{file_name:$file_name, file_size:$file_size,last_modify:$last_modify}]并按照last_modify字段进行降序排序
func PathFileInfo(path string) ([]map[string]string, error) {
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
var filesinfo = make([]map[string]interface{}, 0)
for _, file := range files {
infos[index] = make(map[string]interface{})
infos[index]["file_size"] = file.Size()
infos[index]["last_modify"] = file.ModTime().Unix()
infos[index]["file_name"] = file.Name()
index++
tmp := make(map[string]interface{})
file_size := Unwrap(file.Size(), 0)
last_modify := Unwrap(file.ModTime().Unix(), 0)
file_name := file.Name()
tmp["file_size"] = file_size //strconv.FormatInt(file_size, 10)
tmp["last_modify"] = last_modify //strconv.FormatInt(last_modify, 10)
tmp["file_name"] = file_name
filesinfo = append(filesinfo, tmp)
}
mapsSort := MapsSort{}
mapsSort.Key = "last_modify"
mapsSort.MapList = filesinfo
sort.Sort(&mapsSort)
data, _ := json.Marshal(infos)
return string(data)
var sort_filesinfo = make([]map[string]string, 0)
for _, v := range mapsSort.MapList {
fsz, _ := v["file_size"].(float64)
lmd, _ := v["last_modify"].(float64)
fnm, _ := v["file_name"].(string)
tmp := make(map[string]string)
tmp["file_size"] = strconv.FormatFloat(fsz, 'f', 6, 64)
tmp["last_modify"] = strconv.FormatFloat(lmd, 'f', 6, 64)
tmp["file_name"] = fnm
sort_filesinfo = append(sort_filesinfo, tmp)
}
return sort_filesinfo, err
}
func RunEnv() bool {
sysType := runtime.GOOS
if sysType == "linux" {
return false
} else {
return true
}
}
......@@ -9,16 +9,38 @@
<body>
<div style="font-size: 20px" >{{ .Htmltitle }}</div>
<div id="files" style="display: none">{{ .CurFiles }}</div>
<div id="show" style="overflow-y: scroll;"></div>
<div id="show" style="overflow-y: scroll;">
</div>
<label ></label>
<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 + {{ .UrlLocation }} + item + ">"+ item +"</a>")
})
//
{{ range .CurFiles }}
var lastmodify = {{ .LastModify }}
var sz = {{ .Size }}
sz = sz/1024
if (1024>sz){
sz = sz.toFixed(3)
sz +='K'
}else {
sz /= 1024
sz = sz.toFixed(3)
sz+= 'M'
}
var f_name = "<label class='tmp' style='margin-right: 1px'>" + {{ .Name }} + "</label>"
{{/* 如果 url里面没有server_direc这个参数,说明还没进到选文件的页面,也就是展示的列表全是文件夹,文件夹不予显示大小*/}}
if ( window.location.href.indexOf("server_direc")==-1){
sz = ""
}
var f_size = "<label class='size' style='margin-right: 100px'>" + sz + "</label>"
var f_lastmodify ="<label>" + (new Date(lastmodify*1000)).toLocaleString() + "</label>"
var f_info = f_name+f_size+f_lastmodify
$('#show').append("<a style='padding: 10px' href=" + window.location.href + {{ $.UrlLocation }} +{{ .Name }} + ">"+ f_info +"</a>")
{{ end }}
</script>
......@@ -50,6 +72,18 @@
color: red;
background-color: #8cbbda;
}
label{
display:inline-block;
width:200px;
text-align:right;
}
.tmp{
display:inline-block;
width:500px;
text-align:left;
}
</style >
</body>
</html>
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