main.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "os/signal"
  9. "path/filepath"
  10. "strconv"
  11. "syscall"
  12. "time"
  13. console "imuslab.com/arozos/mod/console"
  14. )
  15. /*
  16. arozos
  17. author: tobychui
  18. To edit startup flags, see main.flag.go
  19. To edit main routing logic, see main.router.go
  20. To edit startup sequence, see startup.go
  21. P.S. Try to keep this file < 300 lines
  22. */
  23. //Close handler, close db and clearn up everything before exit
  24. func SetupCloseHandler() {
  25. c := make(chan os.Signal, 2)
  26. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  27. go func() {
  28. <-c
  29. executeShutdownSequence()
  30. }()
  31. }
  32. func executeShutdownSequence() {
  33. //Shutdown authAgent
  34. systemWideLogger.PrintAndLog("System", "<!> Shutting down auth gateway", nil)
  35. authAgent.Close()
  36. //Shutdown all storage pools
  37. systemWideLogger.PrintAndLog("System", "<!> Shutting down storage pools", nil)
  38. closeAllStoragePools()
  39. //Shutdown Logger
  40. systemWideLogger.Close()
  41. //Shutdown database
  42. systemWideLogger.PrintAndLog("System", "<!> Shutting down database", nil)
  43. sysdb.Close()
  44. //Shutdown network services
  45. StopNetworkServices()
  46. //Shutdown FTP Server
  47. if FTPManager != nil {
  48. systemWideLogger.PrintAndLog("System", "<!> Shutting down FTP Server", nil)
  49. FTPManager.StopFtpServer()
  50. }
  51. //Cleaning up tmp files
  52. systemWideLogger.PrintAndLog("System", "<!> Cleaning up tmp folder", nil)
  53. os.RemoveAll(*tmp_directory)
  54. //Do other things
  55. os.Exit(0)
  56. }
  57. func main() {
  58. //Parse startup flags and paramters
  59. flag.Parse()
  60. //Handle version printing
  61. if *show_version {
  62. fmt.Println("ArozOS " + build_version + " Revision " + internal_version)
  63. fmt.Println("Developed by tobychui and other co-developers, Licensed to " + deviceVendor)
  64. //fmt.Println("THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.")
  65. os.Exit(0)
  66. }
  67. //Handle flag assignments
  68. max_upload_size = int64(*max_upload) << 20 //Parse the max upload size
  69. if *demo_mode { //Disable hardware man under demo mode
  70. enablehw := false
  71. allow_hardware_management = &enablehw
  72. }
  73. //Clean up previous tmp files
  74. final_tmp_directory := filepath.Clean(*tmp_directory) + "/tmp/"
  75. tmp_directory = &final_tmp_directory
  76. os.RemoveAll(*tmp_directory)
  77. os.Mkdir(*tmp_directory, 0777)
  78. //Print copyRight information
  79. log.Println("ArozOS(C) " + strconv.Itoa(time.Now().Year()) + " " + deviceVendor + ".")
  80. log.Println("ArozOS " + build_version + " Revision " + internal_version)
  81. /*
  82. New Implementation of the ArOZ Online System, Sept 2020
  83. */
  84. RunStartup()
  85. /*
  86. Development build test execution
  87. */
  88. Run_Test()
  89. //Initiate all the static files transfer
  90. fs := http.FileServer(http.Dir("./web"))
  91. /*
  92. if *enable_gzip {
  93. //Gzip enabled. Always serve with gzip if header exists
  94. //http.Handle("/", gzipmiddleware.Compress(mrouter(fs)))
  95. http.Handle("/", mrouter(fs))
  96. } else {
  97. //Normal file server without gzip
  98. http.Handle("/", mrouter(fs))
  99. }
  100. */
  101. //Updates 2022-09-06: Gzip handler moved inside the master router
  102. http.Handle("/", mrouter(fs))
  103. //Set database read write to ReadOnly after startup if demo mode
  104. if *demo_mode {
  105. sysdb.UpdateReadWriteMode(true)
  106. }
  107. //Setup handler for Ctrl +C
  108. SetupCloseHandler()
  109. //Start http server
  110. go func() {
  111. if *use_tls {
  112. if !*disable_http {
  113. go func() {
  114. log.Println("Standard (HTTP) Web server listening at :" + strconv.Itoa(*listen_port))
  115. http.ListenAndServe(":"+strconv.Itoa(*listen_port), nil)
  116. }()
  117. }
  118. log.Println("Secure (HTTPS) Web server listening at :" + strconv.Itoa(*tls_listen_port))
  119. http.ListenAndServeTLS(":"+strconv.Itoa(*tls_listen_port), *tls_cert, *tls_key, nil)
  120. } else {
  121. log.Println("Web server listening at :" + strconv.Itoa(*listen_port))
  122. http.ListenAndServe(":"+strconv.Itoa(*listen_port), nil)
  123. }
  124. }()
  125. if *enable_console == true {
  126. //Startup interactive shell for debug and basic controls
  127. Console := console.NewConsole(consoleCommandHandler)
  128. Console.ListenAndHandle()
  129. } else {
  130. //Just do a blocking loop here
  131. select {}
  132. }
  133. }