subservice.go 1.7 KB

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