Alan Yeung 2 роки тому
батько
коміт
3ba8b6f478
2 змінених файлів з 79 додано та 70 видалено
  1. 0 70
      mod/agi/agi.go
  2. 79 0
      mod/agi/external.agi.go

+ 0 - 70
mod/agi/agi.go

@@ -9,7 +9,6 @@ import (
 	"net/http"
 	"os"
 	"path/filepath"
-	"reflect"
 	"strings"
 	"time"
 
@@ -17,7 +16,6 @@ import (
 	uuid "github.com/satori/go.uuid"
 
 	apt "imuslab.com/arozos/mod/apt"
-	"imuslab.com/arozos/mod/common"
 	"imuslab.com/arozos/mod/filesystem"
 	metadata "imuslab.com/arozos/mod/filesystem/metadata"
 	"imuslab.com/arozos/mod/iot"
@@ -210,74 +208,6 @@ func (g *Gateway) APIHandler(w http.ResponseWriter, r *http.Request, thisuser *u
 	g.ExecuteAGIScript(scriptContent, "", "", w, r, thisuser)
 }
 
-//Handle request from RESTFUL API
-func (g *Gateway) ExtAPIHandler(w http.ResponseWriter, r *http.Request) {
-	// obtain all information from "DB" aka our json file
-	// this should be either db or in the constructor instead for production
-	var db map[string]map[string]string
-	jsonFile, err := os.Open("lambda.json")
-	if err != nil {
-		common.SendErrorResponse(w, "Bad config")
-		return
-	}
-	jsonStr, err := ioutil.ReadAll(jsonFile)
-	if err != nil {
-		common.SendErrorResponse(w, "Bad config")
-		return
-	}
-	json.Unmarshal([]byte(jsonStr), &db)
-	defer jsonFile.Close()
-	// end of our DB
-
-	// get the request URI from the r.URL
-	requestURI := filepath.ToSlash(filepath.Clean(r.URL.Path))
-	subpathElements := strings.Split(requestURI[1:], "/")
-
-	// check if it contains only two part, [rexec uuid]
-	if len(subpathElements) != 2 {
-		common.SendErrorResponse(w, "Bad Request, invaild request sent")
-		return
-	}
-
-	// check if UUID exists in the database
-	if db[subpathElements[1]] == nil || reflect.ValueOf(db[subpathElements[1]]).IsNil() {
-		common.SendErrorResponse(w, "Bad Request, invaild UUID entered")
-		return
-	}
-
-	// get the info from the database
-	usernameFromDb := db[subpathElements[1]]["username"]
-	pathFromDb := db[subpathElements[1]]["path"]
-
-	// get the userinfo and the realPath
-	userInfo, err := g.Option.UserHandler.GetUserInfoFromUsername(usernameFromDb)
-	if err != nil {
-		common.SendErrorResponse(w, "Bad username")
-		return
-	}
-	_, realPath, err := virtualPathToRealPath(pathFromDb, userInfo)
-	if err != nil {
-		common.SendErrorResponse(w, "Bad filepath")
-		return
-	}
-
-	// read the file and store it into scriptContent
-	scriptContentByte, err := ioutil.ReadFile(realPath)
-	if err != nil {
-		common.SendErrorResponse(w, "Bad file I/O")
-		return
-	}
-	scriptContent := string(scriptContentByte)
-
-	// execute!
-	start := time.Now()
-	g.ExecuteAGIScript(scriptContent, "", "", w, r, userInfo)
-	duration := time.Since(start)
-
-	log.Println("[Remote AGI] IP:", r.RemoteAddr, " executed the script ", pathFromDb, "(", realPath, ")", " on behalf of", userInfo.Username, "with total duration: ", duration)
-
-}
-
 //Handle user requests
 func (g *Gateway) InterfaceHandler(w http.ResponseWriter, r *http.Request, thisuser *user.User) {
 	//Get user object from the request

+ 79 - 0
mod/agi/external.agi.go

@@ -0,0 +1,79 @@
+package agi
+
+import (
+	"encoding/json"
+	"io/ioutil"
+	"log"
+	"net/http"
+	"path/filepath"
+	"strings"
+	"time"
+
+	"imuslab.com/arozos/mod/common"
+)
+
+//Handle request from EXTERNAL RESTFUL API
+func (g *Gateway) ExtAPIHandler(w http.ResponseWriter, r *http.Request) {
+	// get db
+	sysdb := g.Option.UserHandler.GetDatabase()
+
+	if !sysdb.TableExists("external_agi") {
+		common.SendErrorResponse(w, "Bad Request, invaild database")
+		return
+	}
+
+	// get the request URI from the r.URL
+	requestURI := filepath.ToSlash(filepath.Clean(r.URL.Path))
+	subpathElements := strings.Split(requestURI[1:], "/")
+
+	// check if it contains only two part, [rexec uuid]
+	if len(subpathElements) != 2 {
+		common.SendErrorResponse(w, "Bad Request, invaild request sent")
+		return
+	}
+
+	// check if UUID exists in the database
+	if sysdb.KeyExists("external_agi", subpathElements[1]) {
+		common.SendErrorResponse(w, "Bad Request, invaild UUID entered")
+		return
+	}
+
+	// get the info from the database
+	var dat struct {
+		Username string `json:"username"`
+		Path     string `json:"path"`
+	}
+	jsonData := ""
+	sysdb.Read("external_agi", subpathElements[1], &jsonData)
+	json.Unmarshal([]byte(jsonData), &dat)
+	usernameFromDb := dat.Username
+	pathFromDb := dat.Path
+
+	// get the userinfo and the realPath
+	userInfo, err := g.Option.UserHandler.GetUserInfoFromUsername(usernameFromDb)
+	if err != nil {
+		common.SendErrorResponse(w, "Bad username")
+		return
+	}
+	_, realPath, err := virtualPathToRealPath(pathFromDb, userInfo)
+	if err != nil {
+		common.SendErrorResponse(w, "Bad filepath")
+		return
+	}
+
+	// read the file and store it into scriptContent
+	scriptContentByte, err := ioutil.ReadFile(realPath)
+	if err != nil {
+		common.SendErrorResponse(w, "Bad file I/O")
+		return
+	}
+	scriptContent := string(scriptContentByte)
+
+	// execute!
+	start := time.Now()
+	g.ExecuteAGIScript(scriptContent, "", "", w, r, userInfo)
+	duration := time.Since(start)
+
+	log.Println("[Remote AGI] IP:", r.RemoteAddr, " executed the script ", pathFromDb, "(", realPath, ")", " on behalf of", userInfo.Username, "with total duration: ", duration)
+
+}