main.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. "imuslab.com/arozos/mod/network/gzipmiddleware"
  15. )
  16. /*
  17. arozos
  18. author: tobychui
  19. To edit startup flags, see main.flag.go
  20. To edit main routing logic, see main.router.go
  21. To edit startup sequence, see startup.go
  22. P.S. Try to keep this file < 300 lines
  23. */
  24. //Close handler, close db and clearn up everything before exit
  25. func SetupCloseHandler() {
  26. c := make(chan os.Signal, 2)
  27. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  28. go func() {
  29. <-c
  30. executeShutdownSequence()
  31. }()
  32. }
  33. func executeShutdownSequence() {
  34. //Shutdown authAgent
  35. log.Println("\r- Shutting down auth gateway")
  36. authAgent.Close()
  37. //Shutdown all storage pools
  38. log.Println("\r- Shutting down storage pools")
  39. closeAllStoragePools()
  40. //Shutdown Subservices
  41. log.Println("\r- Shutting down background subservices")
  42. //system_subservice_handleShutdown()
  43. //Shutdown database
  44. log.Println("\r- Shutting down database")
  45. sysdb.Close()
  46. //Shutdown network services
  47. StopNetworkServices()
  48. //Shutdown FTP Server
  49. if ftpServer != nil {
  50. log.Println("\r- Shutting down FTP Server")
  51. ftpServer.Close()
  52. }
  53. //Cleaning up tmp files
  54. log.Println("\r- Cleaning up tmp folder")
  55. os.RemoveAll(*tmp_directory)
  56. //Do other things
  57. os.Exit(0)
  58. }
  59. func main() {
  60. //Parse startup flags and paramters
  61. flag.Parse()
  62. //Handle version printing
  63. if *show_version {
  64. fmt.Println("ArozOS " + build_version + " Revision " + internal_version)
  65. fmt.Println("Developed by tobychui and other co-developers, Licensed to " + deviceVendor)
  66. //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.")
  67. os.Exit(0)
  68. }
  69. //Handle flag assignments
  70. max_upload_size = int64(*max_upload) << 20 //Parse the max upload size
  71. if *demo_mode { //Disable hardware man under demo mode
  72. enablehw := false
  73. allow_hardware_management = &enablehw
  74. }
  75. //Setup handler for Ctrl +C
  76. SetupCloseHandler()
  77. //Clean up previous tmp files
  78. final_tmp_directory := filepath.Clean(*tmp_directory) + "/tmp/"
  79. tmp_directory = &final_tmp_directory
  80. os.RemoveAll(*tmp_directory)
  81. os.Mkdir(*tmp_directory, 0777)
  82. //Print copyRight information
  83. log.Println("ArozOS(C) " + strconv.Itoa(time.Now().Year()) + " " + deviceVendor + ".")
  84. log.Println("ArozOS " + build_version + " Revision " + internal_version)
  85. /*
  86. New Implementation of the ArOZ Online System, Sept 2020
  87. */
  88. RunStartup()
  89. /*
  90. Development build test execution
  91. */
  92. Run_Test()
  93. //Initiate all the static files transfer
  94. fs := http.FileServer(http.Dir("./web"))
  95. if *enable_gzip {
  96. //Gzip enabled. Always serve with gzip if header exists
  97. http.Handle("/", gzipmiddleware.Compress(mrouter(fs)))
  98. } else {
  99. //Normal file server without gzip
  100. http.Handle("/", mrouter(fs))
  101. }
  102. //Set database read write to ReadOnly after startup if demo mode
  103. if *demo_mode {
  104. sysdb.UpdateReadWriteMode(true)
  105. }
  106. //Start http server
  107. go func() {
  108. if *use_tls {
  109. if !*disable_http {
  110. go func() {
  111. log.Println("Standard (HTTP) Web server listening at :" + strconv.Itoa(*listen_port))
  112. http.ListenAndServe(":"+strconv.Itoa(*listen_port), nil)
  113. }()
  114. }
  115. log.Println("Secure (HTTPS) Web server listening at :" + strconv.Itoa(*tls_listen_port))
  116. http.ListenAndServeTLS(":"+strconv.Itoa(*tls_listen_port), *tls_cert, *tls_key, nil)
  117. } else {
  118. log.Println("Web server listening at :" + strconv.Itoa(*listen_port))
  119. http.ListenAndServe(":"+strconv.Itoa(*listen_port), nil)
  120. }
  121. }()
  122. if *enable_console == true {
  123. //Startup interactive shell for debug and basic controls
  124. Console := console.NewConsole(consoleCommandHandler)
  125. Console.ListenAndHandle()
  126. } else {
  127. //Just do a blocking loop here
  128. select {}
  129. }
  130. }