startup.flags.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. prout "imuslab.com/arozos/mod/prouter"
  7. )
  8. /*
  9. Startup Flags Manager
  10. This script is design to provide interface for editing the boot flags
  11. during the system is running
  12. */
  13. func StartupFlagsInit() {
  14. //Create a admin permission router for handling requests
  15. //Register a boot flag modifier
  16. registerSetting(settingModule{
  17. Name: "Runtime",
  18. Desc: "Change startup paramter in runtime",
  19. IconPath: "SystemAO/info/img/small_icon.png",
  20. Group: "Info",
  21. StartDir: "SystemAO/boot/bootflags.html",
  22. RequireAdmin: true,
  23. })
  24. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  25. AdminOnly: true,
  26. UserHandler: userHandler,
  27. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  28. sendErrorResponse(w, "Permission Denied")
  29. },
  30. })
  31. //Register a power handler in system setting menu
  32. registerSetting(settingModule{
  33. Name: "Power",
  34. Desc: "Set the power state of the host device",
  35. IconPath: "SystemAO/boot/img/boot.png",
  36. Group: "Info",
  37. StartDir: "SystemAO/boot/poweroff.html",
  38. RequireAdmin: true,
  39. })
  40. adminRouter.HandleFunc("/system/bootflags", handleBootFlagsFunction)
  41. }
  42. func handleBootFlagsFunction(w http.ResponseWriter, r *http.Request) {
  43. type bootFlags struct {
  44. Hostname string
  45. MaxUploadSize int
  46. MaxFileUploadBuff int
  47. FileIOBuffer int
  48. DisableIPResolver bool
  49. EnableHomePage bool
  50. EnableDirListing bool
  51. }
  52. opr, _ := mv(r, "opr", true)
  53. if opr == "" {
  54. //List the current boot flag, all units in MB
  55. js, _ := json.Marshal(bootFlags{
  56. *host_name,
  57. int(max_upload_size >> 20),
  58. *upload_buf,
  59. *file_opr_buff,
  60. *disable_ip_resolve_services,
  61. *allow_homepage,
  62. *enable_dir_listing,
  63. })
  64. sendJSONResponse(w, string(js))
  65. } else if opr == "set" {
  66. //Set and update the boot flags
  67. newSettings, err := mv(r, "value", true)
  68. if err != nil {
  69. sendErrorResponse(w, "Invalid new seting value")
  70. return
  71. }
  72. //Try parse it
  73. newConfig := bootFlags{
  74. "My ArOZ",
  75. 8192,
  76. 25,
  77. 1024,
  78. false,
  79. false,
  80. true,
  81. }
  82. err = json.Unmarshal([]byte(newSettings), &newConfig)
  83. if err != nil {
  84. sendErrorResponse(w, err.Error())
  85. return
  86. }
  87. //Update the current global flags
  88. log.Println("Updating boot flag to:", newSettings)
  89. *host_name = newConfig.Hostname
  90. max_upload_size = int64(newConfig.MaxUploadSize << 20)
  91. *upload_buf = newConfig.MaxFileUploadBuff
  92. *file_opr_buff = newConfig.FileIOBuffer
  93. *disable_ip_resolve_services = newConfig.DisableIPResolver
  94. *allow_homepage = newConfig.EnableHomePage
  95. *enable_dir_listing = newConfig.EnableDirListing
  96. sendOK(w)
  97. } else {
  98. sendErrorResponse(w, "Unknown operation")
  99. }
  100. }