123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- package main
- import (
- "net/http"
- agi "imuslab.com/arozos/mod/agi"
- "imuslab.com/arozos/mod/common"
- )
- var (
- AGIGateway *agi.Gateway
- )
- func AGIInit() {
- //Create new AGI Gateway object
- gw, err := agi.NewGateway(agi.AgiSysInfo{
- BuildVersion: build_version,
- InternalVersion: internal_version,
- LoadedModule: moduleHandler.GetModuleNameList(),
- ReservedTables: []string{"auth", "permisson", "register", "desktop"},
- ModuleRegisterParser: moduleHandler.RegisterModuleFromAGI,
- PackageManager: packageManager,
- UserHandler: userHandler,
- StartupRoot: "./web",
- ActivateScope: []string{"./web", "./subservice"},
- FileSystemRender: thumbRenderHandler,
- ShareManager: shareManager,
- NightlyManager: nightlyManager,
- TempFolderPath: *tmp_directory,
- })
- if err != nil {
- systemWideLogger.PrintAndLog("AGI", "AGI Gateway Initialization Failed", err)
- }
- //Register user request handler endpoint
- http.HandleFunc("/system/ajgi/interface", func(w http.ResponseWriter, r *http.Request) {
- //Require login check
- authAgent.HandleCheckAuth(w, r, func(w http.ResponseWriter, r *http.Request) {
- //API Call from actual human users
- thisuser, _ := gw.Option.UserHandler.GetUserInfoFromRequest(w, r)
- gw.InterfaceHandler(w, r, thisuser)
- })
- })
- //Register external API request handler endpoint
- http.HandleFunc("/api/ajgi/interface", func(w http.ResponseWriter, r *http.Request) {
- //Check if token exists
- token, err := common.Mv(r, "token", true)
- if err != nil {
- w.WriteHeader(http.StatusUnauthorized)
- w.Write([]byte("401 - Unauthorized (token is empty)"))
- return
- }
- //Validate Token
- if authAgent.TokenValid(token) {
- //Valid
- thisUsername, err := gw.Option.UserHandler.GetAuthAgent().GetTokenOwner(token)
- if err != nil {
- systemWideLogger.PrintAndLog("AGI", "Unable to validate token owner", err)
- w.WriteHeader(http.StatusInternalServerError)
- w.Write([]byte("500 - Internal Server Error"))
- return
- }
- thisuser, _ := gw.Option.UserHandler.GetUserInfoFromUsername(thisUsername)
- gw.APIHandler(w, r, thisuser)
- } else {
- w.WriteHeader(http.StatusUnauthorized)
- w.Write([]byte("401 - Unauthorized (Invalid / expired token)"))
- return
- }
- })
- http.HandleFunc("/api/ajgi/exec", gw.HandleAgiExecutionRequestWithToken)
- AGIGateway = gw
- }
|