文件同步
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

main.go 5.7 KiB

4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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("/queryCommitHistory", websocket.QueryCommitHistoryHandler)
  171. http.HandleFunc("/editCommitHistoryMilestone", websocket.EditCommitHistoryMilestoneHandler)
  172. //服务端启动
  173. log.Println("服务启动成功,监听端口7777,等待连接。")
  174. http.ListenAndServe("0.0.0.0:7777", nil)
  175. }