gui.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "mime"
  7. "net/http"
  8. "path/filepath"
  9. "runtime"
  10. "sync"
  11. "time"
  12. "github.com/calmh/syncthing/model"
  13. "github.com/codegangsta/martini"
  14. "github.com/cratonica/embed"
  15. )
  16. func startGUI(addr string, m *model.Model) {
  17. router := martini.NewRouter()
  18. router.Get("/", getRoot)
  19. router.Get("/rest/version", restGetVersion)
  20. router.Get("/rest/model", restGetModel)
  21. router.Get("/rest/connections", restGetConnections)
  22. router.Get("/rest/config", restGetConfig)
  23. router.Get("/rest/need", restGetNeed)
  24. router.Get("/rest/system", restGetSystem)
  25. fs, err := embed.Unpack(Resources)
  26. if err != nil {
  27. panic(err)
  28. }
  29. go func() {
  30. mr := martini.New()
  31. mr.Use(embeddedStatic(fs))
  32. mr.Use(martini.Recovery())
  33. mr.Action(router.Handle)
  34. mr.Map(m)
  35. err := http.ListenAndServe(addr, mr)
  36. if err != nil {
  37. warnln("GUI not possible:", err)
  38. }
  39. }()
  40. }
  41. func getRoot(w http.ResponseWriter, r *http.Request) {
  42. http.Redirect(w, r, "/index.html", 302)
  43. }
  44. func restGetVersion() string {
  45. return Version
  46. }
  47. func restGetModel(m *model.Model, w http.ResponseWriter) {
  48. var res = make(map[string]interface{})
  49. globalFiles, globalDeleted, globalBytes := m.GlobalSize()
  50. res["globalFiles"], res["globalDeleted"], res["globalBytes"] = globalFiles, globalDeleted, globalBytes
  51. localFiles, localDeleted, localBytes := m.LocalSize()
  52. res["localFiles"], res["localDeleted"], res["localBytes"] = localFiles, localDeleted, localBytes
  53. inSyncFiles, inSyncBytes := m.InSyncSize()
  54. res["inSyncFiles"], res["inSyncBytes"] = inSyncFiles, inSyncBytes
  55. files, total := m.NeedFiles()
  56. res["needFiles"], res["needBytes"] = len(files), total
  57. w.Header().Set("Content-Type", "application/json")
  58. json.NewEncoder(w).Encode(res)
  59. }
  60. func restGetConnections(m *model.Model, w http.ResponseWriter) {
  61. var res = m.ConnectionStats()
  62. w.Header().Set("Content-Type", "application/json")
  63. json.NewEncoder(w).Encode(res)
  64. }
  65. func restGetConfig(w http.ResponseWriter) {
  66. var res = make(map[string]interface{})
  67. res["myID"] = myID
  68. res["repository"] = config.OptionMap("repository")
  69. res["nodes"] = config.OptionMap("nodes")
  70. w.Header().Set("Content-Type", "application/json")
  71. json.NewEncoder(w).Encode(res)
  72. }
  73. type guiFile model.File
  74. func (f guiFile) MarshalJSON() ([]byte, error) {
  75. type t struct {
  76. Name string
  77. Size int
  78. }
  79. return json.Marshal(t{
  80. Name: f.Name,
  81. Size: model.File(f).Size(),
  82. })
  83. }
  84. func restGetNeed(m *model.Model, w http.ResponseWriter) {
  85. files, _ := m.NeedFiles()
  86. gfs := make([]guiFile, len(files))
  87. for i, f := range files {
  88. gfs[i] = guiFile(f)
  89. }
  90. w.Header().Set("Content-Type", "application/json")
  91. json.NewEncoder(w).Encode(gfs)
  92. }
  93. var cpuUsagePercent float64
  94. var cpuUsageLock sync.RWMutex
  95. func restGetSystem(w http.ResponseWriter) {
  96. var m runtime.MemStats
  97. runtime.ReadMemStats(&m)
  98. res := make(map[string]interface{})
  99. res["goroutines"] = runtime.NumGoroutine()
  100. res["alloc"] = m.Alloc
  101. res["sys"] = m.Sys
  102. cpuUsageLock.RLock()
  103. res["cpuPercent"] = cpuUsagePercent
  104. cpuUsageLock.RUnlock()
  105. w.Header().Set("Content-Type", "application/json")
  106. json.NewEncoder(w).Encode(res)
  107. }
  108. func embeddedStatic(fs map[string][]byte) interface{} {
  109. var modt = time.Now().UTC().Format(http.TimeFormat)
  110. return func(res http.ResponseWriter, req *http.Request, log *log.Logger) {
  111. file := req.URL.Path
  112. if file[0] == '/' {
  113. file = file[1:]
  114. }
  115. bs, ok := fs[file]
  116. if !ok {
  117. return
  118. }
  119. mtype := mime.TypeByExtension(filepath.Ext(req.URL.Path))
  120. if len(mtype) != 0 {
  121. res.Header().Set("Content-Type", mtype)
  122. }
  123. res.Header().Set("Content-Size", fmt.Sprintf("%d", len(bs)))
  124. res.Header().Set("Last-Modified", modt)
  125. res.Write(bs)
  126. }
  127. }