subservice.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package main
  2. import (
  3. "net/http"
  4. "os"
  5. "path/filepath"
  6. fs "imuslab.com/arozos/mod/filesystem"
  7. prout "imuslab.com/arozos/mod/prouter"
  8. subservice "imuslab.com/arozos/mod/subservice"
  9. "imuslab.com/arozos/mod/utils"
  10. )
  11. /*
  12. ArOZ Online System - Dynamic Subsystem loading services
  13. */
  14. var (
  15. ssRouter *subservice.SubServiceRouter
  16. reservePaths = []string{
  17. "web",
  18. "system",
  19. "SystemAO",
  20. "img",
  21. "STDIN",
  22. "STDOUT",
  23. "STDERR",
  24. "COM",
  25. "ws",
  26. }
  27. )
  28. func SubserviceInit() {
  29. //If subservice is disabled, do not register endpoints
  30. if *disable_subservices {
  31. return
  32. }
  33. //Create a new subservice handler
  34. ssRouter = subservice.NewSubServiceRouter(
  35. reservePaths,
  36. subserviceBasePort,
  37. userHandler,
  38. moduleHandler,
  39. *listen_port,
  40. )
  41. //Create an admin router for subservice related functions
  42. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  43. ModuleName: "System Setting",
  44. AdminOnly: false,
  45. UserHandler: userHandler,
  46. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  47. utils.SendErrorResponse(w, "Permission Denied")
  48. },
  49. })
  50. //Register url endpoints
  51. adminRouter.HandleFunc("/system/subservice/list", ssRouter.HandleListing)
  52. adminRouter.HandleFunc("/system/subservice/kill", ssRouter.HandleKillSubService)
  53. adminRouter.HandleFunc("/system/subservice/start", ssRouter.HandleStartSubService)
  54. //Make subservice dir
  55. os.MkdirAll("./subservice", 0644)
  56. //Scan and load all subservice modules
  57. subservices, _ := filepath.Glob("./subservice/*")
  58. for _, servicePath := range subservices {
  59. if fs.IsDir(servicePath) && !fs.FileExists(servicePath+"/.disabled") {
  60. //Only enable module with no suspended config file
  61. ssRouter.Launch(servicePath, true)
  62. }
  63. }
  64. }
  65. //Stop all the subprocess correctly
  66. func SubserviceHandleShutdown() {
  67. if ssRouter != nil {
  68. ssRouter.Close()
  69. }
  70. }