diff --git a/fsv2/README.md b/fsv2/README.md index dcb31d7..4f7f4c6 100644 --- a/fsv2/README.md +++ b/fsv2/README.md @@ -14,4 +14,6 @@ 1、涉及到更新的文件,需要做zip压缩。存放在backup 目录下 2、系统启动的时候,扫描监视目录的变动。 3、扫描结果发送到主控端 -4、计算文件hash,对比文件的列表 \ No newline at end of file +4、计算文件hash,对比文件的列表 + +主控发起查询请求,将查询到的数据 反馈到主控 \ No newline at end of file diff --git a/fsv2/fstc b/fsv2/fstc index 61036d6..78f27f1 100644 Binary files a/fsv2/fstc and b/fsv2/fstc differ diff --git a/fsv2/handler/serverinfo.go b/fsv2/handler/serverinfo.go new file mode 100644 index 0000000..508978a --- /dev/null +++ b/fsv2/handler/serverinfo.go @@ -0,0 +1,102 @@ +package handler + +import ( + "encoding/json" + "fmt" + "net/http" + "os" + "path/filepath" + "strings" + "xtcfs/config" +) + +// json 结构体 +type Response struct { + Status string `json:"status"` //状态 + Data FilesListJson `json:"data"` //目录下的文件 + Scdir string `json:"curdir"` // 扫描的目录 +} + +// 文件输出的结构 +type FileJson struct { + Fname string `json:"fname"` + Dirflag string `json:"dirflag"` +} + +type FilesListJson struct { + Flist []FileJson `json:"list"` +} + +// 遍历监视目录,发送到json中 +func SerInfo(w http.ResponseWriter, r *http.Request) { + // 监听的目录通过?p=的方式传入 + //urlpath := r.Header.Get("p") + urlpath := r.URL.Query().Get("p") + upath := strings.TrimSuffix(urlpath, "nf") + fmt.Printf("upath is %s\n", upath) + // 监听的根目录 + realFilePath := filepath.Join(config.G.FilePath, upath) + downloadPath := filepath.Join(filepath.Base(config.G.FilePath), r.URL.Path[1:]) + // 时间目录的情况 + fileInfo, err := os.Stat(realFilePath) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + data := struct { + Rundir string + IsDir bool + FileName string + DownloadPath string + // Files []os.DirEntry + }{ + Rundir: config.G.FilePath, + DownloadPath: downloadPath, + } + + // list json + var flist FilesListJson + + if fileInfo.IsDir() { + data.IsDir = true + // 遍历目录 + files, err := os.ReadDir(realFilePath) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + // data.Files = files + + for _, v := range files { + flist.Flist = append(flist.Flist, FileJson{Fname: v.Name(), Dirflag: 1}) + } + + } else { + //data.FileName = filepath.Base(realFilePath) + flist.Flist = append(flist.Flist, FileJson{Fname: filepath.Base(realFilePath), Dirflag: 0}) + } + + // respone file list + response := Response{ + Status: "success", + Scdir: upath, + Data: flist, + } + // 设置跨域响应头 + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS,PUT,DELETET") + // w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Accept,Accept-Length,Accept-Encoding,X-XSRF-TOKEN,X-XSRF-TOKEN") + w.Header().Set("Access-Control-Allow-Headers", "*") + // + w.Header().Set("Content-Type", "application/json") + + // 如果是OPTIONS请求,返回200 OK + if r.Method == "OPTIONS" { + // fmt.Printf("options is now \n") + // w.WriteHeader(http.StatusOK) + return + } + + json.NewEncoder(w).Encode(response) +} diff --git a/fsv2/main.go b/fsv2/main.go index da4f74b..bc3568e 100644 --- a/fsv2/main.go +++ b/fsv2/main.go @@ -27,6 +27,8 @@ func receiveClient() error { // 显示状态等 http.HandleFunc("/", handler.ReceiveHandler) + // 服务信息 + http.HandleFunc("/sc", handler.SerInfo) // 开启web 服务, return http.ListenAndServe(config.G.WildcardAddress, nil) }