main.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "os/signal"
  8. "path/filepath"
  9. "strconv"
  10. "syscall"
  11. "time"
  12. console "imuslab.com/arozos/mod/console"
  13. fs "imuslab.com/arozos/mod/filesystem"
  14. "imuslab.com/arozos/mod/info/logger"
  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. systemWideLogger.PrintAndLog("System", "<!> Shutting down auth gateway", nil)
  36. authAgent.Close()
  37. //Shutdown all storage pools
  38. systemWideLogger.PrintAndLog("System", "<!> Shutting down storage pools", nil)
  39. closeAllStoragePools()
  40. //Shutdown Logger
  41. systemWideLogger.Close()
  42. //Shutdown database
  43. systemWideLogger.PrintAndLog("System", "<!> Shutting down database", nil)
  44. sysdb.Close()
  45. //Shutdown network services
  46. StopNetworkServices()
  47. //Shutdown FTP Server
  48. if FTPManager != nil {
  49. systemWideLogger.PrintAndLog("System", "<!> Shutting down FTP Server", nil)
  50. FTPManager.StopFtpServer()
  51. }
  52. //Cleaning up tmp files
  53. systemWideLogger.PrintAndLog("System", "<!> Cleaning up tmp folder", nil)
  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. logger.SetGlobalJSONOutput(*log_format == "json")
  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. //Clean up previous tmp files
  72. final_tmp_directory := filepath.Clean(*tmp_directory) + "/tmp/"
  73. tmp_directory = &final_tmp_directory
  74. os.RemoveAll(*tmp_directory)
  75. os.Mkdir(*tmp_directory, 0777)
  76. // Initialize a temporary stdout-only logger so calls before RunStartup are safe.
  77. // RunStartup will replace this with a file-backed logger.
  78. systemWideLogger, _ = logger.NewTmpLogger()
  79. logger.SetDefaultLogger(systemWideLogger)
  80. //Print copyRight information
  81. systemWideLogger.PrintAndLog("System", "ArozOS(C) "+strconv.Itoa(time.Now().Year())+" "+deviceVendor+".", nil)
  82. systemWideLogger.PrintAndLog("System", "ArozOS "+build_version+" Revision "+internal_version, nil)
  83. /*
  84. New Implementation of the ArOZ Online System, Sept 2020
  85. */
  86. RunStartup()
  87. /*
  88. Development build test execution
  89. */
  90. Run_Test()
  91. //Initiate all the static files transfer
  92. baseFileServer := http.FileServer(http.Dir("./web"))
  93. vendorWebDir := filepath.Join(vendorResRoot, "web")
  94. // Wrap the base file server: serve from vendorResRoot/web/ when a matching
  95. // file exists there, falling through to ./web/ otherwise.
  96. fileServer := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  97. vendorPath := filepath.Join(vendorWebDir, filepath.FromSlash(r.URL.Path))
  98. if !vendorWebExists {
  99. // Early exit if the vendor web directory doesn't exist, to avoid unnecessary file existence checks on every request.
  100. baseFileServer.ServeHTTP(w, r)
  101. return
  102. }
  103. if info, err := os.Stat(vendorPath); err == nil && !info.IsDir() {
  104. http.ServeFile(w, r, vendorPath)
  105. return
  106. }
  107. baseFileServer.ServeHTTP(w, r)
  108. })
  109. vendorWebExists = fs.FileExists(vendorWebDir)
  110. //Updates 2022-09-06: Gzip handler moved inside the master router
  111. http.Handle("/", mrouter(fileServer))
  112. //Setup handler for Ctrl +C
  113. SetupCloseHandler()
  114. //Start http server
  115. go func() {
  116. if *use_tls {
  117. if !*disable_http {
  118. go func() {
  119. address := fmt.Sprintf("%s:%d", *listen_host, *listen_port)
  120. systemWideLogger.PrintAndLog("System", fmt.Sprint("Standard (HTTP) Web server listening at", address), nil)
  121. http.ListenAndServe(address, nil)
  122. }()
  123. }
  124. address := fmt.Sprintf("%s:%d", *listen_host, *tls_listen_port)
  125. systemWideLogger.PrintAndLog("System", fmt.Sprint("Secure (HTTPS) Web server listening at", address), nil)
  126. http.ListenAndServeTLS(address, *tls_cert, *tls_key, nil)
  127. } else {
  128. address := fmt.Sprintf("%s:%d", *listen_host, *listen_port)
  129. systemWideLogger.PrintAndLog("System", fmt.Sprint("Web server listening at", address), nil)
  130. http.ListenAndServe(address, nil)
  131. }
  132. }()
  133. if *enable_console {
  134. //Startup interactive shell for debug and basic controls
  135. Console := console.NewConsole(consoleCommandHandler)
  136. Console.ListenAndHandle()
  137. } else {
  138. //Just do a blocking loop here
  139. select {}
  140. }
  141. }