gui.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. http.ListenAndServe(addr, mr)
  29. }()
  30. }
  31. func getRoot(w http.ResponseWriter, r *http.Request) {
  32. http.Redirect(w, r, "/index.html", 302)
  33. }
  34. func restGetVersion() string {
  35. return Version
  36. }
  37. func restGetModel(m *model.Model, w http.ResponseWriter) {
  38. var res = make(map[string]interface{})
  39. globalFiles, globalDeleted, globalBytes := m.GlobalSize()
  40. res["globalFiles"], res["globalDeleted"], res["globalBytes"] = globalFiles, globalDeleted, globalBytes
  41. localFiles, localDeleted, localBytes := m.LocalSize()
  42. res["localFiles"], res["localDeleted"], res["localBytes"] = localFiles, localDeleted, localBytes
  43. inSyncFiles, inSyncBytes := m.InSyncSize()
  44. res["inSyncFiles"], res["inSyncBytes"] = inSyncFiles, inSyncBytes
  45. files, total := m.NeedFiles()
  46. res["needFiles"], res["needBytes"] = len(files), total
  47. w.Header().Set("Content-Type", "application/json")
  48. json.NewEncoder(w).Encode(res)
  49. }
  50. func restGetConnections(m *model.Model, w http.ResponseWriter) {
  51. var res = m.ConnectionStats()
  52. w.Header().Set("Content-Type", "application/json")
  53. json.NewEncoder(w).Encode(res)
  54. }
  55. func restGetConfig(w http.ResponseWriter) {
  56. var res = make(map[string]interface{})
  57. res["repository"] = config.OptionMap("repository")
  58. res["nodes"] = config.OptionMap("nodes")
  59. w.Header().Set("Content-Type", "application/json")
  60. json.NewEncoder(w).Encode(res)
  61. }
  62. type guiFile model.File
  63. func (f guiFile) MarshalJSON() ([]byte, error) {
  64. type t struct {
  65. Name string
  66. Size int
  67. }
  68. return json.Marshal(t{
  69. Name: f.Name,
  70. Size: model.File(f).Size(),
  71. })
  72. }
  73. func restGetNeed(m *model.Model, w http.ResponseWriter) {
  74. files, _ := m.NeedFiles()
  75. gfs := make([]guiFile, len(files))
  76. for i, f := range files {
  77. gfs[i] = guiFile(f)
  78. }
  79. w.Header().Set("Content-Type", "application/json")
  80. json.NewEncoder(w).Encode(gfs)
  81. }
  82. func nrscStatic(path string) interface{} {
  83. if err := nrsc.Initialize(); err != nil {
  84. panic("Unable to initialize nrsc: " + err.Error())
  85. }
  86. return func(res http.ResponseWriter, req *http.Request, log *log.Logger) {
  87. file := req.URL.Path
  88. // nrsc expects there not to be a leading slash
  89. if file[0] == '/' {
  90. file = file[1:]
  91. }
  92. f := nrsc.Get(file)
  93. if f == nil {
  94. return
  95. }
  96. rdr, err := f.Open()
  97. if err != nil {
  98. http.Error(res, "Internal Server Error", http.StatusInternalServerError)
  99. }
  100. defer rdr.Close()
  101. mtype := mime.TypeByExtension(filepath.Ext(req.URL.Path))
  102. if len(mtype) != 0 {
  103. res.Header().Set("Content-Type", mtype)
  104. }
  105. res.Header().Set("Content-Size", fmt.Sprintf("%d", f.Size()))
  106. res.Header().Set("Last-Modified", f.ModTime().UTC().Format(http.TimeFormat))
  107. io.Copy(res, rdr)
  108. }
  109. }