gui.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "runtime"
  8. "sync"
  9. "time"
  10. "github.com/calmh/syncthing/scanner"
  11. "github.com/codegangsta/martini"
  12. )
  13. type guiError struct {
  14. Time time.Time
  15. Error string
  16. }
  17. var (
  18. configInSync = true
  19. guiErrors = []guiError{}
  20. guiErrorsMut sync.Mutex
  21. )
  22. func startGUI(addr string, m *Model) {
  23. router := martini.NewRouter()
  24. router.Get("/", getRoot)
  25. router.Get("/rest/version", restGetVersion)
  26. router.Get("/rest/model", restGetModel)
  27. router.Get("/rest/connections", restGetConnections)
  28. router.Get("/rest/config", restGetConfig)
  29. router.Get("/rest/config/sync", restGetConfigInSync)
  30. router.Get("/rest/need", restGetNeed)
  31. router.Get("/rest/system", restGetSystem)
  32. router.Get("/rest/errors", restGetErrors)
  33. router.Post("/rest/config", restPostConfig)
  34. router.Post("/rest/restart", restPostRestart)
  35. router.Post("/rest/error", restPostError)
  36. go func() {
  37. mr := martini.New()
  38. mr.Use(embeddedStatic())
  39. mr.Use(martini.Recovery())
  40. mr.Action(router.Handle)
  41. mr.Map(m)
  42. err := http.ListenAndServe(addr, mr)
  43. if err != nil {
  44. warnln("GUI not possible:", err)
  45. }
  46. }()
  47. }
  48. func getRoot(w http.ResponseWriter, r *http.Request) {
  49. http.Redirect(w, r, "/index.html", 302)
  50. }
  51. func restGetVersion() string {
  52. return Version
  53. }
  54. func restGetModel(m *Model, w http.ResponseWriter) {
  55. var res = make(map[string]interface{})
  56. globalFiles, globalDeleted, globalBytes := m.GlobalSize()
  57. res["globalFiles"], res["globalDeleted"], res["globalBytes"] = globalFiles, globalDeleted, globalBytes
  58. localFiles, localDeleted, localBytes := m.LocalSize()
  59. res["localFiles"], res["localDeleted"], res["localBytes"] = localFiles, localDeleted, localBytes
  60. inSyncFiles, inSyncBytes := m.InSyncSize()
  61. res["inSyncFiles"], res["inSyncBytes"] = inSyncFiles, inSyncBytes
  62. files, total := m.NeedFiles()
  63. res["needFiles"], res["needBytes"] = len(files), total
  64. w.Header().Set("Content-Type", "application/json")
  65. json.NewEncoder(w).Encode(res)
  66. }
  67. func restGetConnections(m *Model, w http.ResponseWriter) {
  68. var res = m.ConnectionStats()
  69. w.Header().Set("Content-Type", "application/json")
  70. json.NewEncoder(w).Encode(res)
  71. }
  72. func restGetConfig(w http.ResponseWriter) {
  73. json.NewEncoder(w).Encode(cfg)
  74. }
  75. func restPostConfig(req *http.Request) {
  76. err := json.NewDecoder(req.Body).Decode(&cfg)
  77. if err != nil {
  78. log.Println(err)
  79. } else {
  80. saveConfig()
  81. configInSync = false
  82. }
  83. }
  84. func restGetConfigInSync(w http.ResponseWriter) {
  85. json.NewEncoder(w).Encode(map[string]bool{"configInSync": configInSync})
  86. }
  87. func restPostRestart(req *http.Request) {
  88. restart()
  89. }
  90. type guiFile scanner.File
  91. func (f guiFile) MarshalJSON() ([]byte, error) {
  92. type t struct {
  93. Name string
  94. Size int64
  95. }
  96. return json.Marshal(t{
  97. Name: f.Name,
  98. Size: scanner.File(f).Size,
  99. })
  100. }
  101. func restGetNeed(m *Model, w http.ResponseWriter) {
  102. files, _ := m.NeedFiles()
  103. gfs := make([]guiFile, len(files))
  104. for i, f := range files {
  105. gfs[i] = guiFile(f)
  106. }
  107. w.Header().Set("Content-Type", "application/json")
  108. json.NewEncoder(w).Encode(gfs)
  109. }
  110. var cpuUsagePercent float64
  111. var cpuUsageLock sync.RWMutex
  112. func restGetSystem(w http.ResponseWriter) {
  113. var m runtime.MemStats
  114. runtime.ReadMemStats(&m)
  115. res := make(map[string]interface{})
  116. res["myID"] = myID
  117. res["goroutines"] = runtime.NumGoroutine()
  118. res["alloc"] = m.Alloc
  119. res["sys"] = m.Sys
  120. cpuUsageLock.RLock()
  121. res["cpuPercent"] = cpuUsagePercent
  122. cpuUsageLock.RUnlock()
  123. w.Header().Set("Content-Type", "application/json")
  124. json.NewEncoder(w).Encode(res)
  125. }
  126. func restGetErrors(w http.ResponseWriter) {
  127. guiErrorsMut.Lock()
  128. json.NewEncoder(w).Encode(guiErrors)
  129. guiErrorsMut.Unlock()
  130. }
  131. func restPostError(req *http.Request) {
  132. bs, _ := ioutil.ReadAll(req.Body)
  133. req.Body.Close()
  134. showGuiError(string(bs))
  135. }
  136. func showGuiError(err string) {
  137. guiErrorsMut.Lock()
  138. guiErrors = append(guiErrors, guiError{time.Now(), err})
  139. if len(guiErrors) > 5 {
  140. guiErrors = guiErrors[len(guiErrors)-5:]
  141. }
  142. guiErrorsMut.Unlock()
  143. }