main.go 4.0 KB

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