agi.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package main
  2. import (
  3. "net/http"
  4. agi "imuslab.com/arozos/mod/agi"
  5. prout "imuslab.com/arozos/mod/prouter"
  6. "imuslab.com/arozos/mod/utils"
  7. )
  8. var (
  9. AGIGateway *agi.Gateway
  10. )
  11. func AGIInit() {
  12. //Create new AGI Gateway object
  13. gw, err := agi.NewGateway(agi.AgiSysInfo{
  14. BuildVersion: build_version,
  15. InternalVersion: internal_version,
  16. LoadedModule: moduleHandler.GetModuleNameList(),
  17. ReservedTables: []string{"auth", "permisson", "register", "desktop"},
  18. ModuleRegisterParser: moduleHandler.RegisterModuleFromAGI,
  19. PackageManager: packageManager,
  20. UserHandler: userHandler,
  21. StartupRoot: "./web",
  22. ActivateScope: []string{"./web", "./subservice"},
  23. FileSystemRender: thumbRenderHandler,
  24. ShareManager: shareManager,
  25. NightlyManager: nightlyManager,
  26. TempFolderPath: *tmp_directory,
  27. })
  28. if err != nil {
  29. systemWideLogger.PrintAndLog("AGI", "AGI Gateway Initialization Failed", err)
  30. }
  31. //Register user request handler endpoint
  32. http.HandleFunc("/system/ajgi/interface", func(w http.ResponseWriter, r *http.Request) {
  33. //Require login check
  34. authAgent.HandleCheckAuth(w, r, func(w http.ResponseWriter, r *http.Request) {
  35. //API Call from actual human users
  36. thisuser, _ := gw.Option.UserHandler.GetUserInfoFromRequest(w, r)
  37. gw.InterfaceHandler(w, r, thisuser)
  38. })
  39. })
  40. //Register external API request handler endpoint
  41. http.HandleFunc("/api/ajgi/interface", func(w http.ResponseWriter, r *http.Request) {
  42. //Check if token exists
  43. token, err := utils.PostPara(r, "token")
  44. if err != nil {
  45. w.WriteHeader(http.StatusUnauthorized)
  46. w.Write([]byte("401 - Unauthorized (token is empty)"))
  47. return
  48. }
  49. //Validate Token
  50. if authAgent.TokenValid(token) {
  51. //Valid
  52. thisUsername, err := gw.Option.UserHandler.GetAuthAgent().GetTokenOwner(token)
  53. if err != nil {
  54. systemWideLogger.PrintAndLog("AGI", "Unable to validate token owner", err)
  55. w.WriteHeader(http.StatusInternalServerError)
  56. w.Write([]byte("500 - Internal Server Error"))
  57. return
  58. }
  59. thisuser, _ := gw.Option.UserHandler.GetUserInfoFromUsername(thisUsername)
  60. gw.APIHandler(w, r, thisuser)
  61. } else {
  62. w.WriteHeader(http.StatusUnauthorized)
  63. w.Write([]byte("401 - Unauthorized (Invalid / expired token)"))
  64. return
  65. }
  66. })
  67. http.HandleFunc("/api/ajgi/exec", gw.HandleAgiExecutionRequestWithToken)
  68. // external AGI related function
  69. externalAGIRouter := prout.NewModuleRouter(prout.RouterOption{
  70. ModuleName: "ARZ Serverless",
  71. AdminOnly: false,
  72. UserHandler: userHandler,
  73. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  74. errorHandlePermissionDenied(w, r)
  75. },
  76. })
  77. externalAGIRouter.HandleFunc("/api/ajgi/listExt", gw.ListExternalEndpoint)
  78. externalAGIRouter.HandleFunc("/api/ajgi/addExt", gw.AddExternalEndPoint)
  79. externalAGIRouter.HandleFunc("/api/ajgi/rmExt", gw.RemoveExternalEndPoint)
  80. AGIGateway = gw
  81. }