setting.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. module "imuslab.com/arozos/mod/modules"
  6. "imuslab.com/arozos/mod/utils"
  7. )
  8. type settingModule struct {
  9. Name string //Name of the setting module.
  10. Desc string //Description of the setting module
  11. IconPath string //Icon path for the setting module
  12. Group string //Accept {}
  13. StartDir string //Startup Directory / path
  14. RequireAdmin bool //If the setting require admin access.
  15. //^ Enable this to hide this setting from non-admin users, but for API call, module has to handle admin check themselves.
  16. }
  17. type settingGroup struct {
  18. Name string
  19. Group string
  20. IconPath string
  21. Desc string
  22. }
  23. var (
  24. settingModules []settingModule
  25. )
  26. func SystemSettingInit() {
  27. http.HandleFunc("/system/setting/list", system_setting_handleListing)
  28. //Register the module
  29. moduleHandler.RegisterModule(module.ModuleInfo{
  30. Name: "System Setting",
  31. Desc: "Cutomize your systems to fit your needs",
  32. Group: "System Settings",
  33. IconPath: "SystemAO/system_setting/img/small_icon.png",
  34. Version: "1.0",
  35. StartDir: "SystemAO/system_setting/index.html",
  36. SupportFW: true,
  37. InitFWSize: []int{1200, 580},
  38. LaunchFWDir: "SystemAO/system_setting/index.html",
  39. SupportEmb: false,
  40. })
  41. }
  42. // Setting group defination. Your setting module defination must match the group in-order to be shown
  43. func system_setting_getSettingGroups() []settingGroup {
  44. groups := []settingGroup{
  45. {
  46. Name: "Host Information",
  47. Group: "Info",
  48. IconPath: "SystemAO/system_setting/img/server.svg",
  49. Desc: "Config and info about the Server Host",
  50. },
  51. {
  52. Name: "Devices & IoT",
  53. Group: "Device",
  54. IconPath: "SystemAO/system_setting/img/device.svg",
  55. Desc: "Connected clients and IoT devices",
  56. },
  57. {
  58. Name: "Module Management",
  59. Group: "Module",
  60. IconPath: "SystemAO/system_setting/img/module.svg",
  61. Desc: "List of modules loaded in the system",
  62. },
  63. {
  64. Name: "Desktop & Themes",
  65. Group: "Desktop",
  66. IconPath: "SystemAO/desktop/img/personalization.png",
  67. Desc: "Personalize your desktop experience",
  68. },
  69. {
  70. Name: "Disk & Storage",
  71. Group: "Disk",
  72. IconPath: "SystemAO/system_setting/img/drive.svg",
  73. Desc: "Manage Storage Devices and Disks",
  74. },
  75. {
  76. Name: "Network & Connection",
  77. Group: "Network",
  78. IconPath: "SystemAO/system_setting/img/network.svg",
  79. Desc: "Manage Host Network and Connections",
  80. },
  81. {
  82. Name: "Users & Groups",
  83. Group: "Users",
  84. IconPath: "SystemAO/system_setting/img/users.svg",
  85. Desc: "Add, removed or edit users and groups",
  86. },
  87. {
  88. Name: "Clusters & Scheduling",
  89. Group: "Cluster",
  90. IconPath: "SystemAO/system_setting/img/cluster.svg",
  91. Desc: "Cluster, Network Scanning and Task Scheduling",
  92. },
  93. {
  94. Name: "Security & Auth",
  95. Group: "Security",
  96. IconPath: "SystemAO/system_setting/img/security.svg",
  97. Desc: "System Security and Auth Credentials",
  98. },
  99. {
  100. Name: "Developer Options",
  101. Group: "Advance",
  102. IconPath: "SystemAO/system_setting/img/code.svg",
  103. Desc: "Advance configs for developers",
  104. },
  105. {
  106. Name: "AI Integration",
  107. Group: "AInteg",
  108. IconPath: "SystemAO/system_setting/img/ai.svg",
  109. Desc: "Connect AI models, manage pricing, quota and usage",
  110. },
  111. {
  112. Name: "About ArOZ",
  113. Group: "About",
  114. IconPath: "SystemAO/system_setting/img/info.svg",
  115. Desc: "Information of the current running ArOZ Online System",
  116. },
  117. }
  118. //The "Containers" group only has content when Docker management is active
  119. //(dockerManager is non-nil). Showing it otherwise would render an empty
  120. //group whose listing returns no modules and breaks the settings UI.
  121. if dockerManager != nil {
  122. containerGroup := settingGroup{
  123. Name: "Containers",
  124. Group: "Container",
  125. IconPath: "SystemAO/system_setting/img/docker.svg",
  126. Desc: "Manage Docker Engine and container runtime",
  127. }
  128. //Insert just before the "About" group so it sits after AI Integration
  129. //rather than at the very bottom of the settings list.
  130. inserted := false
  131. for i, g := range groups {
  132. if g.Group == "About" {
  133. groups = append(groups[:i], append([]settingGroup{containerGroup}, groups[i:]...)...)
  134. inserted = true
  135. break
  136. }
  137. }
  138. if !inserted {
  139. groups = append(groups, containerGroup)
  140. }
  141. }
  142. return groups
  143. }
  144. func registerSetting(thismodule settingModule) {
  145. settingModules = append(settingModules, thismodule)
  146. }
  147. // List all the setting modules and output it as JSON
  148. func system_setting_handleListing(w http.ResponseWriter, r *http.Request) {
  149. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  150. if err != nil {
  151. utils.SendErrorResponse(w, "User not logged in")
  152. return
  153. }
  154. allSettingGroups := system_setting_getSettingGroups()
  155. listGroup, _ := utils.GetPara(r, "listGroup")
  156. if len(listGroup) > 0 {
  157. //List the given group
  158. var results []settingModule
  159. for _, setMod := range settingModules {
  160. if setMod.Group == listGroup {
  161. //Check if the module is admin only.
  162. if setMod.RequireAdmin && userinfo.IsAdmin() {
  163. //Admin module and user is admin. Append to list
  164. results = append(results, setMod)
  165. } else if setMod.RequireAdmin == false {
  166. //Normal module. Append to list
  167. results = append(results, setMod)
  168. }
  169. }
  170. }
  171. //Always return a JSON array (possibly empty). The front-end iterates the
  172. //response with forEach, so returning an error object here would throw a
  173. //"forEach is not a function" in the settings UI for any empty group.
  174. if results == nil {
  175. results = []settingModule{}
  176. }
  177. jsonString, _ := json.Marshal(results)
  178. utils.SendJSONResponse(w, string(jsonString))
  179. return
  180. } else {
  181. //List all root groups
  182. jsonString, _ := json.Marshal(allSettingGroups)
  183. utils.SendJSONResponse(w, string(jsonString))
  184. return
  185. }
  186. }