package main import ( "fmt" _ "fmt" "fts/config" "fts/websocket" "github.com/fsnotify/fsnotify" _ "github.com/ipfs/go-ipfs-api" "io" "log" "net/http" "os" _ "os" "path/filepath" "strconv" _ "strings" "time" ) func main() { config.InitConfig() //日志设置 _,err := os.Stat(config.LocalWorkSpaceDir) if err != nil { //创建文件目录 os.MkdirAll(config.LocalWorkSpaceDir, os.ModePerm) } time.Now().Month().String() logpath :=config.LocalWorkSpaceDir+"\\"+"fts_"+strconv.Itoa(time.Now().Year())+time.Now().Month().String()+".log" logFile, err := os.OpenFile(logpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) if err != nil { log.Printf("open log file failed, err:", err) return } multiWriter := io.MultiWriter(os.Stdout,logFile) log.SetOutput(multiWriter) log.SetPrefix("[fts] ") log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) //handle.InitLocalWorkSpace(nil,"324523676458291200","11.4") //文件监控 /*config.GobalWatch, err = fsnotify.NewWatcher() if err != nil { log.Println(err) } go func() { for { select { case ev := <-config.GobalWatch.Events: { log.Println(ev.Op.String()+":"+ev.Name) if ev.Op&fsnotify.Create == fsnotify.Create { fmt.Println("创建文件 : ", ev.Name); //这里获取新创建文件的信息,如果是目录,则加入监控中 fi, err := os.Stat(ev.Name); if err == nil && fi.IsDir() { config.GobalWatch.Add(ev.Name); fmt.Println("添加监控 : ", ev.Name); } } if ev.Op&fsnotify.Write == fsnotify.Write { fmt.Println("写入文件 : ", ev.Name); } if ev.Op&fsnotify.Remove == fsnotify.Remove { fmt.Println("删除文件 : ", ev.Name); //如果删除文件是目录,则移除监控 fi, err := os.Stat(ev.Name); if err == nil && fi.IsDir() { config.GobalWatch.Remove(ev.Name); fmt.Println("删除监控 : ", ev.Name); } } if ev.Op&fsnotify.Rename == fsnotify.Rename { fmt.Println("重命名文件 : ", ev.Name); //如果重命名文件是目录,则移除监控 //注意这里无法使用os.Stat来判断是否是目录了 //因为重命名后,go已经无法找到原文件来获取信息了 //所以这里就简单粗爆的直接remove好了 config.GobalWatch.Remove(ev.Name); } if ev.Op&fsnotify.Chmod == fsnotify.Chmod { fmt.Println("修改权限 : ", ev.Name); } } case err := <-config.GobalWatch.Errors: { fmt.Println("error : ", err); return; } } } }();*/ //defer config.GobalWatch.Close() go func() { //客户端心跳检测 log.Println("keeplive") time.Sleep(20*time.Second) var keepliveTimeOut int64 =5 //20s var lastAccept int64 = time.Now().Unix() //秒时间戳 for true{ //log.Print(!websocket.IsKeeplive) //log.Println((time.Now().Unix()-lastAccept)>keepliveTimeOut) if !websocket.IsKeeplive { if (time.Now().Unix()-lastAccept)>keepliveTimeOut{ log.Println("长时间未检测到心跳 Exit") os.Exit(1) } continue } lastAccept = time.Now().Unix() websocket.IsKeeplive = false //time.Sleep(100*time.Millisecond) } }() //http://localhost:7777/ws http.HandleFunc("/upload", websocket.UploadHandler) http.HandleFunc("/subscriptionFileChange", websocket.SubscriptionFileChangeHandler) http.HandleFunc("/download", websocket.DownloadHandler) http.HandleFunc("/init", websocket.InitLocalWorkSpaceHandler) http.HandleFunc("/getFolderFileInfo", websocket.GetFolderFileInfoHandler) http.HandleFunc("/openFileWith", websocket.OpenFileWithHandler) http.HandleFunc("/checkForUpdates", websocket.CheckForUpdatesHandler) http.HandleFunc("/initClientConfig", websocket.InitClientConfigHandler) http.HandleFunc("/watchFile", websocket.WatchFileHandler) http.HandleFunc("/keeplive", websocket.KeepliveHandler) //服务端启动 log.Println("服务启动成功,监听端口7777,等待连接。") http.ListenAndServe("0.0.0.0:7777", nil) } //func main() { // watch, _ := fsnotify.NewWatcher() // w := Watch{ // watch: watch, // } // w.watchDir("C:\\Users\\yuan_rh\\easycloud\\324523676458291200"); // select {}; //} type Watch struct { watch *fsnotify.Watcher; } //监控目录 func (w *Watch) watchDir(dir string) { //通过Walk来遍历目录下的所有子目录 filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { //这里判断是否为目录,只需监控目录即可 //目录下的文件也在监控范围内,不需要我们一个一个加 if info.IsDir() { path, err := filepath.Abs(path); if err != nil { return err; } err = w.watch.Add(path); if err != nil { return err; } fmt.Println("监控目录 : ", path); } return nil; }); go func() { for { select { case ev := <-w.watch.Events: { if ev.Op&fsnotify.Create == fsnotify.Create { fmt.Println("创建文件 : ", ev.Name); //这里获取新创建文件的信息,如果是目录,则加入监控中 fi, err := os.Stat(ev.Name); if err == nil && fi.IsDir() { w.watch.Add(ev.Name); fmt.Println("添加监控 : ", ev.Name); } } if ev.Op&fsnotify.Write == fsnotify.Write { fmt.Println("写入文件 : ", ev.Name); } if ev.Op&fsnotify.Remove == fsnotify.Remove { fmt.Println("删除文件 : ", ev.Name); //如果删除文件是目录,则移除监控 fi, err := os.Stat(ev.Name); if err == nil && fi.IsDir() { w.watch.Remove(ev.Name); fmt.Println("删除监控 : ", ev.Name); } } if ev.Op&fsnotify.Rename == fsnotify.Rename { fmt.Println("重命名文件 : ", ev.Name); //如果重命名文件是目录,则移除监控 //注意这里无法使用os.Stat来判断是否是目录了 //因为重命名后,go已经无法找到原文件来获取信息了 //所以这里就简单粗爆的直接remove好了 w.watch.Remove(ev.Name); } if ev.Op&fsnotify.Chmod == fsnotify.Chmod { fmt.Println("修改权限 : ", ev.Name); } } case err := <-w.watch.Errors: { fmt.Println("error : ", err); return; } } } }(); }