package main import ( _ "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" _ "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 filepath.Ext(ev.Name)==".commit"{ continue } if strings.Index(ev.Name,"历史版本查看")>0{ continue } 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); }else{ filePath :=ev.Name abs := strings.Replace(filePath,config.LocalWorkSpaceDir,"",1) param :=strings.Split(abs,"\\") key :=config.LocalWorkSpaceDir+param[0]+"\\"+param[1] //log.Println("chan-->"+key) ch :=config.GobalWatchChannelMap[key] if ch==nil{ continue } //log.Println("chan-->"+("create"+";"+ev.Name)) ch <- ("create"+";"+ev.Name) } } if ev.Op&fsnotify.Write == fsnotify.Write { //fmt.Println("写入文件 : ", ev.Name); //判断文件,发送事件 fi, err := os.Stat(ev.Name); if err == nil && !fi.IsDir() { filePath :=ev.Name abs := strings.Replace(filePath,config.LocalWorkSpaceDir,"",1) param :=strings.Split(abs,"\\") key :=config.LocalWorkSpaceDir+param[0]+"\\"+param[1] //log.Println("chan-->"+key) ch :=config.GobalWatchChannelMap[key] if ch==nil{ continue } //log.Println("chan-->"+("write"+";"+ev.Name)) //log.Println(ch) ch <- ("write"+";"+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); }else{ filePath :=ev.Name abs := strings.Replace(filePath,config.LocalWorkSpaceDir,"",1) param :=strings.Split(abs,"\\") key :=config.LocalWorkSpaceDir+param[0]+"\\"+param[1] ch :=config.GobalWatchChannelMap[key] if ch==nil{ continue } //log.Println("chan-->"+("remove"+";"+ev.Name)) ch <- ("remove"+";"+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: { log.Printf("error : %v", 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) http.HandleFunc("/messageNotify", websocket.MessageNotifyHandler) http.HandleFunc("/messageMarkRead", websocket.MessageMarkReadHandler) //TODO 消息已读 http.HandleFunc("/queryCommitHistory", websocket.QueryCommitHistoryHandler) http.HandleFunc("/editCommitHistoryMilestone", websocket.EditCommitHistoryMilestoneHandler) //服务端启动 log.Println("服务启动成功,监听端口7777,等待连接。") http.ListenAndServe("0.0.0.0:7777", nil) }