main.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 cluster agent so peers stop routing to this node
  35. systemWideLogger.PrintAndLog("System", "<!> Shutting down cluster agent", nil)
  36. ClusterShutdown()
  37. //Shutdown authAgent
  38. systemWideLogger.PrintAndLog("System", "<!> Shutting down auth gateway", nil)
  39. authAgent.Close()
  40. //Shutdown the mail backend (outbox, IMAP connections, mail database)
  41. if mailBackend != nil {
  42. systemWideLogger.PrintAndLog("System", "<!> Shutting down mail backend", nil)
  43. mailBackend.Close()
  44. }
  45. //Shutdown all storage pools
  46. systemWideLogger.PrintAndLog("System", "<!> Shutting down storage pools", nil)
  47. closeAllStoragePools()
  48. //Shutdown Logger
  49. systemWideLogger.Close()
  50. //Shutdown database
  51. systemWideLogger.PrintAndLog("System", "<!> Shutting down database", nil)
  52. sysdb.Close()
  53. //Shutdown network services
  54. StopNetworkServices()
  55. //Shutdown Arozcast TURN relay
  56. if arozcastTurnRunning() {
  57. systemWideLogger.PrintAndLog("System", "<!> Shutting down Arozcast TURN relay", nil)
  58. arozcastStopTurnRelay()
  59. }
  60. //Shutdown FTP Server
  61. if FTPManager != nil {
  62. systemWideLogger.PrintAndLog("System", "<!> Shutting down FTP Server", nil)
  63. FTPManager.StopFtpServer()
  64. }
  65. //Shutdown media server, stopping any running HLS transcode
  66. if mediaServer != nil {
  67. systemWideLogger.PrintAndLog("System", "<!> Shutting down media server", nil)
  68. mediaServer.Close()
  69. }
  70. //Cleaning up tmp files
  71. systemWideLogger.PrintAndLog("System", "<!> Cleaning up tmp folder", nil)
  72. os.RemoveAll(*tmp_directory)
  73. //Do other things
  74. os.Exit(0)
  75. }
  76. func main() {
  77. //Parse startup flags and paramters
  78. flag.Parse()
  79. logger.SetGlobalJSONOutput(*log_format == "json")
  80. //Handle version printing
  81. if *show_version {
  82. fmt.Println("ArozOS " + build_version + " Revision " + internal_version)
  83. fmt.Println("Developed by tobychui and other co-developers, Licensed to " + deviceVendor)
  84. //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.")
  85. os.Exit(0)
  86. }
  87. //Handle flag assignments
  88. max_upload_size = int64(*max_upload) << 20 //Parse the max upload size
  89. //Clean up previous tmp files
  90. final_tmp_directory := filepath.Clean(*tmp_directory) + "/tmp/"
  91. tmp_directory = &final_tmp_directory
  92. os.RemoveAll(*tmp_directory)
  93. os.Mkdir(*tmp_directory, 0777)
  94. // Initialize a temporary stdout-only logger so calls before RunStartup are safe.
  95. // RunStartup will replace this with a file-backed logger.
  96. systemWideLogger, _ = logger.NewTmpLogger()
  97. logger.SetDefaultLogger(systemWideLogger)
  98. //Print copyRight information
  99. systemWideLogger.PrintAndLog("System", "ArozOS(C) "+strconv.Itoa(time.Now().Year())+" "+deviceVendor+".", nil)
  100. systemWideLogger.PrintAndLog("System", "ArozOS "+build_version+" Revision "+internal_version, nil)
  101. /*
  102. New Implementation of the ArOZ Online System, Sept 2020
  103. */
  104. RunStartup()
  105. /*
  106. Development build test execution
  107. */
  108. Run_Test()
  109. //Initiate all the static files transfer
  110. baseFileServer := http.FileServer(http.Dir("./web"))
  111. vendorWebDir := filepath.Join(vendorResRoot, "web")
  112. // Wrap the base file server: serve from vendorResRoot/web/ when a matching
  113. // file exists there, falling through to ./web/ otherwise.
  114. fileServer := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  115. vendorPath := filepath.Join(vendorWebDir, filepath.FromSlash(r.URL.Path))
  116. if !vendorWebExists {
  117. // Early exit if the vendor web directory doesn't exist, to avoid unnecessary file existence checks on every request.
  118. baseFileServer.ServeHTTP(w, r)
  119. return
  120. }
  121. if info, err := os.Stat(vendorPath); err == nil && !info.IsDir() {
  122. http.ServeFile(w, r, vendorPath)
  123. return
  124. }
  125. baseFileServer.ServeHTTP(w, r)
  126. })
  127. vendorWebExists = fs.FileExists(vendorWebDir)
  128. //Updates 2022-09-06: Gzip handler moved inside the master router
  129. http.Handle("/", mrouter(fileServer))
  130. //Setup handler for Ctrl +C
  131. SetupCloseHandler()
  132. //Start http server
  133. go func() {
  134. if *use_tls {
  135. if !*disable_http {
  136. go func() {
  137. address := fmt.Sprintf("%s:%d", *listen_host, *listen_port)
  138. systemWideLogger.PrintAndLog("System", fmt.Sprint("Standard (HTTP) Web server listening at", address), nil)
  139. http.ListenAndServe(address, nil)
  140. }()
  141. }
  142. address := fmt.Sprintf("%s:%d", *listen_host, *tls_listen_port)
  143. systemWideLogger.PrintAndLog("System", fmt.Sprint("Secure (HTTPS) Web server listening at", address), nil)
  144. http.ListenAndServeTLS(address, *tls_cert, *tls_key, nil)
  145. } else {
  146. address := fmt.Sprintf("%s:%d", *listen_host, *listen_port)
  147. systemWideLogger.PrintAndLog("System", fmt.Sprint("Web server listening at", address), nil)
  148. http.ListenAndServe(address, nil)
  149. }
  150. }()
  151. if *enable_console {
  152. //Startup interactive shell for debug and basic controls
  153. Console := console.NewConsole(consoleCommandHandler)
  154. Console.ListenAndHandle()
  155. } else {
  156. //Just do a blocking loop here
  157. select {}
  158. }
  159. }