main.go 5.6 KB

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