gui.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "log"
  7. "mime"
  8. "net/http"
  9. "path/filepath"
  10. "bitbucket.org/tebeka/nrsc"
  11. "github.com/calmh/syncthing/model"
  12. "github.com/codegangsta/martini"
  13. )
  14. func startGUI(addr string, m *model.Model) {
  15. router := martini.NewRouter()
  16. router.Get("/", getRoot)
  17. router.Get("/rest/version", restGetVersion)
  18. router.Get("/rest/model", restGetModel)
  19. router.Get("/rest/connections", restGetConnections)
  20. router.Get("/rest/config", restGetConfig)
  21. router.Get("/rest/need", restGetNeed)
  22. go func() {
  23. mr := martini.New()
  24. mr.Use(nrscStatic("gui"))
  25. mr.Use(martini.Recovery())
  26. mr.Action(router.Handle)
  27. mr.Map(m)
  28. err := http.ListenAndServe(addr, mr)
  29. if err != nil {
  30. warnln("GUI not possible:", err)
  31. }
  32. }()
  33. }
  34. func getRoot(w http.ResponseWriter, r *http.Request) {
  35. http.Redirect(w, r, "/index.html", 302)
  36. }
  37. func restGetVersion() string {
  38. return Version
  39. }
  40. func restGetModel(m *model.Model, w http.ResponseWriter) {
  41. var res = make(map[string]interface{})
  42. globalFiles, globalDeleted, globalBytes := m.GlobalSize()
  43. res["globalFiles"], res["globalDeleted"], res["globalBytes"] = globalFiles, globalDeleted, globalBytes
  44. localFiles, localDeleted, localBytes := m.LocalSize()
  45. res["localFiles"], res["localDeleted"], res["localBytes"] = localFiles, localDeleted, localBytes
  46. inSyncFiles, inSyncBytes := m.InSyncSize()
  47. res["inSyncFiles"], res["inSyncBytes"] = inSyncFiles, inSyncBytes
  48. files, total := m.NeedFiles()
  49. res["needFiles"], res["needBytes"] = len(files), total
  50. w.Header().Set("Content-Type", "application/json")
  51. json.NewEncoder(w).Encode(res)
  52. }
  53. func restGetConnections(m *model.Model, w http.ResponseWriter) {
  54. var res = m.ConnectionStats()
  55. w.Header().Set("Content-Type", "application/json")
  56. json.NewEncoder(w).Encode(res)
  57. }
  58. func restGetConfig(w http.ResponseWriter) {
  59. var res = make(map[string]interface{})
  60. res["repository"] = config.OptionMap("repository")
  61. res["nodes"] = config.OptionMap("nodes")
  62. w.Header().Set("Content-Type", "application/json")
  63. json.NewEncoder(w).Encode(res)
  64. }
  65. type guiFile model.File
  66. func (f guiFile) MarshalJSON() ([]byte, error) {
  67. type t struct {
  68. Name string
  69. Size int
  70. }
  71. return json.Marshal(t{
  72. Name: f.Name,
  73. Size: model.File(f).Size(),
  74. })
  75. }
  76. func restGetNeed(m *model.Model, w http.ResponseWriter) {
  77. files, _ := m.NeedFiles()
  78. gfs := make([]guiFile, len(files))
  79. for i, f := range files {
  80. gfs[i] = guiFile(f)
  81. }
  82. w.Header().Set("Content-Type", "application/json")
  83. json.NewEncoder(w).Encode(gfs)
  84. }
  85. func nrscStatic(path string) interface{} {
  86. if err := nrsc.Initialize(); err != nil {
  87. panic("Unable to initialize nrsc: " + err.Error())
  88. }
  89. return func(res http.ResponseWriter, req *http.Request, log *log.Logger) {
  90. file := req.URL.Path
  91. // nrsc expects there not to be a leading slash
  92. if file[0] == '/' {
  93. file = file[1:]
  94. }
  95. f := nrsc.Get(file)
  96. if f == nil {
  97. return
  98. }
  99. rdr, err := f.Open()
  100. if err != nil {
  101. http.Error(res, "Internal Server Error", http.StatusInternalServerError)
  102. }
  103. defer rdr.Close()
  104. mtype := mime.TypeByExtension(filepath.Ext(req.URL.Path))
  105. if len(mtype) != 0 {
  106. res.Header().Set("Content-Type", mtype)
  107. }
  108. res.Header().Set("Content-Size", fmt.Sprintf("%d", f.Size()))
  109. res.Header().Set("Last-Modified", f.ModTime().UTC().Format(http.TimeFormat))
  110. io.Copy(res, rdr)
  111. }
  112. }