文件同步
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
5.8 KiB

  1. package main
  2. import (
  3. _ "fmt"
  4. "fts/config"
  5. "fts/websocket"
  6. "github.com/fsnotify/fsnotify"
  7. _ "github.com/ipfs/go-ipfs-api"
  8. "io"
  9. "log"
  10. "net/http"
  11. "os"
  12. _ "os"
  13. "path/filepath"
  14. "strconv"
  15. "strings"
  16. _ "strings"
  17. "time"
  18. )
  19. func main() {
  20. config.InitConfig()
  21. //日志设置
  22. _,err := os.Stat(config.LocalWorkSpaceDir)
  23. if err != nil {
  24. //创建文件目录
  25. os.MkdirAll(config.LocalWorkSpaceDir, os.ModePerm)
  26. }
  27. time.Now().Month().String()
  28. logpath :=config.LocalWorkSpaceDir+"\\"+"fts_"+strconv.Itoa(time.Now().Year())+time.Now().Month().String()+".log"
  29. logFile, err := os.OpenFile(logpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
  30. if err != nil {
  31. log.Printf("open log file failed, err:", err)
  32. return
  33. }
  34. multiWriter := io.MultiWriter(os.Stdout,logFile)
  35. log.SetOutput(multiWriter)
  36. log.SetPrefix("[fts] ")
  37. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  38. //handle.InitLocalWorkSpace(nil,"324523676458291200","11.4")
  39. //文件监控
  40. config.GobalWatch, err = fsnotify.NewWatcher()
  41. if err != nil {
  42. log.Println(err)
  43. }
  44. go func() {
  45. for {
  46. select {
  47. case ev := <-config.GobalWatch.Events:
  48. {
  49. //log.Println(ev.Op.String()+":"+ev.Name)
  50. if filepath.Ext(ev.Name)==".commit"{
  51. continue
  52. }
  53. if strings.Index(ev.Name,"历史版本查看")>0{
  54. continue
  55. }
  56. if ev.Op&fsnotify.Create == fsnotify.Create {
  57. //fmt.Println("创建文件 : ", ev.Name);
  58. //这里获取新创建文件的信息,如果是目录,则加入监控中
  59. fi, err := os.Stat(ev.Name);
  60. if err == nil && fi.IsDir() {
  61. config.GobalWatch.Add(ev.Name);
  62. //fmt.Println("添加监控 : ", ev.Name);
  63. }else{
  64. filePath :=ev.Name
  65. abs := strings.Replace(filePath,config.LocalWorkSpaceDir,"",1)
  66. param :=strings.Split(abs,"\\")
  67. key :=config.LocalWorkSpaceDir+param[0]+"\\"+param[1]
  68. //log.Println("chan-->"+key)
  69. ch :=config.GobalWatchChannelMap[key]
  70. if ch==nil{
  71. continue
  72. }
  73. //log.Println("chan-->"+("create"+";"+ev.Name))
  74. ch <- ("create"+";"+ev.Name)
  75. }
  76. }
  77. if ev.Op&fsnotify.Write == fsnotify.Write {
  78. //fmt.Println("写入文件 : ", ev.Name);
  79. //判断文件,发送事件
  80. fi, err := os.Stat(ev.Name);
  81. if err == nil && !fi.IsDir() {
  82. filePath :=ev.Name
  83. abs := strings.Replace(filePath,config.LocalWorkSpaceDir,"",1)
  84. param :=strings.Split(abs,"\\")
  85. key :=config.LocalWorkSpaceDir+param[0]+"\\"+param[1]
  86. //log.Println("chan-->"+key)
  87. ch :=config.GobalWatchChannelMap[key]
  88. if ch==nil{
  89. continue
  90. }
  91. //log.Println("chan-->"+("write"+";"+ev.Name))
  92. //log.Println(ch)
  93. ch <- ("write"+";"+ev.Name)
  94. }
  95. }
  96. if ev.Op&fsnotify.Remove == fsnotify.Remove {
  97. //fmt.Println("删除文件 : ", ev.Name);
  98. //如果删除文件是目录,则移除监控
  99. fi, err := os.Stat(ev.Name);
  100. if err == nil && fi.IsDir() {
  101. config.GobalWatch.Remove(ev.Name);
  102. //fmt.Println("删除监控 : ", ev.Name);
  103. }else{
  104. filePath :=ev.Name
  105. abs := strings.Replace(filePath,config.LocalWorkSpaceDir,"",1)
  106. param :=strings.Split(abs,"\\")
  107. key :=config.LocalWorkSpaceDir+param[0]+"\\"+param[1]
  108. ch :=config.GobalWatchChannelMap[key]
  109. if ch==nil{
  110. continue
  111. }
  112. //log.Println("chan-->"+("remove"+";"+ev.Name))
  113. ch <- ("remove"+";"+ev.Name)
  114. }
  115. }
  116. if ev.Op&fsnotify.Rename == fsnotify.Rename {
  117. //fmt.Println("重命名文件 : ", ev.Name);
  118. //如果重命名文件是目录,则移除监控
  119. //注意这里无法使用os.Stat来判断是否是目录了
  120. //因为重命名后,go已经无法找到原文件来获取信息了
  121. //所以这里就简单粗爆的直接remove好了
  122. config.GobalWatch.Remove(ev.Name);
  123. }
  124. if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
  125. //fmt.Println("修改权限 : ", ev.Name);
  126. }
  127. }
  128. case err := <-config.GobalWatch.Errors:
  129. {
  130. log.Printf("error : %v", err);
  131. return;
  132. }
  133. }
  134. }
  135. }();
  136. defer config.GobalWatch.Close()
  137. go func() {
  138. //客户端心跳检测
  139. log.Println("keeplive")
  140. time.Sleep(20*time.Second)
  141. var keepliveTimeOut int64 =5 //20s
  142. var lastAccept int64 = time.Now().Unix() //秒时间戳
  143. for true{
  144. //log.Print(!websocket.IsKeeplive)
  145. //log.Println((time.Now().Unix()-lastAccept)>keepliveTimeOut)
  146. if !websocket.IsKeeplive {
  147. if (time.Now().Unix()-lastAccept)>keepliveTimeOut{
  148. log.Println("长时间未检测到心跳 Exit")
  149. os.Exit(1)
  150. }
  151. continue
  152. }
  153. lastAccept = time.Now().Unix()
  154. websocket.IsKeeplive = false
  155. //time.Sleep(100*time.Millisecond)
  156. }
  157. }()
  158. //http://localhost:7777/ws
  159. http.HandleFunc("/upload", websocket.UploadHandler)
  160. http.HandleFunc("/subscriptionFileChange", websocket.SubscriptionFileChangeHandler)
  161. http.HandleFunc("/download", websocket.DownloadHandler)
  162. http.HandleFunc("/init", websocket.InitLocalWorkSpaceHandler)
  163. http.HandleFunc("/getFolderFileInfo", websocket.GetFolderFileInfoHandler)
  164. http.HandleFunc("/openFileWith", websocket.OpenFileWithHandler)
  165. http.HandleFunc("/checkForUpdates", websocket.CheckForUpdatesHandler)
  166. http.HandleFunc("/initClientConfig", websocket.InitClientConfigHandler)
  167. //http.HandleFunc("/watchFile", websocket.WatchFileHandler)
  168. http.HandleFunc("/keeplive", websocket.KeepliveHandler)
  169. http.HandleFunc("/messageNotify", websocket.MessageNotifyHandler)
  170. http.HandleFunc("/messageMarkRead", websocket.MessageMarkReadHandler)
  171. //TODO 消息已读
  172. http.HandleFunc("/queryCommitHistory", websocket.QueryCommitHistoryHandler)
  173. http.HandleFunc("/editCommitHistoryMilestone", websocket.EditCommitHistoryMilestoneHandler)
  174. //服务端启动
  175. log.Println("服务启动成功,监听端口7777,等待连接。")
  176. http.ListenAndServe("0.0.0.0:7777", nil)
  177. }