gui.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/codegangsta/martini"
  12. )
  13. func startGUI(addr string, m *Model) {
  14. router := martini.NewRouter()
  15. router.Get("/", getRoot)
  16. router.Get("/rest/version", restGetVersion)
  17. router.Get("/rest/model", restGetModel)
  18. router.Get("/rest/connections", restGetConnections)
  19. router.Get("/rest/config", restGetConfig)
  20. router.Get("/rest/need", restGetNeed)
  21. go func() {
  22. mr := martini.New()
  23. mr.Use(nrscStatic("gui"))
  24. mr.Use(martini.Recovery())
  25. mr.Action(router.Handle)
  26. mr.Map(m)
  27. http.ListenAndServe(addr, mr)
  28. }()
  29. }
  30. func getRoot(w http.ResponseWriter, r *http.Request) {
  31. http.Redirect(w, r, "/index.html", 302)
  32. }
  33. func restGetVersion() string {
  34. return Version
  35. }
  36. func restGetModel(m *Model, w http.ResponseWriter) {
  37. var res = make(map[string]interface{})
  38. globalFiles, globalDeleted, globalBytes := m.GlobalSize()
  39. res["globalFiles"], res["globalDeleted"], res["globalBytes"] = globalFiles, globalDeleted, globalBytes
  40. localFiles, localDeleted, localBytes := m.LocalSize()
  41. res["localFiles"], res["localDeleted"], res["localBytes"] = localFiles, localDeleted, localBytes
  42. inSyncFiles, inSyncBytes := m.InSyncSize()
  43. res["inSyncFiles"], res["inSyncBytes"] = inSyncFiles, inSyncBytes
  44. files, total := m.NeedFiles()
  45. res["needFiles"], res["needBytes"] = len(files), total
  46. w.Header().Set("Content-Type", "application/json")
  47. json.NewEncoder(w).Encode(res)
  48. }
  49. func restGetConnections(m *Model, w http.ResponseWriter) {
  50. var res = m.ConnectionStats()
  51. w.Header().Set("Content-Type", "application/json")
  52. json.NewEncoder(w).Encode(res)
  53. }
  54. func restGetConfig(w http.ResponseWriter) {
  55. var res = make(map[string]interface{})
  56. res["repository"] = config.OptionMap("repository")
  57. res["nodes"] = config.OptionMap("nodes")
  58. w.Header().Set("Content-Type", "application/json")
  59. json.NewEncoder(w).Encode(res)
  60. }
  61. func restGetNeed(m *Model, w http.ResponseWriter) {
  62. files, _ := m.NeedFiles()
  63. if files == nil {
  64. // We don't want the empty list to serialize as "null\n"
  65. files = make([]FileInfo, 0)
  66. }
  67. w.Header().Set("Content-Type", "application/json")
  68. json.NewEncoder(w).Encode(files)
  69. }
  70. func nrscStatic(path string) interface{} {
  71. if err := nrsc.Initialize(); err != nil {
  72. panic("Unable to initialize nrsc: " + err.Error())
  73. }
  74. return func(res http.ResponseWriter, req *http.Request, log *log.Logger) {
  75. file := req.URL.Path
  76. // nrsc expects there not to be a leading slash
  77. if file[0] == '/' {
  78. file = file[1:]
  79. }
  80. f := nrsc.Get(file)
  81. if f == nil {
  82. return
  83. }
  84. rdr, err := f.Open()
  85. if err != nil {
  86. http.Error(res, "Internal Server Error", http.StatusInternalServerError)
  87. }
  88. defer rdr.Close()
  89. mtype := mime.TypeByExtension(filepath.Ext(req.URL.Path))
  90. if len(mtype) != 0 {
  91. res.Header().Set("Content-Type", mtype)
  92. }
  93. res.Header().Set("Content-Size", fmt.Sprintf("%d", f.Size()))
  94. res.Header().Set("Last-Modified", f.ModTime().UTC().Format(http.TimeFormat))
  95. io.Copy(res, rdr)
  96. }
  97. }