A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

157 Zeilen
4.7 KiB

  1. package writefreely
  2. import (
  3. "fmt"
  4. "github.com/gogits/gogs/pkg/tool"
  5. "github.com/gorilla/mux"
  6. "github.com/writeas/impart"
  7. "github.com/writeas/web-core/auth"
  8. "net/http"
  9. "runtime"
  10. "time"
  11. )
  12. var (
  13. appStartTime = time.Now()
  14. sysStatus systemStatus
  15. )
  16. type systemStatus struct {
  17. Uptime string
  18. NumGoroutine int
  19. // General statistics.
  20. MemAllocated string // bytes allocated and still in use
  21. MemTotal string // bytes allocated (even if freed)
  22. MemSys string // bytes obtained from system (sum of XxxSys below)
  23. Lookups uint64 // number of pointer lookups
  24. MemMallocs uint64 // number of mallocs
  25. MemFrees uint64 // number of frees
  26. // Main allocation heap statistics.
  27. HeapAlloc string // bytes allocated and still in use
  28. HeapSys string // bytes obtained from system
  29. HeapIdle string // bytes in idle spans
  30. HeapInuse string // bytes in non-idle span
  31. HeapReleased string // bytes released to the OS
  32. HeapObjects uint64 // total number of allocated objects
  33. // Low-level fixed-size structure allocator statistics.
  34. // Inuse is bytes used now.
  35. // Sys is bytes obtained from system.
  36. StackInuse string // bootstrap stacks
  37. StackSys string
  38. MSpanInuse string // mspan structures
  39. MSpanSys string
  40. MCacheInuse string // mcache structures
  41. MCacheSys string
  42. BuckHashSys string // profiling bucket hash table
  43. GCSys string // GC metadata
  44. OtherSys string // other system allocations
  45. // Garbage collector statistics.
  46. NextGC string // next run in HeapAlloc time (bytes)
  47. LastGC string // last run in absolute time (ns)
  48. PauseTotalNs string
  49. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  50. NumGC uint32
  51. }
  52. func handleViewAdminDash(app *app, u *User, w http.ResponseWriter, r *http.Request) error {
  53. updateAppStats()
  54. p := struct {
  55. *UserPage
  56. Message string
  57. SysStatus systemStatus
  58. AboutPage, PrivacyPage string
  59. }{
  60. UserPage: NewUserPage(app, r, u, "Admin", nil),
  61. Message: r.FormValue("m"),
  62. SysStatus: sysStatus,
  63. }
  64. var err error
  65. p.AboutPage, err = getAboutPage(app)
  66. if err != nil {
  67. return err
  68. }
  69. p.PrivacyPage, _, err = getPrivacyPage(app)
  70. if err != nil {
  71. return err
  72. }
  73. showUserPage(w, "admin", p)
  74. return nil
  75. }
  76. func handleAdminUpdateSite(app *app, u *User, w http.ResponseWriter, r *http.Request) error {
  77. vars := mux.Vars(r)
  78. id := vars["page"]
  79. // Validate
  80. if id != "about" && id != "privacy" {
  81. return impart.HTTPError{http.StatusNotFound, "No such page."}
  82. }
  83. // Update page
  84. m := ""
  85. err := app.db.UpdateDynamicContent(id, r.FormValue("content"))
  86. if err != nil {
  87. m = "?m=" + err.Error()
  88. }
  89. return impart.HTTPError{http.StatusFound, "/admin" + m + "#page-" + id}
  90. }
  91. func updateAppStats() {
  92. sysStatus.Uptime = tool.TimeSincePro(appStartTime)
  93. m := new(runtime.MemStats)
  94. runtime.ReadMemStats(m)
  95. sysStatus.NumGoroutine = runtime.NumGoroutine()
  96. sysStatus.MemAllocated = tool.FileSize(int64(m.Alloc))
  97. sysStatus.MemTotal = tool.FileSize(int64(m.TotalAlloc))
  98. sysStatus.MemSys = tool.FileSize(int64(m.Sys))
  99. sysStatus.Lookups = m.Lookups
  100. sysStatus.MemMallocs = m.Mallocs
  101. sysStatus.MemFrees = m.Frees
  102. sysStatus.HeapAlloc = tool.FileSize(int64(m.HeapAlloc))
  103. sysStatus.HeapSys = tool.FileSize(int64(m.HeapSys))
  104. sysStatus.HeapIdle = tool.FileSize(int64(m.HeapIdle))
  105. sysStatus.HeapInuse = tool.FileSize(int64(m.HeapInuse))
  106. sysStatus.HeapReleased = tool.FileSize(int64(m.HeapReleased))
  107. sysStatus.HeapObjects = m.HeapObjects
  108. sysStatus.StackInuse = tool.FileSize(int64(m.StackInuse))
  109. sysStatus.StackSys = tool.FileSize(int64(m.StackSys))
  110. sysStatus.MSpanInuse = tool.FileSize(int64(m.MSpanInuse))
  111. sysStatus.MSpanSys = tool.FileSize(int64(m.MSpanSys))
  112. sysStatus.MCacheInuse = tool.FileSize(int64(m.MCacheInuse))
  113. sysStatus.MCacheSys = tool.FileSize(int64(m.MCacheSys))
  114. sysStatus.BuckHashSys = tool.FileSize(int64(m.BuckHashSys))
  115. sysStatus.GCSys = tool.FileSize(int64(m.GCSys))
  116. sysStatus.OtherSys = tool.FileSize(int64(m.OtherSys))
  117. sysStatus.NextGC = tool.FileSize(int64(m.NextGC))
  118. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  119. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  120. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  121. sysStatus.NumGC = m.NumGC
  122. }
  123. func adminResetPassword(app *app, u *User, newPass string) error {
  124. hashedPass, err := auth.HashPass([]byte(newPass))
  125. if err != nil {
  126. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not create password hash: %v", err)}
  127. }
  128. err = app.db.ChangePassphrase(u.ID, true, "", hashedPass)
  129. if err != nil {
  130. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not update passphrase: %v", err)}
  131. }
  132. return nil
  133. }