diff options
| -rwxr-xr-x | backend/data/otolToSqlite.py | 2 | ||||
| -rwxr-xr-x | backend/server.py | 130 | ||||
| -rw-r--r-- | src/App.vue | 4 | ||||
| -rw-r--r-- | src/components/SearchModal.vue | 5 | ||||
| -rw-r--r-- | vite.config.js | 2 |
5 files changed, 65 insertions, 78 deletions
diff --git a/backend/data/otolToSqlite.py b/backend/data/otolToSqlite.py index c393474..93ed294 100755 --- a/backend/data/otolToSqlite.py +++ b/backend/data/otolToSqlite.py @@ -35,7 +35,7 @@ idToName = {} # Maps node IDs to names # Check for existing db if os.path.exists(dbFile): - print("ERROR: Existing {} file".format(dbFile), file=sys.stderr) + print("ERROR: Existing {} db".format(dbFile), file=sys.stderr) sys.exit(1) # Parse treeFile diff --git a/backend/server.py b/backend/server.py index 2ff7e74..ec43ff3 100755 --- a/backend/server.py +++ b/backend/server.py @@ -12,13 +12,14 @@ tolnodeReqDepth = 2 # A depth of 0 means only respond with one node usageInfo = f"usage: {sys.argv[0]}\n" -usageInfo += "Starts a server that listens for GET requests to " + hostname + ":" + str(port) + "/tolnode/name1,\n" -usageInfo += "and responds with JSON representing an object mapping names to node objects.\n" -usageInfo += "Normally, the response includes node name1, and child nodes up to depth " + str(tolnodeReqDepth) + ".\n" +usageInfo += "Starts a server that listens for GET requests to http://" + hostname + ":" + str(port) + ".\n" +usageInfo += "Responds to path+query /data/type1?name=name1 with JSON data.\n" usageInfo += "\n" -usageInfo += "A query section ?type=type1 can be used to affect the result.\n" -usageInfo += "If type1 is 'children', the operation acts on node name1's children, and the results combined.\n" -usageInfo += "If type1 is 'chain', the response includes nodes from name1 up to the root, and their direct children.\n" +usageInfo += "If type1 is 'node': \n" +usageInfo += " Responds with a map from names to node objects, representing\n" +usageInfo += " nodes name1, and child nodes up to depth " + str(tolnodeReqDepth) + ".\n" +usageInfo += "If type1 is 'children': Like 'node', but excludes node name1.\n" +usageInfo += "If type1 is 'chain': Like 'node', but gets nodes from name1 up to the root, and their direct children.\n" dbCon = sqlite3.connect(dbFile) def lookupName(name): @@ -26,21 +27,6 @@ def lookupName(name): cur.execute("SELECT name, data FROM nodes WHERE name = ?", (name,)) row = cur.fetchone() return row[1] if row != None else None -#def lookupNameLike(name): -# cur = dbCon.cursor() -# found = False -# cur.execute("SELECT name, data FROM nodes WHERE name like ?", ("%{}%".format(name),)) -# rows = cur.fetchall() -# if len(rows) == 0: -# return None -# else: -# jsonStr = "{" -# for i in range(len(rows)): -# jsonStr += json.dumps(rows[i][0]) + ":" + rows[i][1] -# if i < len(njList) - 1: -# jsonStr += "," -# jsonStr += "}" -# return jsonFromNameJsonList(rows) class DbServer(BaseHTTPRequestHandler): def do_GET(self): @@ -50,60 +36,60 @@ class DbServer(BaseHTTPRequestHandler): queryDict = urllib.parse.parse_qs(urlParts.query) # Check first element of path match = re.match(r"/([^/]+)/(.+)", path) - if match != None: - reqType = match.group(1) - if reqType == "tolnode": - name = match.group(2) - # Check query string - if "type" not in queryDict: - nodeJson = lookupName(name) - if nodeJson != None: - results = [] - getResultsUntilDepth(name, nodeJson, tolnodeReqDepth, results) - self.respondJson(nodeResultsToJSON(results)) - return - elif queryDict["type"][0] == "children": - nodeJson = lookupName(name) - if nodeJson != None: - obj = json.loads(nodeJson) - results = [] + if match != None and match.group(1) == "data" and "name" in queryDict: + reqType = match.group(2) + name = queryDict["name"][0] + print(name) + # Check query string + if reqType == "node": + nodeJson = lookupName(name) + if nodeJson != None: + results = [] + getResultsUntilDepth(name, nodeJson, tolnodeReqDepth, results) + self.respondJson(nodeResultsToJSON(results)) + return + elif reqType == "children": + nodeJson = lookupName(name) + if nodeJson != None: + obj = json.loads(nodeJson) + results = [] + for childName in obj["children"]: + nodeJson = lookupName(childName) + if nodeJson != None: + getResultsUntilDepth(childName, nodeJson, tolnodeReqDepth, results) + self.respondJson(nodeResultsToJSON(results)) + return + elif reqType == "chain": + results = [] + ranOnce = False + while True: + jsonResult = lookupName(name) + if jsonResult == None: + if ranOnce: + print("ERROR: Parent-chain node {} not found".format(name), file=sys.stderr) + break + results.append([name, jsonResult]) + obj = json.loads(jsonResult) + # Add children + if not ranOnce: + ranOnce = True + else: + internalFail = False for childName in obj["children"]: - nodeJson = lookupName(childName) - if nodeJson != None: - getResultsUntilDepth(childName, nodeJson, tolnodeReqDepth, results) + jsonResult = lookupName(childName) + if jsonResult == None: + print("ERROR: Parent-chain-child node {} not found".format(name), file=sys.stderr) + internalFail = True + break + results.append([childName, jsonResult]) + if internalFail: + break + # Check if root + if obj["parent"] == None: self.respondJson(nodeResultsToJSON(results)) return - elif queryDict["type"][0] == "chain": - results = [] - ranOnce = False - while True: - jsonResult = lookupName(name) - if jsonResult == None: - if ranOnce: - print("ERROR: Parent-chain node {} not found".format(name), file=sys.stderr) - break - results.append([name, jsonResult]) - obj = json.loads(jsonResult) - # Add children - if not ranOnce: - ranOnce = True - else: - internalFail = False - for childName in obj["children"]: - jsonResult = lookupName(childName) - if jsonResult == None: - print("ERROR: Parent-chain-child node {} not found".format(name), file=sys.stderr) - internalFail = True - break - results.append([childName, jsonResult]) - if internalFail: - break - # Check if root - if obj["parent"] == None: - self.respondJson(nodeResultsToJSON(results)) - return - else: - name = obj["parent"] + else: + name = obj["parent"] self.send_response(404) self.end_headers() self.end_headers() diff --git a/src/App.vue b/src/App.vue index 24ae12b..89d227d 100644 --- a/src/App.vue +++ b/src/App.vue @@ -189,7 +189,7 @@ export default defineComponent({ // Check if data for node-to-expand exists, getting from server if needed let tolNode = this.tolMap.get(layoutNode.name)!; if (!this.tolMap.has(tolNode.children[0])){ - return fetch('/tolnode/' + layoutNode.name + '?type=children') + return fetch('/data/children?name=' + encodeURIComponent(layoutNode.name)) .then(response => response.json()) .then(obj => { Object.getOwnPropertyNames(obj).forEach(key => {this.tolMap.set(key, obj[key])}); @@ -492,7 +492,7 @@ export default defineComponent({ tryLayout(this.activeRoot, this.tileAreaPos, this.tileAreaDims, this.lytOpts, {allowCollapse: true, layoutMap: this.layoutMap}); // Get initial tol node data - fetch('/tolnode/' + rootName) + fetch('/data/node?name=' + encodeURIComponent(rootName)) .then(response => response.json()) .then(obj => { Object.getOwnPropertyNames(obj).forEach(key => {this.tolMap.set(key, obj[key])}); diff --git a/src/components/SearchModal.vue b/src/components/SearchModal.vue index 9c30cbc..f3bb237 100644 --- a/src/components/SearchModal.vue +++ b/src/components/SearchModal.vue @@ -20,11 +20,12 @@ export default defineComponent({ let input = this.$refs.searchInput as HTMLInputElement; // Query server let url = new URL(window.location.href); - url.pathname = '/tolnode/' + input.value; + url.pathname = '/data/node'; + url.search = '?name=' + encodeURIComponent(input.value); fetch(url.toString()) .then(response => { // Search successful. Get nodes in parent-chain, add to tolMap, then emit event. - url.search = '?type=chain'; + url.pathname = '/data/chain'; fetch(url.toString()) .then(response => response.json()) .then(obj => { diff --git a/vite.config.js b/vite.config.js index 7b02d3f..524c1d6 100644 --- a/vite.config.js +++ b/vite.config.js @@ -5,7 +5,7 @@ export default defineConfig({ plugins: [vue()], server: { proxy: { - '/tolnode': 'http://localhost:8000', + '/data': 'http://localhost:8000', } }, //server: {open: true} //open browser when dev server starts |
